2014-07-03 63 views
0

這是我的模塊訪問陣列變量

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()) ; 

} 

它上面的代碼

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

) 

返回以下結果在位指示我使用的foreach

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

得到[cus_mobile]出於上述陣列,但它給我所有0123在tbl_customer_registration中有行。

如何從中獲得唯一的[cus_mobile] => 0716352642

回答

1
$row->cus_mobile 

因爲你有對象的數組..真的不知道,如果我得到你的問題

(修訂版) 試試這個..

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

感謝我試過的情況下,那但是相同的結果。在foreach裏面有 – user3766509

+0

嗎? – Gil

0

如果我得到你的好問題是,你所做的是以對象數組的形式檢索數據,並嘗試以純數組的形式訪問其數據。如果你這樣做,你可能會以數組方式檢索數據結果,可以使用名爲result_array()的方法,而不是$query->result()

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

foreach($result as $key => $row) : 
echo $row['cus_mobile']; 
0
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; 
    } 

這將爲如果結果有一排工作...我們需要處理大於一個row.Thanks