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 

Messy ListCat hack- display number of posts/date last posted

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



Joined: 07 May 2002
Posts: 4

PostPosted: Sun May 12, 2002 6:41 pm    Post subject: Messy ListCat hack- display number of posts/date last posted Reply with quote

I just made a quick hack for my wife's blog, http://ool.wd1.net/saplings

For the category list, it will display the number of posts in a category and the month/day of the last post.

I haven't cleaned it up yet and am still learning PHP and CSS, so it's bound to have things which could be done better.

First I changed the ListCat's function in the b2template.functions.php, you can replace the whole function with this code:
Quote:

function list_cats($optionall = 1, $all = "All", $optiondates = 0, $optioncount = 0) {
echo "<p class=storyCategory>";
global $tablecategories,$querycount, $tableposts;
$query="SELECT * FROM $tablecategories";
$result=mysql_query($query);
$querycount++;
if (intval($optionall) == 1) {
echo "\t<a href=\"$PHP_SELF?cat=all\">$all</a>\n";
}
while($row = mysql_fetch_object($result)) {
echo "\t<a href=\"$PHP_SELF?cat=".$row->cat_ID."\">";
echo $row->cat_name."</a>";
if ( (intval($optiondates) == 1) || (intval($optioncount) == 1) ) {

$queryposts="SELECT COUNT(*) as numposts, MONTH(MAX(post_date)) as lastmonth, DAYOFMONTH(MAX(post_date)) as lastday FROM $tableposts WHERE post_category=$row->cat_ID";
$resultposts=mysql_query($queryposts);
$querycountposts++;
$rowposts = mysql_fetch_object($resultposts);
if (intval($optioncount) == 1) {
echo "\t(".$rowposts->numposts.")";
}

if (intval($optiondates) == 1) {
if ($rowposts->lastmonth != "") {
echo "\t<I>".$rowposts->lastmonth."/".$rowposts->lastday."</I>";
}
}

}

echo "<br />\n";

}
echo "</p>";
}



The one addition I'm really unsure of is echo "<p class=storyCategory>"; and of course, later echo "</p"

It was my quick and dirty way to stop the number of posts and date from printing in really large font.

Then you just change the function call to ListCats in the index.php file to use the two parameters,
Quote:

<?php list_cats(0,"All",1,1); ?>



You have to stick "All" in as that is the default for the second parm that comes with List_cats(unless you have chosen another option, in which case use your own)

The first 1 after the all tells list_cat's to display the date.
The second 1 tells it to display the number of posts.

If you record the date & time of a person's last visit, this same code could be easily modified to display the number of new posts by simply adding a where clause to the query.[/i]
Back to top
View user's profile Send private message
adamwalker



Joined: 07 Apr 2002
Posts: 125
Location: England

PostPosted: Tue May 14, 2002 6:14 pm    Post subject: Reply with quote

hey i like the hack. ill prob add it to my site soon. dont forget to post when u update it :)
_________________
Shameless Plug - For all your B2 template and hack needs visit www.adamwalker.34sp.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
mikelittle



Joined: 11 May 2002
Posts: 376
Location: UK

PostPosted: Thu Aug 15, 2002 10:18 pm    Post subject: update for 0.6pre4 Reply with quote

I've updated this hack for the latest code:
The original from 0.6pre4:
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";
   }
}


becomes:

Code:

function list_cats($optionall = 1, $all = "All", $sort_column = 'ID', $sort_order = 'asc', $file = 'blah',
                   $optiondates = 0, $optioncount = 0) {
    global $tablecategories,$querycount, $tableposts;
    global $PHP_SELF;
   $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 $row->cat_name."</a>";
        if ( (intval($optiondates) == 1) || (intval($optioncount) == 1) ) {
            $queryposts="SELECT COUNT(*) as numposts, MONTH(MAX(post_date)) as lastmonth, DAYOFMONTH(MAX(post_date)) as lastday FROM $tableposts WHERE post_category=$row->cat_ID";
            $resultposts=mysql_query($queryposts);
            $querycountposts++;
            $rowposts = mysql_fetch_object($resultposts);
            if (intval($optioncount) == 1) {
                echo "  (".$rowposts->numposts.")";
            }

            if (intval($optiondates) == 1) {
                if ($rowposts->lastmonth != "") {
                    echo "  ".$rowposts->lastday."/".$rowposts->lastmonth."";
                }
            }

        }
        echo "<br />\n";
    }
}


And to use it, where you used to call:
Code:
<?php list_cats(0, 'All', 'name'); ?>


You now call:
Code:
<?php list_cats(0, 'All', 'name', 'asc', 'blah', 1, 1); ?>


Hope this helps.
Mike
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
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