问题描述:

Ok i have one query in codeigniter where i select all data

function get_story()

{

$this->db->select('*');

$this->db->from('stories');

$query = $this->db->get();

return $query;

}

then i make another query with data from another table

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;

}

Then i am trying to create table with data from two queries. Making one query with another JOIN its not option. I created 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>';

}

In td tag where is ???? i need results from second query, i have no idea how to insert data from second query into foreach??? Does somebody have idea, helpe is really needed?

网友答案:

If I understand your question, I think you just need to use string concatenation. Prepare your HTML output into a variable and print it later.

$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;
相关阅读:
Top