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条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名