简体   繁体   中英

Count results in joined table and show between () in sidebar

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.

Try to replace

echo '<h3><label class="field">Aantal:</label></h3>', $this->db->count_all_results('bedrijven');

with following

echo '<h3><label class="field">Aantal:</label></h3>', count($results);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM