PHP Foreach有两个查询

时间:2014-11-30 13:55:48

标签: php foreach

好的,我在codeigniter中有一个查询,我选择所有数据

function get_story()
{
        $this->db->select('*');
        $this->db->from('stories');
        $query = $this->db->get();
        return $query;
}

然后我用另一个表中的数据进行另一个查询

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

然后我尝试使用来自两个查询的数据创建表。使用另一个JOIN进行一个查询不是选项。我创建了foreach

  foreach($story->result() as $row)
{
    echo '<tr>
    <td>'.$row->title_english.'</td>
    <td>'?????????????????????????????????????????????????????</td>
    <td align="center">     
        <a href="'.site_url('admin/clients/edit_story/'.$row->id ).'"><img src="'.site_url('admin/images/icons/pencil.png').'" class="tooltip" title="'.lang('edit').'" /></a>
        <a href="#delete_'.$row->id.'" rel="facebox"><img src="'.site_url('admin/images/icons/minus-circle.png').'" class="tooltip" title="'.lang('delete').'" /></a>

    </td>

</tr>
    ';

    echo '<div id="delete_'.$row->id.'" style="display: none;">
<h1>'.lang('client_delete').'</h1><br />

    '.lang('story_confirm_delete').'

    <br /><br />
        <a href="'.site_url('admin/clients/delete_story/'.$row->id).'" class="button submit"><span><span>'.lang('yes').'</span></span></a>
        <a href="javascript:CloseFacebox();" class="button cancel"><span><span>'.lang('no').'</span></span></a>
        <br /><br />
</div>';
}

在td标签中哪里是????我需要第二个查询的结果,我不知道如何将数据从第二个查询插入foreach ???有人有想法,真的需要帮助吗?

1 个答案:

答案 0 :(得分:0)

如果我理解您的问题,我认为您只需要使用string concatenation。将HTML输出准备到变量中并稍后打印。

$html = '<table>';
foreach($story->result() as $row)
{
    $html .= '<tr>';
    $html .= '<td>'.$row->title_english.'</td>';
    $html .= '<td>';
    foreach($stories->result() as $row2){
        // display your second query result here
        $html .= '?????????????????????????????????????????????????????';
    }
    $html .= '</td>';
    $html .= '<td align="center">';
    $html .= '<a href="'.site_url('admin/clients/edit_story/'.$row->id ).'"><img src="'.site_url('admin/images/icons/pencil.png').'" class="tooltip" title="'.lang('edit').'" /></a>';
    $html .= '<a href="#delete_'.$row->id.'" rel="facebox"><img src="'.site_url('admin/images/icons/minus-circle.png').'" class="tooltip" title="'.lang('delete').'" /></a>';
    $html .= '</td>';
    $html .= '</tr>';

    $html .= '<div id="delete_'.$row->id.'" style="display: none;">';
    $html .= '<h1>'.lang('client_delete').'</h1><br />';
    $html .= lang('story_confirm_delete');

    $html .= '<br /><br />';
    $html .= '<a href="'.site_url('admin/clients/delete_story/'.$row->id).'" class="button submit"><span><span>'.lang('yes').'</span></span></a>';
    $html .= '<a href="javascript:CloseFacebox();" class="button cancel"><span><span>'.lang('no').'</span></span></a>';
    $html .= '<br /><br />';
    $html .= '</div>';
}
$html .= '</table>';
echo $html;