dongwei7048 2013-04-03 12:29 采纳率: 100%
浏览 46
已采纳

计数结果在连接表中并显示在侧栏中的()之间

I have a sidebar with categories. as you can see here: http://kees.een-site-bouwen.nl/

The id of the category in within the url. with an uri segment(3) When you click on one of the categories for example webdesign. some factories are shown. these factories have a join with the categories. so when the category id = 11, the factories with category id=11 are shown.

but now i want to show how much factories are in that category. the output will show up between the

() here: http://kees.een-site-bouwen.nl/

So my actual question is: how do i count_all_results() so it only shows me the factories who are located in the category?

What i've tried.

<?php
if (isset($results) && is_array($results)) { 
foreach ($results as $key => $row) { 
$id = $row->idcategorieen; 
$segment3 = $this->uri->segment(3); 
if ($id == $segment3) { 

echo '<h2>Categorie:', '&nbsp;' , $row->Categorie;  
echo '<h2>';
echo br(1);
echo '<p class="field">';
echo '<h3><label class="field">Aantal:</label></h3>', $this->db->count_all_results('bedrijven');
echo '</p>';
echo br(1); 
break;
} 
}
}
?>
<br />
<h4>Bedrijven</h4>
<hr>
<br />
<?php foreach($results as $item):?>

<p>Bedrijfsnaam: <a href="<?php echo base_url()?>home/showindividueel/<?php echo $item->idbedrijven?>">
<?= $item->Bedrijfsnaam   ?></a><br /></p> 
 </tr>
 <?php endforeach;?>

this shows me all of the factories in my database. but i only need the ones in the specific category.

Sidebar code:

        echo '<ul>';
        if(isset($cat) && is_array($cat)){
            foreach($links as $k => $value)
                {
                    echo '<li>';
                    echo '<br>';
                    echo '<a href="'.base_url().'home/categorie/'.$value->idcategorieen.' ">' .$value->Categorie. '</a>';
                    echo nbs(1);
                    echo '(';
                    echo ')';
                    echo '</li>';
                }
            }
        echo '<ul>';

Thanks.

  • 写回答

2条回答 默认 最新

  • dpw43061 2013-05-01 09:58
    关注

    I fixed my problem using a new function in my controller.

    My controller:

    $data['counts'] = $this->bedrijven_model->bedrijf_categorie();
    

    My model:

    function bedrijf_categorie()
    {
    
        $sql = "
        SELECT count(b.idbedrijven) as ItemCount, c.Categorie as Categorie
        FROM `bedrijven` b
        INNER JOIN `bedrijfcategorieen` bc
        ON bc.idbedrijven = b.idbedrijven
        INNER JOIN `categorieen` c
        ON c.idcategorieen = bc.idcategorieen
        GROUP BY c.idcategorieen
        ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    

    my views:

        <?php   
        echo '<ul>';
                foreach($counts as $count){
    
                        echo '<li>';
                        echo '<a href="'.base_url().'home/categorieen/'.$count['Categorie'].' ">' .$count['Categorie']. '</a>';
                        echo nbs(1);
                        echo '(';
                        echo $count['ItemCount'];
                        echo ')';
                        echo '</li>';
                    }
        echo '</ul>';
        ?>
    

    Anyway. thanks people who helped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c