Question

I wish to display the results of a database query in my view whilst giving each row a unique ID.

My model is:

function get_load_lines($ordernumber)
{
    $this->db->select('product,pricepercube,qty,linecost,linediscount,cubicvolume,bundles,treated,autospread');
    $this->db->from('Customer_Order_Lines');
    $this->db->where('CustomerOrderID',$ordernumber);
    $q = $this->db->get();
    if($q->num_rows() > 0) 
    {
        return $q->row_array(); 
    }
}

My controller is:

function edit_order_lines() 
{
    $data = array(
        'ordernumber' =>$this->input->post('ordernumber'),
        'customer' =>$this->input->post('customer'),
        'loadlines' => $this->sales_model->get_load_lines($this->input->post('ordernumber'))
    );

    $this->load->view('sales/edit_order_lines',$data);
}

As mentioned I want to display each of these rows in my view.

So I use this code in my view:

<table>
<?php
$i=1;
foreach ($loadlines as $row)
{ 
    echo "<tr>";
    echo "<td>".$i."</td>";
    echo "<td>".$row['product']."</td>";
    echo "<td>".$row['pricepercube']."</td>";
    echo "<td>".$row['qty']."</td>";
    echo "</tr>";
    $i++;
}
?>
</table>

This does not have the intended result. $i increments for each array entry, not each array line.

Any advice on the best way to display this data?

So if 3 rows the below is required:

enter image description here

Was it helpful?

Solution

In your model, try returning a result_array, rather than a row_array:

return $q->result_array();

If more than one result exists, result array will return all of the results, whereas row array will only return one. CodeIgniter's user guide explains the differences.

You could also make your for loop at little bit tidier, by incrementing $i like this:

$i=1;
foreach ($loadlines as $row)
{ 
    echo "<tr>";
    echo "<td>".$i++."</td>";
    echo "<td>".$row['product']."</td>";
    echo "<td>".$row['pricepercube']."</td>";
    echo "<td>".$row['qty']."</td>";
    echo "</tr>";
} 

OTHER TIPS

I'm not sure I understand you question clearly, but in order to edit Customer Order Line (order details) you should also pull the ID of each Customer_Order_Lines when you query the database. (assuming your table Customer_Order_Lines has primary key filed called ID).

 $this->db->select('id,product,pricepercube,q...');

Then when you loop through the results, do:

foreach ($loadlines as $row)
           { 
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product']."</td>";
echo "<td>".$row['pricepercube']."</td>";
echo "<td>".$row['qty']."</td>";
echo "</tr>";
$i++;
           }

This will give you the specific unique ID's (primary keys) of each Order Line. Then, you can updated each Order Line by referring to this ID.

Let me know if I misunderstood your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top