2013-03-08 31 views
0

我想使用CodeIgniter從數據庫檢索數據。查詢工作正常,並將數據檢索到一個數組,可以用print_array()顯示數據。使用codeigniter從數據庫中檢索數據

Array 
(
[0] => Array 
    (
     [trans_id] => 33 
     [CustomerAccountNumber] => BR002 
     [TransactionType] => Invoice 
     [TransactionDate] => 2012-09-06 00:00:00 
     [InvoiceNo] => 00001732262 
     [OrderNo] => 0000183946 
     [GoodsValueInAccountCurrency] => 1055.26 
     [AccountBalance] => 1104.52 
     [SYSTraderTranTypeID] => 4 
    ) 

[1] => Array 
    (
     [trans_id] => 34 
     [CustomerAccountNumber] => BR002 
     [TransactionType] => Invoice 
     [TransactionDate] => 2012-09-19 00:00:00 
     [InvoiceNo] => 00001375022 
     [OrderNo] => 0000184907 
     [GoodsValueInAccountCurrency] => 49.26 
     [AccountBalance] => 1104.52 
     [SYSTraderTranTypeID] => 4 
    ) 

但每當我試圖在表中顯示的數據來生成PDF,它給了我一個錯誤,指出「試圖獲得非對象的屬性」。但是所有的對象都進入了一個數組來顯示。這是我的代碼:

print_array($data['data']); 
     { foreach ($data['data'] as $key=>$link) 
     { 
       { 
       $html .= ' 
       <tr> 
        <td width = "100">'.$link->InvoiceNo.'</td> 
        <td width = "300">'.($link->OrderNo).'</td> 
        <td width = "100">'.($link->TransactionDate).'</td> 
        <td width = "100">'.($link->TransactionType).'</td> 
        <td width = "100">'.($link->GoodsValueInAccountCurrency).'</td>  
       </tr>'; 
       } 



     }} 

但是這給了我錯誤「試圖獲得非對象的屬性」。我沒有得到任何線索。爲什麼?請幫忙。

+0

Array [(trans_id] => 34 [CustomerAccountNumber] => BAR004 [TransactionType] =>發票[TransactionDate] => 2012-09-19 00:00:00 [InvoiceNo] => 0000175022 [OrderNo] => 0000184907 [GoodsValueInAccountCurrency] => 49.26 [AccountBalance] => 1104.52 [SYSTraderTranTypeID] => 4) – BiL 2013-03-08 11:55:32

+1

嘗試使用$ link [「key」]而不是$ link-> key – deadlock 2013-03-08 11:56:13

+0

您可能正在使用CI中的result_array()函數來獲取數組。你可以使用result()來獲取結果數組作爲對象。 – Prashank 2013-03-08 12:03:46

回答

1

你有一個數組,你試圖訪問一個對象的屬性。您應該使用這種語法來構建表:

$html .= ' 
    <tr> 
     <td width = "100">'.$link['InvoiceNo'].'</td> 
     <td width = "300">'.($link['OrderNo']).'</td> 
     <td width = "100">'.($link['TransactionDate']).'</td> 
     <td width = "100">'.($link['TransactionType']).'</td> 
     <td width = "100">'.($link['GoodsValueInAccountCurrency']).'</td>  
    </tr>'; 

$link->InvoiceNumber是訪問一個object屬性的語法。 $link['InvoiceNumber']是訪問array元素的語法。

+0

@ mcryan.That一個工作。感謝這個 – BiL 2013-03-08 11:59:57