我从模型返回2个结果查询到视图。所以,在控制器中我只是通过print_r($ 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 ) ) ) ) )
模型
$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);
}
控制器
$data['value'] = $this->input->post('newparam');
$data['products']=$this->Search_model->getItems($data['value'],$location);
print_r( $data);
查看
<?php foreach ($query2 as $product){ ?>
<tr>
<td><?php echo $product->product_name?></td>
</tr>
<?php } ?></table>
答案 0 :(得分:1)
您必须将数据传递到您的视图,如下所示
/* This is in your controller */
$data['value'] = $this->input->post('newparam');
$data['products']=$this->Search_model->getItems($data['value'],$location);
/* say your view file is myview.php then */
$this->load->view('myview', $data);
在你查看(myview.php)中,你得到两个变量
<?php print_r($products);?>
<?php print_r($value);?>
并迭代如下
<table>
<?php foreach ($products[0]['query2'] as $product): ?>
<tr>
<td><?php echo $product->product_name?></td>
</tr>
<?php endforeach;?>
</table>
答案 1 :(得分:1)
像这样在控制器中传递数据
$this->load->view('myview', $data);
在视图中尝试此操作
<?php
foreach ($products as $prod){
$query1 = $prod['query1'];
$query2 = $prod['query2'];
}
foreach($query2 as $product){?>
<tr>
<td><?php echo $product->product_name?></td>
</tr>
<?php
}
?>
答案 2 :(得分:1)
假设您的$products
变量具有以下数据结构:
[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 )
)
)
)
然后循环遍历query2
数组中的数据,你可以这样做:
<?php foreach($products[0]['query2'] as $product) { ?>
<tr>
<td><?php echo $product->product_name;?></td>
</tr>
<?php } ?>
答案 3 :(得分:0)
将数据从控制器传递到视图,然后在视图中循环
CONTROLLER
function index(){
$data['value'] = $this->input->post('newparam');
$data['products']=$this->Search_model->getItems($data['value'],$location);
$this->load->view("pages/extras/php/content", $data);
}
查看
<?php
echo $value;
foreach ($products as $key => $value) {
//do whatever you want
}
?>
有关更多示例,请参阅https://www.codeigniter.com/user_guide/general/views.html