将多个表值放入数组中

时间:2012-03-09 22:45:06

标签: php database arrays codeigniter foreach

所以我试图遍历通过id连接的表的所有值。涉及4个表,我试图根据第一个表的id获取每个表的所有值。这是我到目前为止的代码。 (P.S我正在使用CodeIgniter的数据库方法)

    $course = $this->db->query("SELECT * FROM courses");

    // Put Courses into array
    foreach ($course->result() as $row)
    {
        $courses['id'] = $row->id;
        $courses['course_name'] = $row->course_name;

        // Put Topics into array
        $topic = $this->db->query("SELECT * FROM course_topics WHERE course_id = 5");
        foreach ($topic->result() as $row)
        {
            $topics[] = array(
                                'id'            => $row->id,
                                'course_id'     => $row->course_id,
                                'topic_name'    => $row->topic_name,
                                'order'         => $row->order
                                );

            // Put badges into array
            $badge = $this->db->query("SELECT * FROM course_topic_badges WHERE topic_id = ".$row->id);
            foreach ($badge->result() as $row)
            {
                $badges[] = array(
                                    'id'            => $row->id,
                                    'topic_id'      => $row->topic_id,
                                    'badge_name'    => $row->badge_name
                                    );

                // Put dotpoints into array
                $dotpoint = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$row->id);
                foreach ($dotpoint->result() as $row)
                {
                    $dotpoints[] = array(
                                        'id'            => $row->id,
                                        'badge_id'      => $row->badge_id,
                                        'dotpoint'      => $row->dotpoint,
                                        'viddler_video_id' => $row->viddler_video_id,
                                        'viddler_openurl' => $row->viddler_openurl,
                                        'order'         => $row->order
                                        );                  
                }
            }
        }
    }

我一直在摆弄一段时间,所以可能会有一些线路没有以最好的方式完成:)

所以基本上我需要稍后遍历数组,但我只是不确定实际填充它的最佳方法。

感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

在我看来,您的代码正在做一些您可能不希望它的替换。至少用以下几行:

$courses['id'] = $row->id;
$courses['course_name'] = $row->course_name;

如果您对使用(呈现)按实体(表格)分隔的数据感兴趣,那么我建议您进行的唯一更改是将这两行(上图)更改为以下内容:

$courses[] = array('id'=>$row->id, 'course_name'=>$row->course_name);

但是,我注意到您在每个子实体中都包含了父ID,这使我得出结论,您希望以反映关系层次结构的方式使用/呈现数据。如果是这种情况,我建议您采取以下措施:

$query = $this->db->query('SELECT * FROM courses');
$courses = $query->result_array();

// Put Courses into array
foreach ($courses as &$c)
{   
  // Put Topics into array
  $query = $this->db->query('SELECT id, topic_name, `order` FROM course_topics WHERE course_id = '.$c['id']);
  $topics = $query->result_array();
  $c['topics'] = $topics;
  foreach ($topics as $ti=>$t)
  {   
    $query = $this->db->query('SELECT id, badge_name FROM course_topic_badges WHERE topic_id = '.$t['id']);
    $badges = $query->result_array();
    $c['topics'][$ti]['badges'] = $badges;
    foreach ($badges as $bi=>$b)
    {   
      $query = $this->db->query('SELECT id, dotpoint, viddler_video_id, viddler_openurl, `order` FROM course_topic_dotpoints WHERE badge_id = '.$b['id']);
      $dotpoints = $query->result_array();
      $c['topics'][$ti]['badges'][$bi]['dotpoints'] = $dotpoints;
    }   
  }   
}

这构建了一个相当大的关联数组,但如果您真的需要所有数据并且关系层次结构对您很重要,那么它是一个很好的数据结构。当然最好将你构建的内容减少到你将使用的内容。

编辑:

这是您可以如何从数组中迭代/提取信息。此代码将位于视图文件中。

<h2>Courses:</h2>
<?php
  foreach($courses as $c) 
  {
    echo '<h3>'.$c['course_name'].' ('.$c['id'].")</h3>\n";
    echo '<ul>';
    foreach($c['topics'] as $t) 
    {   
      echo '<li>'.$t['topic_name'].' ('.$t['id'].")</li>\n";
      echo '<ul>';
      foreach($t['badges'] as $b) 
      {   
        echo '<li>'.$b['badge_name'].' ('.$b['id'].")</li>\n";
        echo '<ul>';
        foreach($b['dotpoints'] as $dp)
        {   
          echo '<li>'.$dp['dotpoint'].' viddler: '.$dp['viddler_video_id'].' viddler_url: '.$dp['viddler_openurl']."</li><br/>";
        }
        echo '</ul>';
      }     
      echo '</ul>';
    }
    echo '</ul>';

  }
?>