简体   繁体   English

需要帮助遍历查询结果

[英]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 data传递到视图

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; 包含一个foreach ,它遍历变量$data['orders'] = $row->cart;包含的array $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. 在数据库中,我正在按cust_id查找cust_orders表。客户可能有多于1行,这意味着有多个订单。

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 . 我需要遍历cust_id = =到$session_id所有行。

I feel the problem is in my MODEL because if I change $data['id'] = $row->cust_id; 我觉得问题出在我的MODEL因为如果我更改$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 echo $row->cust_id;' I am able to see all of the rows which match the customers id, but it breaks my foreach` code. echo $row->cust_id;' I am able to see all of the rows which match the customers id, but it breaks my foreach`代码。

How do I properly assign the values returned in the MODEL query so that all cust_orders == to $session_id are displayed? 如何正确分配在MODEL查询中返回的values ,以便显示所有cust_orders ==到$session_id

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

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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