douyue7536
2015-11-11 21:48
浏览 57
已采纳

将查询+额外变量从模型传递到视图

I'm wondering what is the best way to pass a query result + a separate piece of text from a model to a view.

My model

    $query = $this->db->query("SELECT * from ci_wizard_results WHERE `tblresultrequestsource` LIKE '%".$countryqueryresult->countrytld."%' AND tblresultkioskname = '' ".$sqlkioskfilter." ORDER BY ci_wizard_results.tblresultdatetime DESC");
    $querytext = "some text"
    return $query->result();

Controller:

$data['yunbe_selectall'] = $this->wizard_model->yunbe_selectall();

View:

<?php foreach($yunbe_selectall as $yunbe_selectallrow) { ?>
                        <tr>
                                <td><?php echo $yunbe_selectallrow->tblresultrefid;?></td>
                                <td><?php echo $yunbe_selectallrow->tblresultdatetime;?></td>
                                <td><?php echo $yunbe_selectallrow->tblresultip;?></td>
                                <td><?php echo $yunbe_selectallrow->tblresultrequestsource;?></td>

I want to display "$querytext" into the view. I know it would be better to put each return in a different function, but since there are some complicated query's in the function to get to the result, I'd like to avoid to duplicate it.

I tried passing it as an array, but I can't manage to get it displayed.

Thanks!!

</div>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dotaer1993 2015-11-11 21:53
    已采纳

    You are on the right track with an array:

    query = $this->db->query("SELECT * from ci_wizard_results WHERE `tblresultrequestsource` LIKE '%".$countryqueryresult->countrytld."%' AND tblresultkioskname = '' ".$sqlkioskfilter." ORDER BY ci_wizard_results.tblresultdatetime DESC");
    $querytext = "some text"
    return array('text'=>$querytext, 'result'=>$query->result());
    

    Controller:

    $data['yunbe_selectall'] = $this->wizard_model->yunbe_selectall();
    

    View:

    <h2><?php echo $yunbe_selectall['text'];?></h2>
    <?php foreach($yunbe_selectall['result'] as $yunbe_selectallrow) { ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题