2013-03-08 95 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