Foreach结果与连接表

时间:2014-10-25 11:52:52

标签: php mysql

好的我在使用codeIgniter这是我的查询

function get_stories()
{  

   $this->db->from('stories');
   $this->db->join('stories_to_categories', 'stories.id = stories_to_categories.story');
   $this->db->join('categories', 'stories_to_categories.category = categories.id');
   $query = $this->db->get();
   return $query;
}

我收到了结果

Array
(
    [0] => stdClass Object
    (
        [id] => 1
        [title_english] => 1
        [title_srpski] => 
        [subtitle_english] => 
        [subtitle_srpski] => 
        [logo] => 0
        [gallery] => 0
        [story_english] => 1
        [story_srpski] => 
        [uri_english] => 1
        [uri_srpski] => 
        [story] => 1
        [category] => 1
        [parent] => 0
        [name_english] => strategy
    )

    [1] => stdClass Object
    (
        [id] => 2
        [title_english] => 1
        [title_srpski] => 
        [subtitle_english] => 
        [subtitle_srpski] => 
        [logo] => 0
        [gallery] => 0
        [story_english] => 1
        [story_srpski] => 
        [uri_english] => 1
        [uri_srpski] => 
        [story] => 1
        [category] => 2
        [parent] => 0
        [name_english] => external
    )

    [2] => stdClass Object
    (
        [id] => 3
        [title_english] => 1
        [title_srpski] => 
        [subtitle_english] => 
        [subtitle_srpski] => 
        [logo] => 0
        [gallery] => 0
        [story_english] => 1
        [story_srpski] => 
        [uri_english] => 1
        [uri_srpski] => 
        [story] => 1
        [category] => 3
        [parent] => 0
        [name_english] => internal
    )

    [3] => stdClass Object
    (
        [id] => 4
        [title_english] => 1
        [title_srpski] => 
        [subtitle_english] => 
        [subtitle_srpski] => 
        [logo] => 0
        [gallery] => 0
        [story_english] => 1
        [story_srpski] => 
        [uri_english] => 1
        [uri_srpski] => 
        [story] => 1
        [category] => 4
        [parent] => 0
        [name_english] => stakeholder
    )

    [4] => stdClass Object
    (
        [id] => 5
        [title_english] => 1
        [title_srpski] => 
        [subtitle_english] => 
        [subtitle_srpski] => 
        [logo] => 0
        [gallery] => 0
        [story_english] => 1
        [story_srpski] => 
        [uri_english] => 1
        [uri_srpski] => 
        [story] => 1
        [category] => 5
        [parent] => 0
        [name_english] => crisis
    )

)

我正在打印

foreach($story->result() as $row)
{
    echo '
        <tr>
            <td>' . $row->title_english . '</td>
            <td>' . $row->name_english . ',<td>
    ';
}

在我的桌子上我有

1   strategy,   
1   external,   
1   internal,   
1   stakeholder,    
1   crisis,

我尝试实现

1战略,外部,内部,利益相关者,危机, 请有人帮我这个吗?

2 个答案:

答案 0 :(得分:0)

我会使用类似的东西

$results = array();

foreach($story->result() as $row){
  if(!isset($results[$row->title_english]))
    $results[$row->title_english] = array();      
  $results[$row->title_english][] = $row->name_english;    
}

foreach($results as $title_english => $names){
   echo '<tr><td>' . $title_english . '</td><td>' . implode(',',$names) . '</td><tr>';
}

此代码创建另一个(实际上是2维)数组,其中包含由$row->title_english值给出的索引的子数组。遍历初始阵列并填充新阵列后,您可以轻松地以所需格式输出vaules。

答案 1 :(得分:-1)

尝试在你的foreach中做到这一点:

foreach( $story->result() as $row )
{
    echo '
        <tr>
            <td>' . $row->title_english . '</td>
            <td>' . $row->name_english . ',</td>
        </tr>
    ';
}