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 

[Hack] Last X posts from Y Category Links

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



Joined: 05 Aug 2002
Posts: 179
Location: Cambridge, MA

PostPosted: Wed Mar 05, 2003 10:15 pm    Post subject: [Hack] Last X posts from Y Category Links Reply with quote

Ok, hacked this together for my http://goombalooza.com site. Keep in mind, my site is hacked up like crazy and I don't really plan to upgrade the b2 part for it since it's for friends, but.. I got this to work and I thought it may be useful for you

In b2functions.php, add this function

Code:

function list_recentPosts($category,$limit) {
   global $tableposts,$myrow,$blogfilename,$siteurl;

//if you don't set a limit, default is 5
   if (!isset($limit)) { $limit = 5; }

//if you don't set a category, default is 1
   if (!isset($category)) { $category = 1;}

$sql = "select ID, post_title, post_author, DATE_FORMAT(post_date, '%m/%d/%y') AS date from $tableposts where post_category=$category order by post_date desc limit $limit";
      $result = mysql_query ($sql) or die (mysql_error());

//here is the loop that produces the links by title

   while ($row = mysql_fetch_array($result)){
      echo "<a href=\"".$siteurl."/".$blogfilename."?p=".$row["ID"]."\">";
      echo "".$row["post_title"]."</a><br />";

/* this will grab the user's nickname for that post followed by the date below the link, you can exclude it if you want */
      $myrow = get_userdata($row["post_author"]);
      echo "  <i>(".$myrow["user_nickname"]." on ".$row["date"].")</i><br />";
      }
}


Then on your page (make sure you still include the blog.header.php at the top), whereever you want your list to be, you put this:

Code:

<?php list_recentPosts(categorynum,limitnumber) ?>

ie: <?php list_recentPosts(4,6) ?> would list the last 6 posts from category 4

Let me know if you try it, how it goes. Problems, let me know. Any programmers that see a way to optimize it, let me know. I don't have an ego on this stuff, it's pretty fun

EDIT: Michael pointed out a problem, thanks!


Last edited by dtdgoomba on Thu Mar 06, 2003 5:00 am; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
Cyberian75



Joined: 26 Sep 2002
Posts: 1274
Location: Oregon

PostPosted: Thu Mar 06, 2003 4:06 am    Post subject: Reply with quote

Nice... Smile

But why echo ".php"? I think $blogfilename has the full filename.
_________________
Michael P.

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



Joined: 05 Aug 2002
Posts: 179
Location: Cambridge, MA

PostPosted: Thu Mar 06, 2003 4:20 am    Post subject: Reply with quote

ahhh, good point... it's b/c i was altering my actual code

my site uses a 2nd page after the headlines and I forgot to delete it, ha

thanks!
Back to top
View user's profile Send private message Visit poster's website
Cyberian75



Joined: 26 Sep 2002
Posts: 1274
Location: Oregon

PostPosted: Thu Mar 06, 2003 4:29 am    Post subject: Reply with quote

Also, I wouldn't reccomand selecting all (*) fields. Just select the ones you need which will increase the speed of the query. Smile
_________________
Michael P.

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



Joined: 05 Aug 2002
Posts: 179
Location: Cambridge, MA

PostPosted: Thu Mar 06, 2003 5:00 am    Post subject: Reply with quote

duly noted... hey, this is what forums are for right? Very Happy
Back to top
View user's profile Send private message Visit poster's website
Cyberian75



Joined: 26 Sep 2002
Posts: 1274
Location: Oregon

PostPosted: Thu Mar 06, 2003 5:02 am    Post subject: Reply with quote

Of course... Very Happy
_________________
Michael P.

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



Joined: 19 Apr 2003
Posts: 27

PostPosted: Sat Apr 19, 2003 6:59 pm    Post subject: Reply with quote

Perfect...this was the last thing I was looking for to complete my transition from a perl cms script i was using. Now the real work can begin...templates!! Cool
_________________
7 out of 10 voices in my head say I'm not crazy
Back to top
View user's profile Send private message
dtdgoomba



Joined: 05 Aug 2002
Posts: 179
Location: Cambridge, MA

PostPosted: Wed Apr 23, 2003 12:56 am    Post subject: Reply with quote

Glad to hear this helped!
_________________
Goombalooza! | Last X posts from Y Category | Login Box
Back to top
View user's profile Send private message Visit poster's website
mikelowe



Joined: 04 Jul 2003
Posts: 1
Location: Melbourne

PostPosted: Fri Jul 04, 2003 10:22 am    Post subject: Reply with quote

looks like what I am after, haven't played with your code yet, but was thinking of modifing the to accept multiple categories/array of categories (anyway, this is what I am planning to do).

The code could be
Keep up the good work.

Cheers
_________________
mikelowe
Back to top
View user's profile Send private message
Jenny



Joined: 24 Aug 2003
Posts: 2

PostPosted: Sun Aug 24, 2003 11:37 pm    Post subject: Reply with quote

I am trying to convert this great piece of code so that it also displays the posts, just like on the normal index.php (I need the limitation per category)
But I cannot figure out how to include the content as the_content() doesn't seem to work.
Back to top
View user's profile Send private message
sabotage



Joined: 15 Jul 2003
Posts: 36

PostPosted: Tue Aug 26, 2003 10:58 am    Post subject: Reply with quote

heh, i've been trying the same as jenny, but my head is already split up from other things i had on my mind....

so any help would be twice as appreciated by now
Back to top
View user's profile Send private message
sabotage



Joined: 15 Jul 2003
Posts: 36

PostPosted: Tue Sep 02, 2003 11:34 am    Post subject: Reply with quote

*bump*....

please? Smile
Back to top
View user's profile Send private message
Johanna



Joined: 01 Aug 2003
Posts: 11
Location: Sweden

PostPosted: Sun Sep 07, 2003 11:06 am    Post subject: Problems displaying the date Reply with quote

I can't get the code to display the date. User nickname is displayed correctly, but I want the date of the post as well. Why does it not work? My code:
Quote:

//if you don't set a limit, default is 5
if (!isset($limit)) { $limit = 5; }

//if you don't set a category, default is 1
if (!isset($category)) { $category = 1;}

$sql = "select ID, post_title, post_author, DATE_FORMAT(post_date, 'd:F:Y') AS date from $tableposts where post_category=$category order by post_date desc limit $limit";
$result = mysql_query ($sql) or die (mysql_error());

//here is the loop that produces the links by title

while ($row = mysql_fetch_array($result)){
echo "<a href=\"".$siteurl."/".$blogfilename."?p=".$row["ID"]."\">";
echo "".$row["post_title"]."</a><br />";

/* this will grab the user's nickname for that post followed by the date below the link, you can exclude it if you want */
$myrow = get_userdata($row["post_author"]);
echo " <i>(".$myrow["user_nickname"]." den ".$row["date"].")</i><br />";
}
}

as you can see very little is changed from the code given by dtdgoomba at first. Anyone? Please?
_________________
Constantly floating around in the clouds...
Back to top
View user's profile Send private message Visit poster's website
dtdgoomba



Joined: 05 Aug 2002
Posts: 179
Location: Cambridge, MA

PostPosted: Tue Sep 16, 2003 1:18 am    Post subject: Reply with quote

hey everyone, sorry, I hadn't checked this code or b2 boards in months since I stopped playing with it. I'll try and take a look this week to help you out. In the meantime, please point me to a link (PM me if you don't want it public) where you have the site in the works. and put just after your $sql line and before your $result line:

echo $sql;

you should then see the sql statement being run and maybe from there I can see if there is an error. What I try to do is put some echo statements before and after things to see what is missing in the results. Well, i guess all REAL programmers do that too, ha...

I'll try to do my best to help you all
_________________
Goombalooza! | Last X posts from Y Category | Login Box
Back to top
View user's profile Send private message Visit poster's website
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