在codeigniter中打印列数(*)

时间:2014-08-29 12:58:11

标签: codeigniter codeigniter-2 codeigniter-datamapper

我从这样的数据库中获取数据。

select categories.categorie_name, count(*)
from names
join categories on names.categories_id=categories.categories_id
group by categories.categorie_name
order by 2

我得到的结果是2列

 categorie_name  |  count(*) 

当我尝试从我的视图中访问count(*)列时,我无法打印列中的内容。

<?php foreach ($categories as $categorie)
        echo $categorie->categorie_name." ".$categorie->count(*)."<br>";
?>

请问您如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

试试这个

select categories.categorie_name, count(*) as cnt
from names
join categories on names.categories_id=categories.categories_id
group by categories.categorie_name
order by 2

然后使用像$categorie->cnt;

这样的cnt

答案 1 :(得分:1)

{}使用大括号count(*),然后您可以正确地从对象访问它。

   <?php 
    foreach ($categories as $categorie){
            echo $categorie->categorie_name." ".$categorie->{'count(*)'}."<br>";
    }
    ?>

另外,只需将{ref count(*)添加为&#39; cat_count&#39;

select categories.categorie_name, count(*) as cat_count
from names
join categories on names.categories_id=categories.categories_id
group by categories.categorie_name
order by 2

现在使用$categorie->cat_count

进行访问