1

I am developing a system project. How to display 3 column value from MySQL to 3 UILabel(1. nameLabel.text, 2. idnumberLabel.text, 3. statusLabel.text) using Alamofire.

PHP Code xxxx.php:

 if ($_GET) {
        $idnumber = $_GET['idnumber'];

        // get a product from products table
        $stmt = $pdo->prepare("SELECT * FROM register WHERE idnumber = :idnumber");
        $stmt->execute(array(":idnumber" => $idnumber));
        $result = $stmt->fetch();

        if (!empty($result)) {
            // check for empty result
            if ($stmt->rowCount() > 0) {

                $user = array();
                $user["name"] = $result["name"];
                $user["idnumber"] = $result["idnumber"];
                $user["status"] = $result["status"];
                // success
                $response["success"] = 1;

                // user node
                $response["user"] = array();

                array_push($response["user"], $user);

                // echoing JSON response
                echo json_encode($response);
            } else {
                // no product found
                $response["success"] = 0;
                $response["error_message"] = "Not registered user";

                // echo no users JSON
                echo json_encode($response);
            }
        } else {
            // no product found
            $response["success"] = 0;
            $response["error_message"] = "No user found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // required field is missing
        $response["success"] = 0;
        $response["error_message"] = "Required field(s) is missing";

        // echoing JSON response
        echo json_encode($response);
    }

this is the link to find user by id number http://localhost/xxxx.php/idnumber=? I want to use UITextfield to execute the idnumber.

JSON encode

  {"success":1,"user":[{"name":"PETER JENSEN","idnumber":"35433332","status":"PROCESS"}]}

Swift Code:

 @IBAction func CheckUserStatusTapped(sender: UIButton) {
        var idnumber = self.idnumberTextField.text
        let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
        let parameters =
            ["idnumber": idnumber]

        //load and parse the JSON into an array
        Alamofire.request(.POST, "http://localhost/xxxx.php", parameters: parameters).responseJSON { (request, response, data, error) in

                 var name = jsonResult [0]["name"]
            var idnumber = jsonResult [0]["idnumber"]
            var status = jsonResult [0]["status"]

        }
    }

I tried this but it got nothing and error. I dont know to display this value to the UILabel.Please help me :(. Im so confuse. Sorry for my bad english!

2 Answers 2

1

When you declare var idnumber and assign it idnumberTextField.text, you're making a copy of the String that's stored at idnumberTextField.text. When you go and assign something else to idnumber later, it doesn't do anything to idnumberTextField.text. This is a key understanding you will want to develop about programming.

The declaration of idnumber in the scope of CheckUserStatusTapped is also being shadowed by your idnumber declaration in the response handler. Even if you weren't doing that, assigning to idnumber doesn't change the .text property on idnumberTextField.

You need to modify the UILabel object itself to make it display something else.

You also don't want to update UI from a background thread. In your response handler, you're going to want to dispatch a function to the main thread like so:

dispatch_async(dispatch_get_main_queue()) {
    self.idnumberTextField.text = jsonResult[0]["idnumber"]
}
Sign up to request clarification or add additional context in comments.

2 Comments

What should I do to idnumberTextField.text to make it a String? What do you mean by I need to modify the UILabel object to make it display something else?
Assign the desired string value to idnumberTextField as I did in the example.
1

If your retrieved data is correct, this question show how you can parse a JSON response. Infact in your code jsonResult is never declared.

Swift/PHP How to display mysql data (json) in UITableView using Alamofire

Comments

Your Answer

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