doumi5223 2017-10-24 09:50 采纳率: 100%
浏览 28
已采纳

如何在codeigniter视图中循环此输出

I am returning 2 result query from model to view. so, in controller i am just printing the this array by print_r($data) function. i dont know how to loop this result in my view

this is the result i am getting in the controller from the model. so how can i loop through the data.

Array ( [value] => Grocery 
[products] => Array ( [0] => Array ( [query1] => Array ( [0] => stdClass Object ( [parent_category] => Beverages ) [1] => stdClass Object ( [parent_category] => Household ) ) [query2] => Array ( [0] => stdClass Object ( [product_name] => Britannia Cake - Fruity Fun ) [1] => stdClass Object ( [product_name] => Tetley Green Tea - Long Leaf ) [2] => stdClass Object ( [product_name] => Tetley Green Tea - Lemon ) [3] => stdClass Object ( [product_name] => AVT Gold Cup Premium Dust Tea ) [4] => stdClass Object ( [product_name] => Brooke Bond Red Label Tea ) [5] => stdClass Object ( [product_name] => sd sdsds s ) [6] => stdClass Object ( [product_name] => sds dfcs ) [7] => stdClass Object ( [product_name] => fhgg ) [8] => stdClass Object ( [product_name] => hearts ) [9] => stdClass Object ( [product_name] => banana ) [10] => stdClass Object ( [product_name] => hearts ) ) ) ) )

Model

 $query3=$this->db->select('product_name')
                      ->from('materials')
                      ->where('category',$value)
                      ->get();
                      $data=array();
                      if($query3->num_rows() > 0)
                      {
                          //$data['category'] = $this->test($value,$location);
                            $query4=$this->db->select('parent_category')
                          ->distinct()
                          ->from('materials')
                         ->where('category',$value)
                         ->where('warehouse_id',$location)
                         ->group_by('parent_category')
                                               ->get();
                          if($query4->num_rows() > 0)
                          {

                            $data['query1']= $query4->result();

                          }
                            $data['query2']=$query3->result();
                          //print_r($data);

                          return $data=array($data);
                      }

Controller

$data['value'] = $this->input->post('newparam');
$data['products']=$this->Search_model->getItems($data['value'],$location);
print_r(  $data);

View

<?php foreach ($query2 as $product){ ?>


        <tr>
                <td><?php echo $product->product_name?></td>

        </tr>


<?php } ?></table>
  • 写回答

4条回答 默认 最新

  • dongtanxi5676756 2017-10-24 10:15
    关注

    Assuming your $products variable has the following data structure :

    [products] => Array ( 
                [0] => Array ( 
                        [query1] => Array ( 
                                [0] => stdClass Object ( [parent_category] => Beverages ) 
                                [1] => stdClass Object ( [parent_category] => Household ) ) 
                        [query2] => Array ( 
                                [0] => stdClass Object ( [product_name] => Britannia Cake - Fruity Fun ) 
                                [1] => stdClass Object ( [product_name] => Tetley Green Tea - Long Leaf ) 
                                [2] => stdClass Object ( [product_name] => Tetley Green Tea - Lemon ) 
                                [3] => stdClass Object ( [product_name] => AVT Gold Cup Premium Dust Tea ) 
                                [4] => stdClass Object ( [product_name] => Brooke Bond Red Label Tea ) 
                                [5] => stdClass Object ( [product_name] => sd sdsds s ) 
                                [6] => stdClass Object ( [product_name] => sds dfcs ) 
                                [7] => stdClass Object ( [product_name] => fhgg ) 
                                [8] => stdClass Object ( [product_name] => hearts ) 
                                [9] => stdClass Object ( [product_name] => banana ) 
                                [10] => stdClass Object ( [product_name] => hearts ) 
                        ) 
                ) 
            ) 
    

    Then to loop through the data in the query2 array, you could do :

    <?php foreach($products[0]['query2'] as $product) { ?>
        <tr>
            <td><?php echo $product->product_name;?></td>
        </tr>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败