将值从2个表传递给视图codeigniter

时间:2011-10-02 19:42:13

标签: php codeigniter codeigniter-2

我的功能将显示买入和卖出列表。我试图在一个页面中显示这两个。 我从2个不同的表中获取值,我想将这两个值传递到同一个模板

有人可以建议怎么做吗?

控制器

function leads(){
    $this->load->model('listings');

    $data['mylists']=$this->member_functions->mine();
    $data['mylists2']=$this->member_functions->mine();

    $data['heading']='headings/heading_view';
    $data['body']='listbody';
    $data['nav']='right';

    $this->load->view('includes/template',$data);

}

模型

 function mine(){

    $mylists=$this->db->get('buy');

    if ($mylists->num_rows()>0){

        foreach ($mylists->result() as $a)
        {

            $data[]=$a;

        }
        return $data;
    }

    $mylists2=$this->db->get('sell');

    if ($mylists2->num_rows>0)
    {
        foreach ($mylists->result() as $b)
        {
            $data[]=$b;

        }

        return $data;

    }

}

查看

  <h2>Buy leads</h2>
                 <?php foreach ($mylists as $mylist):?>
         <p><?php echo "$mylist->type1 in $mylist->country as $mylist->buyid" ?></p>

       <?php endforeach;?>
            </div>
        <br />
            <h2>Sell leads</h2>
            <?php foreach ($mylists2 as $mylist2):?>
        <p><?php echo "$mylist2->type1 in $mylist2->country" ?></p>

       <?php endforeach;?>

1 个答案:

答案 0 :(得分:1)

你不能在同一个函数中使用2个return语句,因为每当遇到第一个语句时......函数返回并停在那里。尝试使用2个结果返回单个数组,例如:

型号:

function mine(){

    $mylists=$this->db->get('buy');

    if ($mylists->num_rows()>0){

        foreach ($mylists->result() as $a)
        {
            $data['res1'][]=$a;
        }
    }

    $mylists2=$this->db->get('sell');

    if ($mylists2->num_rows>0)
    {
        foreach ($mylists->result() as $b)
        {
            $data['res2'][]=$b;
        }
    }
  return $data;
}

<强>控制器:

$data['lists']=$this->member_functions->mine();

在您看来,应该像$lists['res1'] adn $lists['res2']

一样调用此数组