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

时间:2010-06-03 00:16:23

标签: php codeigniter

我有两个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>, &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。相反,它显示了类别表的行ID。所以,如果我正在查看/ site / categories / 6 <?=$crow->id?>将显示6何时应该显示10(此时该类别中唯一商家的行ID。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

categoryPageList 中的SELECT子句包含两个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');