View previous topic :: View next topic |
Author |
Message |
dtdgoomba
Joined: 05 Aug 2002 Posts: 179 Location: Cambridge, MA
|
Posted: Wed Mar 05, 2003 10:15 pm Post subject: [Hack] Last X posts from Y Category Links |
|
|
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 |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1274 Location: Oregon
|
Posted: Thu Mar 06, 2003 4:06 am Post subject: |
|
|
Nice...
But why echo ".php"? I think $blogfilename has the full filename. _________________ Michael P.
 |
|
Back to top |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 179 Location: Cambridge, MA
|
Posted: Thu Mar 06, 2003 4:20 am Post subject: |
|
|
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 |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1274 Location: Oregon
|
Posted: Thu Mar 06, 2003 4:29 am Post subject: |
|
|
Also, I wouldn't reccomand selecting all (*) fields. Just select the ones you need which will increase the speed of the query.  _________________ Michael P.
 |
|
Back to top |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 179 Location: Cambridge, MA
|
Posted: Thu Mar 06, 2003 5:00 am Post subject: |
|
|
duly noted... hey, this is what forums are for right?  |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1274 Location: Oregon
|
Posted: Thu Mar 06, 2003 5:02 am Post subject: |
|
|
Of course...  _________________ Michael P.
 |
|
Back to top |
|
 |
mcgub
Joined: 19 Apr 2003 Posts: 27
|
Posted: Sat Apr 19, 2003 6:59 pm Post subject: |
|
|
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!!  _________________ 7 out of 10 voices in my head say I'm not crazy |
|
Back to top |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 179 Location: Cambridge, MA
|
|
Back to top |
|
 |
mikelowe
Joined: 04 Jul 2003 Posts: 1 Location: Melbourne
|
Posted: Fri Jul 04, 2003 10:22 am Post subject: |
|
|
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 |
|
 |
Jenny
Joined: 24 Aug 2003 Posts: 2
|
Posted: Sun Aug 24, 2003 11:37 pm Post subject: |
|
|
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 |
|
 |
sabotage
Joined: 15 Jul 2003 Posts: 36
|
Posted: Tue Aug 26, 2003 10:58 am Post subject: |
|
|
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 |
|
 |
sabotage
Joined: 15 Jul 2003 Posts: 36
|
Posted: Tue Sep 02, 2003 11:34 am Post subject: |
|
|
*bump*....
please?  |
|
Back to top |
|
 |
Johanna
Joined: 01 Aug 2003 Posts: 11 Location: Sweden
|
Posted: Sun Sep 07, 2003 11:06 am Post subject: Problems displaying the date |
|
|
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 |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 179 Location: Cambridge, MA
|
Posted: Tue Sep 16, 2003 1:18 am Post subject: |
|
|
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 |
|
 |
|