简体   繁体   中英

Need help looping through query result

Model

    $data   =   array();
    $session_id = $this->session->userdata('id'); 

    $q = $this->db->get_where('cust_orders', array('cust_id' => $id));


        foreach ($q->result() as $row)
        {
            $data['id'] = $row->cust_id; 
            $data['orders'] = $row->cart; 

        }
            return $data;

Controller

Passes the data to the view

View

<?php foreach($_orders as $key => $item) : ?>
<tr>

    <td>
    <?php 
        $price = $item['price']; #change this to order status 

        switch ($price) 
        {
        case (64); $UPSTrack = '1Z12345E1512345676'; #Shipped add tracking number
              echo 'Shipped <br> <a href="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums='.$UPSTrack.'">Track My Order</a>'; 
              break;

        case (88);
              echo 'Shipped'; 
              break;

        default;
              echo 'Processing'; 
              break;
        }
    ?>

    </td>




    <td>
    <?php foreach ($item['options'] as $option => $value) 
            {
            echo '<span class="option">'.$option . ': </span> <span class="options">' . $value . '</span>';
            } 
    ?>
    </td>


    <td><?php echo $item['id']; ?></td>

    <td><?php echo $item['qty'];  ?></td>

    <td><?php echo number_format($item['price'],2); ?></td>

</tr>    



<?php endforeach; ?>

Options is an ARRAY

Contains a foreach which loops through the array contained in the variable $data['orders'] = $row->cart;

In the database I am looking up the cust_orders table by cust_id A customer may have more than 1 row meaning multiple orders.

The problem is that I am only looping through the last row. I need to loop through all rows where cust_id is = to $session_id .

I feel the problem is in my MODEL because if I change $data['id'] = $row->cust_id; to echo $row->cust_id;' I am able to see all of the rows which match the customers id, but it breaks my echo $row->cust_id;' I am able to see all of the rows which match the customers id, but it breaks my foreach` code.

How do I properly assign the values returned in the MODEL query so that all cust_orders == to $session_id are displayed?

Right now, you are overwrite your array with each customer value, so you need multidimensional array.

It should be like below

EDIT: Change your foreach in your model as below

   foreach ($q->result() as $row)
    {
        $data['id'] = $row->cust_id; 
        $data['orders'][] = $row->cart; 

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM