user3766509
user3766509

Reputation: 37

Access array variable in CodeIgniter

this is my module

function order_alert($email){

        $this->db->select("tbl_customer_registration.cus_mobile");
        $this->db->from('tbl_customer_registration');
        $this->db->where('tbl_customer_registration.cus_email', $email);
        $query = $this->db->get();
        echo '<pre>';
        print_r($query->result()) ;

}

It return the following result for the above code

Array
(
    [0] => stdClass Object
        (
            [cus_mobile] => 0716352642
        )

)

In the contoller I use foreach

foreach($result as $row) :
echo $row['cus_mobile'];

to get [cus_mobile] out of the above array but it gives me all [cus_mobile] rows in the tbl_customer_registration.

How do I get the only [cus_mobile] => 0716352642 out of it.

Upvotes: 0

Views: 88

Answers (3)

Sagar Ch
Sagar Ch

Reputation: 13

    function order_alert($email){

    $this->db->select("tbl_customer_registration.cus_mobile");
    $this->db->from('tbl_customer_registration');
    $this->db->where('tbl_customer_registration.cus_email', $email);
    $query = $this->db->get();
   if($query->num_rows()==1)
    $result_row = $query->row();
    return $result_row;
    else 
    return false;

}

    foreach($result_row as $row) {
    echo $row->cus_mobile;
    }

This will work for if result has one row... now to need to handle the case for greater than one row.Thanks

Upvotes: 0

Adriano Rosa
Adriano Rosa

Reputation: 8761

If I got well your question, what your doing is retrieving data as array of objects and trying to access its data as pure array. To do so as you mean you might retrieve data result as an array there is method called result_array() you might use it instead $query->result().

echo '<pre>';
print_r($query->result_array()) ;

foreach($result as $key => $row) :
echo $row['cus_mobile'];

Upvotes: 0

Gil
Gil

Reputation: 184

$row->cus_mobile

since you have an array of objects.. not really sure if i got your question

(UPDATED) Try this..

foreach($query->result() as $row) {
   echo $row->cus_mobile;
}

Upvotes: 1

Related Questions