0

I have the code in Controller like this:

public function akses(){
    $username = $this->input->post('username');
    $password = $this->input->post('password');

    $this->form_validation->set_rules('username','username','required|trim|max_length[36]');
    $this->form_validation->set_rules('password','password','required|trim|max_length[500]');


    if($this->form_validation->run()==true){
        $data = array("UserName" => $username, "Password" => $password);
        $data_string = json_encode($data);
        $ch = curl_init($this->ws_url->GetUrl('UserLogin'));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        //execute post
        $result = json_decode(curl_exec($ch), true);
        //close connection
        curl_close($ch);

        if(count($result['User']) != NULL){
            echo "Data Found";

            foreach($result['User'] as $detail){
                echo $detail['IdUser'];
        }


        }
    }
}

I want to get the data of $detail['IdUser']. But when I try to get it, it returns error Illegal string offset 'IdUser'. But the data was found. I checked it directly to my web service it has no problem.

And when I try to var_dump($result['User']) it has no problem too. Why when I foreach the data, it is returns error like I said above? Any solution for me? Thanks

EDIT This is the result of var_dump($result['User']) : array(6) { ["IdUser"]=> string(36) "fad706d1-b481-f1cb-81fa-9009ee7d75d4" ["Username"]=> string(4) "kugi" ["Password"]=> string(4) "kugi" ["Email"]=> string(14) "[email protected]" ["Fullname"]=> string(33) "Katalog Unsur Geografis Indonesia" ["IdentityNumber"]=> string(10) "1001001001" }

4
  • 2
    paste result of var_dump($result['User']) Commented Nov 30, 2015 at 7:50
  • @Saty I edited my post above Commented Nov 30, 2015 at 7:52
  • 2
    Don't use foreach just use $result['User']["IdUser"] Commented Nov 30, 2015 at 7:54
  • 1
    @Saty it works fine. But can you explain me why it is error? When the code above I try on my friend's computer. It works fine with that foreach. Thanks Commented Nov 30, 2015 at 7:58

1 Answer 1

1

You don't need to use a foreach loop. you already have this information:

public function akses(){
$username = $this->input->post('username');
$password = $this->input->post('password');

$this->form_validation->set_rules('username','username','required|trim|max_length[36]');
$this->form_validation->set_rules('password','password','required|trim|max_length[500]');


if($this->form_validation->run()==true){
    $data = array("UserName" => $username, "Password" => $password);
    $data_string = json_encode($data);
    $ch = curl_init($this->ws_url->GetUrl('UserLogin'));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    //execute post
    $result = json_decode(curl_exec($ch), true);
    //close connection
    curl_close($ch);

    if(count($result['User']) != NULL){
        echo "Data Found";
        echo $result['User']["IdUser"];
    }
}
}
Sign up to request clarification or add additional context in comments.

4 Comments

this is really helped me. But can you explain me why it is error? When the code above I try on my friend's computer. It works fine with that foreach. Thanks
@gultomicro maybe you tried different code at your friend's computer? because the code will run the same on all computers..
In fact I get that code from my friend. In his computer it runs with no error
If you are both running the code in localhost probably he had his security warnings turn off and you don't you can do echo @$detail['IdUser']; and it will ignore error. you see this error because this is not proper loop

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.