0

I am trying to load the table by fetching data from the external database using PHP. I have the script on the server, and able to fetch the data. I'm trying to get help and understand the logic, but still I cant get it.

At the moment, I have only set up my tableview but there is not data.

Please let me know how can I fetch the data.

Thanks

code:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var group = [Group]()

    @IBOutlet weak var tableview: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        tableview.reloadData()
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return myarray.count
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "groupCell", for: indexPath) as! UITableViewCell
       // cell.textLabel?.text = myarray[indexPath.item]
        return cell
    }

}

Code:(how can I get the data displayed on the table view)?

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//let myarray = ["item1", "item2", "item3"]



var group = [Group]()

@IBOutlet weak var tableview: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: "http://www.myurl/myfile.php")

    let task = URLSession.shared.dataTask(with: url! as URL) { data, response, error in

        guard let data = data, error == nil else { return }

        print(NSString(data: data, encoding: String.Encoding.utf8.rawValue))
    }

    task.resume()        
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableview.reloadData()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return myarray.count
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "groupCell", for: indexPath) as! UITableViewCell
   // cell.textLabel?.text = myarray[indexPath.item]
    return cell
}

}

Kunal Parekh
  • 380
  • 6
  • 24
  • As mentioned in your question that you are using PHP to connect with external database, You need to create web services to fetch data from database. These web services will be called from mobile app to fetch data. Once receive data using webservice, then you can display that in app. – Surjeet Singh Jun 27 '17 at 04:48
  • *... and able to fetch the data*. Then show the code you are using to fetch the data. – vadian Jun 27 '17 at 04:49
  • @Surjeet how can i use this web service? I do remember reading about it, but couldnt understand at all – Kunal Parekh Jun 27 '17 at 04:51
  • @SarahMalik You can use NSUrlSession to call web services or some third party library like AFNetworking, https://github.com/Alamofire/Alamofire as well to call web services. – Surjeet Singh Jun 27 '17 at 04:55
  • @Surjeet , well I found this online [link] (https://stackoverflow.com/questions/26364914/http-request-in-swift-with-post-method) and I am able to see the desire output. How do I use the data to display on the table – Kunal Parekh Jun 27 '17 at 05:06
  • You need to create a array from data received from web service to display on a table and within func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell func you can display data on labels. – Surjeet Singh Jun 27 '17 at 05:26
  • @Surjeet , I have just posted my new code and I am not able to find relevant information and its just confuses me alot :( . If you see my second part of my code, and let me know how can I do this. Thanks.... – Kunal Parekh Jun 27 '17 at 06:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147680/discussion-between-sarah-malik-and-surjeet). – Kunal Parekh Jun 27 '17 at 06:33
  • @Surjeet, I am continuing this discussion in chat – Kunal Parekh Jun 27 '17 at 06:38

2 Answers2

1

Here is the tutorial for load data on iOS via Web Services from iOS side and create Web Services from Server side.

Create a simple PHP/MySQL web service for iOS app.

https://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app

To call that Web Service from iOS app

http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

Bhavesh Dhaduk
  • 1,888
  • 15
  • 30
1

You need to create a array or Dictionary based on your response using below code

You need to pass string to these func which you are using within print statement.

If response return as a dictionary

func convertToDictionary(text: String) -> [String: Any]? {
    if let data = text.data(using: .utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
        } catch {
            print(error.localizedDescription)
        }
    }
    return nil
}

Get array from dictionary to bind with tableview.

If response return as an Array

func convertToArray(text: String) -> [Any]? {
        if let data = text.data(using: .utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
            } catch {
                print(error.localizedDescription)
            }
        }
        return nil
    }

Then bind data from this array to tableView.

Surjeet Singh
  • 11,691
  • 2
  • 37
  • 54