PHP 将来自 3 table 关系的数据循环到每个 div 元素中
PHP Looping data from 3 table relation into each div element
我需要帮助。
我想在我的 Codeigniter PHP 页面中显示来自 3 table 关系的 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;?>
以上代码显示 item_name 重复了 sub_category_name。
现在输出=
sub_category1 => category1=>item1
sub_category1 => category1=>item2
sub_category1 => category1=>item3
sub_category2 => category1=>item4
sub_category2 => category1=>item5
sub_category2 => category1=>item6
...
我要获取实现数据回显=
sub_category1 => category1=>item1,item2,item3
sub_category2 => category1=>item4,item5,item6
.....
问题:
如何 show/Looping item_name 每个 sub_category_id ...?
感谢您的帮助。
我假设您的查询结果如下:
Iphone Xr 64 GB|Iphone|Apple
Iphone XS 128 GB|Iphone|Apple
Airpod 2| Headphone|Apple
Airpod Pro| Headphone|Apple
Macbook air 2020 128Gb| laptop|Apple
所以,如果你想按子类别显示分组,你应该在模型或控制器中重组你的数据,而不是在视图中这样做,因为嵌套 php 代码和 html代码很难调试和重用。结构如下:
[Note]
:因为你查询的输出是stdclass,所以在使用我的代码之前将其转换为数组:
$arr = json_decode(json_encode($yourObject), TRUE);
['Iphone'=>
[
'category'=>'Apple',
'items'=> ['Iphone Xr 64 GB','Iphone XS 128 GB']
]
]
我有一些代码供您按 sub_category
对列表进行分组
$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'],
],
...
];
视图应该是:
<?php foreach($new_arr as $key => $value)?>
<div class="card">
<div class="card-header">
<h3 class=""><?php echo $key;?></h3>
<h5 class=""><?php echo $value[0]['category'];?></h5>
</div>
<div class="card-footer">
<ul>
<?php foreach($value as $item) ?>
<li class="nav-item">
<a href="#" class="nav-link">
<?php echo $item['item']?>
</a>
</li>
<?php endforeach;?>
</ul>
</div>
</div>
<?php endforeach;?>
我需要帮助。 我想在我的 Codeigniter PHP 页面中显示来自 3 table 关系的 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;?>
以上代码显示 item_name 重复了 sub_category_name。 现在输出=
sub_category1 => category1=>item1
sub_category1 => category1=>item2
sub_category1 => category1=>item3
sub_category2 => category1=>item4
sub_category2 => category1=>item5
sub_category2 => category1=>item6
...
我要获取实现数据回显=
sub_category1 => category1=>item1,item2,item3
sub_category2 => category1=>item4,item5,item6
.....
问题: 如何 show/Looping item_name 每个 sub_category_id ...?
感谢您的帮助。
我假设您的查询结果如下:
Iphone Xr 64 GB|Iphone|Apple
Iphone XS 128 GB|Iphone|Apple
Airpod 2| Headphone|Apple
Airpod Pro| Headphone|Apple
Macbook air 2020 128Gb| laptop|Apple
所以,如果你想按子类别显示分组,你应该在模型或控制器中重组你的数据,而不是在视图中这样做,因为嵌套 php 代码和 html代码很难调试和重用。结构如下:
[Note]
:因为你查询的输出是stdclass,所以在使用我的代码之前将其转换为数组:
$arr = json_decode(json_encode($yourObject), TRUE);
['Iphone'=>
[
'category'=>'Apple',
'items'=> ['Iphone Xr 64 GB','Iphone XS 128 GB']
]
]
我有一些代码供您按 sub_category
对列表进行分组$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'],
],
...
];
视图应该是:
<?php foreach($new_arr as $key => $value)?>
<div class="card">
<div class="card-header">
<h3 class=""><?php echo $key;?></h3>
<h5 class=""><?php echo $value[0]['category'];?></h5>
</div>
<div class="card-footer">
<ul>
<?php foreach($value as $item) ?>
<li class="nav-item">
<a href="#" class="nav-link">
<?php echo $item['item']?>
</a>
</li>
<?php endforeach;?>
</ul>
</div>
</div>
<?php endforeach;?>