Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将数据从3个表关系循环到每个div元素_Php_Loops_Codeigniter - Fatal编程技术网

PHP将数据从3个表关系循环到每个div元素

PHP将数据从3个表关系循环到每个div元素,php,loops,codeigniter,Php,Loops,Codeigniter,我需要帮助。 我想在我的Codeigniter PHP页面中从3个表关系中显示div元素: my Table is= Category =>category_id, category_name Sub_Category => sub_category_id,category_id, sub_category_name Item => item_id, sub_category_id,item_name 模型= public function i

我需要帮助。 我想在我的Codeigniter PHP页面中从3个表关系中显示div元素:

my Table is=

    Category =>category_id, category_name 
    Sub_Category => sub_category_id,category_id, sub_category_name  
    Item => item_id, sub_category_id,item_name
模型=

public function item_data()
   {
   $query = $this->db->query('SELECT A.item_name, B.sub_category_name, C.category_name FROM 
   item A
   sub_kategori A 
   LEFT JOIN sub_category B ON B.sub_category_id = A.sub_category_id
   LEFT JOIN category C ON C.category_id = B.category_id
   ');
   return $query->result();
   }
控制器:

function item_view()
{
//--------
$item_data = $this->model_app->data_ticket();
$data['item_data'] = $item_data;

$this->load->view('template', $data);
}
视图:

<?php $no = 0; foreach($item_data as $row) : $no++;?>
 <div class="card">           
   <div class="card-header">
     <h3 class=""><?php echo $row->sub_category_name;?></h3>
     <h5 class=""><?php echo $row->category_name;?></h5>
   </div>
   <div class="card-footer">
     <ul>

//... How to show/Looping item_name by sub_category_id ...?

       <li class="nav-item">
         <a href="#" class="nav-link">
           <?php echo $row->item_name;?>
         </a>
       </li>                                          
     </ul>
   </div>
 </div>
<?php endforeach;?>
我想得到数据回音=

sub_category1 => category1=>item1,item2,item3
sub_category2 => category1=>item4,item5,item6
.....
问题: 如何按子类别id显示/循环项目名称


谢谢您的帮助。

我假设您的查询结果如下:

Iphone Xr 64 GB | Iphone | Apple

iphonexs128gb | Iphone | Apple

Airpod 2 |耳机|苹果

Airpod Pro |耳机|苹果

MacBookAir 2020 128Gb笔记本电脑|苹果

因此,如果您想按子类别显示组,您应该在模型或控制器中重新构造数据,而不要在视图中进行,因为嵌套php代码和html代码很难调试和重用。对于这样的结构:

[注意]
:因为查询的输出是stdclass,所以在使用我的代码之前,让我们先将其转换为数组:

$arr=json\u decode(json\u encode($yourObject),TRUE)

我有一些代码供您按子类别对列表进行分组

$arr = [
    ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XR'],
    ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XS'],
    ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone X'],
    ['category'=>'Apple','sub_category'=>'HeadPhone','item'=>'AirPod 2'],
    ['category'=>'Apple','sub_category'=>'Laptop','item'=>'Macbook air'],
    ['category'=>'Samsung','sub_category'=>'MobilePhone','item'=>'Galaxy S10'],
];
$newArr = [];
foreach ($arr as $key => $item) {
    $newArr[$item['sub_category']][$key] = $item;
}
结果将是:

$newArr = [
    'Iphone'=>[
        ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XR'],
        ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XS'],
        ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone X'],
    ],
    ...
];
意见应该是:



因此,您有三个表,具有关系:一个项目有一个子类别是一个类别的子类别。您想按子类别显示项目组,对吗?@dewokaji请向我们展示您获得的输出和您想要实现的目标???@EricsNguyen,是的right@KUMAR谢谢,我编辑了详细问题,在重新构造数组之后,我在控制器上得到了一些错误=“不能将stdClass类型的对象用作…”中的数组”,所以,将数组替换为stdClass@Erics,非常感谢你的帮助,我非常感激。这稍微修改一下就解决了我的问题
$arr = [
    ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XR'],
    ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XS'],
    ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone X'],
    ['category'=>'Apple','sub_category'=>'HeadPhone','item'=>'AirPod 2'],
    ['category'=>'Apple','sub_category'=>'Laptop','item'=>'Macbook air'],
    ['category'=>'Samsung','sub_category'=>'MobilePhone','item'=>'Galaxy S10'],
];
$newArr = [];
foreach ($arr as $key => $item) {
    $newArr[$item['sub_category']][$key] = $item;
}
$newArr = [
    'Iphone'=>[
        ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XR'],
        ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XS'],
        ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone X'],
    ],
    ...
];