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
}

}

9
  • 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. Commented Jun 27, 2017 at 4:48
  • ... and able to fetch the data. Then show the code you are using to fetch the data. Commented Jun 27, 2017 at 4:49
  • @Surjeet how can i use this web service? I do remember reading about it, but couldnt understand at all Commented Jun 27, 2017 at 4:51
  • @SarahMalik You can use NSUrlSession to call web services or some third party library like AFNetworking, github.com/Alamofire/Alamofire as well to call web services. Commented Jun 27, 2017 at 4:55
  • @Surjeet , well I found this online [link] (stackoverflow.com/questions/26364914/…) and I am able to see the desire output. How do I use the data to display on the table Commented Jun 27, 2017 at 5:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.