将值从 2 个表传递到视图 Codeigniter

发布于 2024-12-08 08:41:44 字数 1555 浏览 3 评论 0原文

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

有人可以建议如何操作吗?

控制器

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

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']='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;?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-12-15 08:41:44

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

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();

在您看来,该数组的调用方式应类似于 $lists['res1 '] adn $lists['res2']

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']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文