Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Output problems with COUNT
#1

[eluser]Unknown[/eluser]
I'm having some problems outputting a COUNT from SQL... My SQL query is as follows:

Code:
$this->db->select('entry_slug,entry_title,entry_body,entry_published,user_name,user_showname,COUNT(comment_id)');
            $this->db->from('blog_entries');
            $this->db->join('user_accounts','user_accounts.user_id=blog_entries.user_id','inner');
            $this->db->join('blog_comments','blog_comments.entry_id=blog_entries.entry_id','left');
            $this->db->group_by('blog_entries.entry_id');

My template looks like so:

Code:
<?= file_get_contents($_SERVER['DOCUMENT_ROOT'] .'header.html'); ?>
<? foreach($Entries->result() as $Entry): ?>
<?
    $Date        = mdate('%Y/%m/%d',mysql_to_unix($Entry->entry_published));
    $Slug        = addslashes(quotemeta($Entry->entry_slug));
    $URLSlug    = 'blog/'. $Date .'/'. $Slug;

    if($Entry->user_showname != null) {
        $Author    = $Entry->user_showname;
    } else {
        $Author    = $Entry->user_name;
    }
?>
    <h1>&lt;?= anchor($URLSlug,$Entry->entry_title); ?&gt;</h1>

    <p class="MetaInformation">
        <strong>
            Published on &lt;?= $Entry->entry_published; ?&gt; by
            &lt;?= anchor('profile/'. $Entry->user_name,$Author); ?&gt;
        </strong>
    </p>

    &lt;?= nl2br($Entry->entry_body); ?&gt;

    <p class="MetaInformation TxtRight">
        &lt;?= anchor($URLSlug .'/comments',$Entry->comment_id .' Comment(s)'); ?&gt;
    </p>
&lt;? endforeach; ?&gt;
&lt;?= file_get_contents($_SERVER['DOCUMENT_ROOT'] .'footer.html'); ?&gt;

Everything else outputs just fine, but I can't figure out how to print the COUNT result in my views template.

Any ideas?

Edit: Solved it with the slight addition 'AS comment_id' in the SQL, after COUNT(comment_id)
#2

[eluser]jedd[/eluser]
Hi Selyoid and welcome to the CI forums.

It might just be a naming problem - I don't use the AR class, but have issues with results from COUNTs because of the parenthesis.

You can use the 2nd parameter of $this->db->SELECT() - when set to FALSE it doesn't try to do anything magical with your first parameter, so you can rename it using the SQL AS feature to something more useful - such as count_comment_id

More information in the [url="http://ellislab.com/codeigniter/user-guide/database/active_record.html"] A R section of the user guide[/url], specifically in the Select paragraph.




Theme © iAndrew 2016 - Forum software by © MyBB