doumu1873 2018-09-29 14:44
浏览 43

Codeigniter一对多关系数据显示

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.

  • 写回答

1条回答 默认 最新

  • drw85135 2018-09-29 15:57
    关注

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类