I am working on a project, In this project i have to display one category name and subjects which related to particular category. One side is category table and many side was the subject table. I have written a code which join tables and give me some array of data. But i'm stuck at showing that result, here my result i got
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
This type of array.
I have problem with display it within a list
Here is my dummy list which i wish to populating with results data
<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>
Here is my code section in my model class
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;
}
There is easy way to archive this? or give me suggestion to easily figure out this problem here. Thank you.
Try your loop array using this
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>';
}