将值从 2 个表传递到视图 Codeigniter
我有一个功能可以显示买入和卖出列表。我试图在一页中显示这两个内容。 我从两个不同的表中获取值,我想将这两个值传递到同一个模板中,
有人可以建议如何操作吗?
控制器
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在同一个函数中使用 2 个 return 语句,因为每当遇到第一个语句时......好吧,函数就会返回并在那里停止。尝试返回包含 2 个结果的单个数组,例如:
Model:
Controller:
在您看来,该数组的调用方式应类似于
$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:
Controller:
In your view, this array should be called like
$lists['res1']
adn$lists['res2']