开发者

pass values from 2 tables to a view codeigniter

im having function that will display both buy and sell listings. Im trying to display both of these in one page. im getting the values from 2 different tables and i wanr to pass both the values into the same template

Can someone please suggest how to do it?

controller

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

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

    $data['heading']开发者_如何学Python='headings/heading_view';
    $data['body']='listbody';
    $data['nav']='right';

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

}

Model

 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;

    }

}

View

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


You cannot use 2 return statements within the same function, since whenever the first is encountered...well, the function returns, and stops there. Try returning a single array with the 2 results instead, such as:

Model:

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

Controller:

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

In your view, this array should be called like $lists['res1'] adn $lists['res2']

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜