我有两个foreach语句。一个变量不知何故进入了另一个变量,我不确定如何修复它。
这是我的控制器:
// Categories Page Code
function categories($id)
{
$this->load->model('Business_model');
$data['businessList'] = $this->Business_model->categoryPageList($id);
$data['catList'] = $this->Business_model->categoryList();
$data['featured'] = $this->Business_model->frontPageList();
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$data['page_title'] = 'Welcome To Jerome - Largest Ghost Town in America';
$data['page'] = 'category_view'; // pass the actual view to use as a parameter
$this->load->view('container',$data);
}发生的情况是,类别只会显示某个类别中的业务。
使用categoryPageList($id)函数从数据库中拉取业务。
下面是该函数:
function categoryPageList($id) {
$this->db->select('b.id, b.busname, b.busowner, b.webaddress, p.thumb, v.title, c.catname, s.specname, p.thumb, c.id');
$this->db->from ('business AS b');
$this->db->where('b.category', $id);
$this->db->join('photos AS p', 'p.busid = b.id', 'left');
$this->db->join('video AS v', 'v.busid = b.id', 'left');
$this->db->join('specials AS s', 's.busid = b.id', 'left');
$this->db->join('category As c', 'b.category = c.id', 'left');
$this->db->group_by("b.id");
return $this->db->get();
}下面是视图:
<h2>Welcome to Jerome, Arizona</h2>
<p>Choose the Category of Business you are interested in:<br/> <?php foreach ($catList->result() as $row): ?>
<a href="/site/categories/<?=$row->id?>"><?=$row->catname?></a>,
<?php endforeach; ?></p>
<table id="businessTable" class="tablesorter">
<thead><tr><th>Business Name</th><th>Business Owner</th><th>Web</th><th>Photos</th><th>Videos</th><th>Specials</th></tr></thead>
<?php if(count($businessList) > 0) : foreach ($businessList->result() as $crow): ?>
<tr>
<td><a href="/site/business/<?=$crow->id?>"><?=$crow->busname?></a></td>
<td><?=$crow->busowner?></td>
<td><a href="<?=$crow->webaddress?>">Visit Site</a></td>
<td>
<?php if(isset($crow->thumb)):?>
yes
<?php else:?>
no
<?php endif?>
</td>
<td>
<?php if(isset($crow->title)):?>
yes
<?php else:?>
no
<?php endif?>
</td>
<td>
<?php if(isset($crow->specname)):?>
yes
<?php else:?>
no
<?php endif?>
</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<td colspan="4"><p>No Category Selected</p></td>
<?php endif; ?>
</table>问题出现在这里。<?=$crow->id?>应该显示业务表中的行id。相反,它显示category表的行ID。因此,如果我正在查看/site/categories/6,<?=$crow->id?>将显示6,而它应该显示10 (此时该类别中唯一的企业的行ID。
我该如何解决这个问题呢?
发布于 2010-06-03 08:54:55
categoryPageList中的SELECT子句包含两个id列;这两个列都是商业和category表的id列。因此,您只需要获取业务id。
$this->db->select('b.id, b.busname, b.busowner, b.webaddress, p.thumb, v.title, c.catname, s.specname, p.thumb, c.id');
删除b.id应该可以做到这一点。
$this->db->select('b.busname, b.busowner, b.webaddress, p.thumb, v.title, c.catname, s.specname, p.thumb, c.id');
如果您需要这两个值,请为它们提供别名。
$this->db->select('b.id business_id, b.busname, b.busowner, b.webaddress, p.thumb, v.title, c.catname, s.specname, p.thumb, c.id category_id');
https://stackoverflow.com/questions/2962530
复制相似问题