0
votes

I am trying to retrieve data from database using CodeIgniter. The query works fine and retrieves data into an array which can be shown with print_array().

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
    )

)

But whenever I try to display data within a table to generate pdf,it gives me an error stating "Trying to get property of non-object". But all the object are coming into an array to display. Here is my code:

    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>';
                }



        }}

But this is giving me errors "Trying to get property of non-object". I am not getting any clue. Why? Please help.

1
Array ( [trans_id] => 34 [CustomerAccountNumber] => BAR004 [TransactionType] => Invoice [TransactionDate] => 2012-09-19 00:00:00 [InvoiceNo] => 0000175022 [OrderNo] => 0000184907 [GoodsValueInAccountCurrency] => 49.26 [AccountBalance] => 1104.52 [SYSTraderTranTypeID] => 4 ) - BiL
Try $link["key"] instead of $link->key - deadlock
You are probably using result_array() function in CI to get the array. You can use result() to get the result array as object. - Prashank
@Prashank.Thanks.you are right.I have sorted this one.Ta - BiL

1 Answers

1
votes

You have an array and you are trying to access the property of an object. You should be using this syntax to build your table:

$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 is the syntax to access an object property. $link['InvoiceNumber'] is the syntax to access an array element.