首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >一个查询中的变量进入视图中的另一个查询

一个查询中的变量进入视图中的另一个查询
EN

Stack Overflow用户
提问于 2010-06-03 08:16:23
回答 1查看 110关注 0票数 1

我有两个foreach语句。一个变量不知何故进入了另一个变量,我不确定如何修复它。

这是我的控制器:

代码语言:javascript
运行
复制
// 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)函数从数据库中拉取业务。

下面是该函数:

代码语言:javascript
运行
复制
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();
}

下面是视图:

代码语言:javascript
运行
复制
<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>, &nbsp;
            <?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。

我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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');

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2962530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档