Codeigniter一对多关系数据显示

时间:2018-09-29 14:44:19

标签: php codeigniter crud

我正在一个项目中,在这个项目中,我必须显示一个类别名称和与特定类别相关的主题。一侧是类别表,而另一侧是主题表。我写了一段代码,将表连接起来并给我一些数据。但是我坚持显示结果,在这里我得到了结果

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;
}

有简单的存档方法吗?或者给我建议,以便在这里轻松解决此问题。谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用此循环数组

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>';
}