boardom Forum Index boardom
b2 message board
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

counting posts by category

 
Post new topic   Reply to topic    boardom Forum Index -> Hacks
View previous topic :: View next topic  
Author Message
koski



Joined: 22 Jun 2002
Posts: 16

PostPosted: Wed Aug 28, 2002 12:20 pm    Post subject: counting posts by category Reply with quote

sometimes is useful to see how many posts you have in each category.

So, the first step is to code the function that will count the posts:

Code:
function count_cat_posts($cat_ID) {
        global $tableposts;

        $query = "SELECT * FROM $tableposts WHERE post_category = $cat_ID";
        $result = mysql_query( $query );
        return mysql_num_rows( $result );
}


You can add that function to b2-include/b2template.functions.php
You can see that it only requires a single parameter, the Category ID.

Now make more easy for us.

When listing the categories was very funny that the sistem adds automatically the posts count, but not when in a dropdown.

So let's go.

As easy as adding other paramter to the function 'list_cats' (in b2-include/b2template.function.php):

original function:

Code:
function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = 'blah') {
        global $tablecategories,$querycount;
        global $pagenow;
        $file = ($file == 'blah') ? $pagenow : $file;
        $sort_column = 'cat_'.$sort_column;
        $query="SELECT * FROM $tablecategories WHERE cat_ID > 0 ORDER BY $sort_column $sort_order";
        $result=mysql_query($query);
        $querycount++;
        if (intval($optionall) == 1) {
                echo "\t<a href=\"$file?cat=all\">$all</a><br />\n";
        }
        while($row = mysql_fetch_object($result)) {
                echo "\t<a href=\"$file?cat=".$row->cat_ID."\">";
                echo stripslashes($row->cat_name)."</a><br />\n";
        }
}


Now the modified function:

Code:
function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = 'blah', $count = 0) {
        global $tablecategories,$querycount;
        global $pagenow;
        $file = ($file == 'blah') ? $pagenow : $file;
        $sort_column = 'cat_'.$sort_column;
        $query="SELECT * FROM $tablecategories WHERE cat_ID > 0 ORDER BY $sort_column $sort_order";
        $result=mysql_query($query);
        $querycount++;
        if (intval($optionall) == 1) {
                echo "\t<a href=\"$file?cat=all\">$all</a><br />\n";
        }
        while($row = mysql_fetch_object($result)) {
                echo "\t<a href=\"$file?cat=".$row->cat_ID."\">";
                echo stripslashes($row->cat_name)."</a>";
                if ( $count == 1) {
                        echo "   ".
                             count_cat_posts( $row->cat_ID );
                }
                echo "<br />\n";
        }
}


We have added a parameter called $count, that accepts values 0 or 1.
0 = not counted
1 = count posts

Easy, hope you like it ;)

Salu2

PS: you can see how it works in http://oscar.gen7es.net click in "Archivos" and you will see how it works OR going directly to the page http://oscar.gen7es.net/b2custar.php. The weblog is in spanish but anyway you can figure out how it works.
Back to top
View user's profile Send private message
GamerZ



Joined: 15 May 2002
Posts: 537
Location: Singapore

PostPosted: Thu Aug 29, 2002 11:31 am    Post subject: Reply with quote

read some threads in sitepointforums, syaing that by using mysql_num_rows is quite inefficient. Instead try this
[php:1:04963d0f2a]
function count_cat_posts($cat_ID) {
global $tableposts;

$query = "SELECT COUNT(*) FROM $tableposts WHERE post_category = $cat_ID";
$result = mysql_query( $query );
return mysql_result($result,0);
}
[/php:1:04963d0f2a]
_________________

++ GamerZ.Per.Sg - Complex Simplicity
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
koski



Joined: 22 Jun 2002
Posts: 16

PostPosted: Thu Aug 29, 2002 1:42 pm    Post subject: Reply with quote

GamerZ I have tried your code and it has a problem when no rows were returned.

"Warning: Unable to jump to row 0 on MySQL result index #"

So, for now, I'll keep my code. But thank you for the advise.
Back to top
View user's profile Send private message
GamerZ



Joined: 15 May 2002
Posts: 537
Location: Singapore

PostPosted: Thu Aug 29, 2002 3:13 pm    Post subject: Reply with quote

koski wrote:
GamerZ I have tried your code and it has a problem when no rows were returned.

"Warning: Unable to jump to row 0 on MySQL result index #"

So, for now, I'll keep my code. But thank you for the advise.
hmm wierd. LOL Okie
_________________

++ GamerZ.Per.Sg - Complex Simplicity
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
alex_t_king



Joined: 09 Oct 2002
Posts: 194

PostPosted: Fri Oct 25, 2002 1:54 am    Post subject: Reply with quote

I guess I should search this thing before I go off and start hacking on my own - I basically wrote the same code you guys did, but I'd embedded it all in the list_cats() function. I like the way you separated the count into it's own function, so I redid my code in that manner, but left my SQL using the COUNT(*) function as gamerz noted - using this is a much more efficient method.

Here it is:

Code:
function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = 'blah', $count_posts = 0) {
   global $tablecategories,$querycount;
   global $pagenow;
   global $querystring_start, $querystring_equal, $querystring_separator;
   $file = ($file == 'blah') ? $pagenow : $file;
   $sort_column = 'cat_'.$sort_column;
   $query="SELECT * FROM $tablecategories WHERE cat_ID > 0 ORDER BY $sort_column $sort_order";
   $result=mysql_query($query);
   $querycount++;
   if (intval($optionall) == 1) {
      echo "\t<a href=\"".$file.$querystring_start.'cat'.$querystring_equal.'all">'.$all."</a><br />\n";
   }
   while($row = mysql_fetch_object($result)) {
// begin hack
// added new param: $count_posts
      $temp = "";
// getting the count of posts in each section
      if ($count_posts == 1) {
         $count = count_cat_posts($row->cat_ID);
         $temp = " (".$count.")";
      }
// end hack
      echo "\t<a href=\"".$file.$querystring_start.'cat'.$querystring_equal.$row->cat_ID.'">';
      echo stripslashes($row->cat_name)."</a>".$temp."<br />\n";
   }
}

// hack
function count_cat_posts($cat_ID) {
   global $tableposts;

   $count = mysql_query("SELECT COUNT(*) FROM $tableposts WHERE post_category = '$cat_ID'");
   $count = mysql_result($count,0);
   return $count;
}


This shows "Category Name (15)" where 15 is the number of posts for each category.
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
yulee



Joined: 03 May 2003
Posts: 8

PostPosted: Sun May 04, 2003 12:45 pm    Post subject: Reply with quote

Thank you Alex, awesome!
Back to top
View user's profile Send private message
Cyberian75



Joined: 26 Sep 2002
Posts: 1274
Location: Oregon

PostPosted: Sun May 04, 2003 6:06 pm    Post subject: Reply with quote

I would use "COUNT(ID)" in the count function for efficiency. There's really no point of counting every column in each row.
_________________
Michael P.

Back to top
View user's profile Send private message AIM Address
yulee



Joined: 03 May 2003
Posts: 8

PostPosted: Sun May 04, 2003 6:22 pm    Post subject: Reply with quote

Err, excuse me but I'm not a developer, so I cannot follow your comments. Do you think there is a way to get it running better?

Thank you!
Back to top
View user's profile Send private message
Cyberian75



Joined: 26 Sep 2002
Posts: 1274
Location: Oregon

PostPosted: Sun May 04, 2003 6:41 pm    Post subject: Reply with quote

yulee wrote:
Err, excuse me but I'm not a developer, so I cannot follow your comments. Do you think there is a way to get it running better?

Thank you!



It's nothing major, but it's recommended to query only the columns needed. So, Alex's second function becomes:

Code:

function count_cat_posts($cat_ID) {
   global $tableposts;

   $count = mysql_query("SELECT COUNT(ID) FROM $tableposts WHERE post_category = '$cat_ID'");
   $count = mysql_result($count,0);
   return $count;
}


Although the increase in performance is not noticeable, it should increase it by a little.
_________________
Michael P.

Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    boardom Forum Index -> Hacks All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2 © 2001, 2002 phpBB Group