我正在做一个项目,在这个项目中,我必须显示一个类别名称和与特定类别相关的主题。一面是类别表,多面是主题表。我编写了一个连接表并给我一些数据数组的代码。但是我坚持显示那个结果,这是我的结果我得到的
Array
(
[id_category] => 9
[cat_name] => OL
[tbl_subject] => Array
(
[0] => Array
(
[id_subject] => 13
[name] => Science
[description] => සාමාන්ය පෙළ විද්යාව
[id_category_fk] => 9
)
[1] => Array
(
[id_subject] => 14
[name] => Agri & Food technology
[description] => කෘෂි හා ආහාර තාක්ෂණය
[id_category_fk] => 9
)
[2] => Array
(
[id_subject] => 31
[name] => Business & Accounting
[description] =>
[id_category_fk] => 9
)
)
)
1
Array
(
[id_category] => 16
[cat_name] => Grade 06
[tbl_subject] => Array
(
[0] => Array
(
[id_subject] => 15
[name] => Science
[description] => විද්යාව
[id_category_fk] => 16
)
[1] => Array
(
[id_subject] => 32
[name] => Sinhala
[description] => සිංහල භාෂාව හා සාහිත්යය
[id_category_fk] => 16
)
[2] => Array
(
[id_subject] => 33
[name] => Geography
[description] => භූගෝල විද්යාව
[id_category_fk] => 16
)
[3] => Array
(
[id_subject] => 34
[name] => Buddhism
[description] => බුද්ධ ධර්මය
[id_category_fk] => 16
)
[4] => Array
(
[id_subject] => 36
[name] => Citizan Education
[description] => පුරවැසි අධ්යාපනය
[id_category_fk] => 16
)
[5] => Array
(
[id_subject] => 52
[name] => Geography English medium
[description] =>
[id_category_fk] => 16
)
)
)
1
这种类型的数组。
我在列表中显示它时遇到问题 这是我的虚拟列表,我希望用结果数据填充它
<div class="col-4 mb-4">
<h3> Category Name</h3>
<ul class="list-group list-group-flush exam-list">
<li class="list-group-item d-flex justify-content-between align-items-center">
Subject name <span class="badge badge-primary badge-pill">14</span></li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Subject name <span class="badge badge-primary badge-pill">14</span></li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Subject name <span class="badge badge-primary badge-pill">14</span></li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Subject name <span class="badge badge-primary badge-pill">14</span></li>
</ul>
</div>
这是我的模型类中的代码部分
public function getAllExamPapers()
{
// get distinct item of category table and join table with subject table
$this->db->distinct();
$this->db->select('tbl_category.id_category,tbl_category.name AS cat_name');
$this->db->join('tbl_subject', 'tbl_category.id_category = tbl_subject.id_category_fk');
$results = $this->db->get('tbl_category')->result_array();
// Loop through the tbl_category array
foreach ($results as $i => $result) {
// Get an array of subject
// 'id_category_fk' is the foreign_key in the tbl_subject table
$this->db->where('id_category_fk', $result['id_category']);
$sub_query = $this->db->get('tbl_subject')->result_array();
// Add the subject array to the array entry for this category
$results[$i]['tbl_subject'] = $sub_query;
}
return $results;
}
有简单的方法可以存档吗?或者给我建议在这里轻松解决这个问题。谢谢。
用这个试试你的循环数组
foreach($results as $result){
echo '<h3>'.$result['cat_name'].'</h3>'
echo '<ul>';
foreach($result['tbl_subject'] as $subject){
echo '<li>'.$subject['name'].'</li>';
}
echo '</ul>';
}