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 

Last 5 Posts hack
Goto page 1, 2  Next
 
Post new topic   Reply to topic    boardom Forum Index -> Hacks
View previous topic :: View next topic  
Author Message
Agent



Joined: 12 Jun 2002
Posts: 2

PostPosted: Wed Jun 12, 2002 3:20 am    Post subject: Last 5 Posts hack Reply with quote

Well, this hack displays the title and time of the last 5 posts that you/your blog posters have made. First of all, I would like to thank nessahead, because I basically just modified her last five comments hack. Put this code inside a file called lastposts.php:
Code:

<?php

include("b2config.php"); // change this to match your b2config.php location

?>

<p>
<?php
$db = mysql_connect("$dbhost", "$dbusername", "$dbpassword");
mysql_select_db("$dbname",$db);

$sql="SELECT ID,post_author,post_date,post_title,post_content FROM $tableposts ORDER BY post_date DESC LIMIT 5";
$result=mysql_query($sql,$db);

$num = mysql_num_rows($result);
$cur = 1;

// Loop-dee-loop to display last 5 records
while ($num >= $cur) {

$row = mysql_fetch_array($result);

$post_ID = $row["ID"];
$post_date = $row["post_date"];
$post_title = $row["post_title"];

// Convert datetime to happiness
$string = $row["post_date"];
$stringArray = explode("-", $string);
$date = mktime(0,0,0,$stringArray[1],$stringArray[2],$stringArray[0]);
$convertedDate = date("m.d.Y", $date);

// Display each record
echo "<p style=\"margin-top: 5; margin-bottom: 5\"><b>φ</b> <a href=\"index.php?p=$post_ID\" class=\"nav\">$post_title</a> [$convertedDate]<br>";

$cur++;
}

?>
</p>

Now, just put <? include("lastposts.php"); ?> somewhere in your page.
Back to top
View user's profile Send private message
nessahead



Joined: 12 Mar 2002
Posts: 312
Location: Los Angeles, CA

PostPosted: Wed Jun 12, 2002 4:05 am    Post subject: Reply with quote

Nice! Smile
_________________
raar!
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Agent



Joined: 12 Jun 2002
Posts: 2

PostPosted: Wed Jun 12, 2002 4:11 am    Post subject: Reply with quote

Thanks. :D
Back to top
View user's profile Send private message
feelso



Joined: 30 May 2002
Posts: 52
Location: Paris, France

PostPosted: Wed Jun 12, 2002 7:10 pm    Post subject: Reply with quote

Great, but I have some questions:

How can I add the time in hour and the name of the post author?

Why do the apostrophe (') always appear with an anti-slash before such as: O\'neal ? What can I do to avoid that?

Can we limit the number of characters of the title in order to avoid long title to appear on a little table for instance?

Thanks in advance!


feelso
Back to top
View user's profile Send private message
nessahead



Joined: 12 Mar 2002
Posts: 312
Location: Los Angeles, CA

PostPosted: Wed Jun 12, 2002 7:45 pm    Post subject: Reply with quote

For changing what's displayed, take a look at the line that starts off
Code:
echo "...
You can change the stuff in there to make it look how you want. If you want to add in the name of the post author, I think you'd add in
Code:
$post_author


As for adding in the time with the date, take a look at the line that says
Code:
$convertedDate = date("m.d.Y", $date);
Take a look at http://www.php.net/manual/en/function.date.php and scroll down a bit and you'll see what all the symbols represent. So, you can change the thing that says
Code:
m.d.Y
to say what you want it to.

For stripping the slashes, there's a little PHP function called stripsplashes() - http://www.php.net/manual/en/function.stripslashes.php - I think that will work for ya. Those apostrophes need to be there because apostrophes are used quite a bit in coding and whatnot and the code wouldn't be able to tell the difference between what's content and what's code. So, any one character that has a \ before it is displayed as is and not regarded as code Smile

As for limiting the characters of the title to avoid it being too long, I'm sure it's not that hard to do... but I have no idea how to do it Razz
_________________
raar!
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
feelso



Joined: 30 May 2002
Posts: 52
Location: Paris, France

PostPosted: Wed Jun 12, 2002 8:03 pm    Post subject: Reply with quote

nessahead wrote:

If you want to add in the name of the post author, I think you'd add in
Code:
$post_author


--> $post_author = ID of the author... not the name! That's my problem actually!

nessahead wrote:
As for adding in the time with the date, take a look at the line that says
Code:
$convertedDate = date("m.d.Y", $date);


--> if I add the time parameters in $convertDate, the result is: "00h00". That's my problem actually!

nessahead wrote:
For stripping the slashes, there's a little PHP function called stripsplashes()


--> I heard about that but i don't understand how to use it... in this particular case!

nessahead wrote:
As for limiting the characters of the title to avoid it being too long, I'm sure it's not that hard to do... but I have no idea how to do it :P


--> Well, i hope that someone will help me :)

Thanks nessahead, you have a nice website...


feelso
Back to top
View user's profile Send private message
nessahead



Joined: 12 Mar 2002
Posts: 312
Location: Los Angeles, CA

PostPosted: Wed Jun 12, 2002 9:07 pm    Post subject: Reply with quote

Ah, ok. The author's nickname is not in b2posts, it's in b2users so you'd have to get the data from that table under user_nickname. Haven't really thought out how to display it, though...

Don't know what to say about the time thing Razz

For stripping slashes, where it says
Code:
$post_title = $row["post_title"];
replace it with
Code:
$post_title = stripslashes($row["post_title"]);


Smile
_________________
raar!
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Benjy



Joined: 25 Jan 2002
Posts: 108
Location: Paris, France

PostPosted: Thu Jun 13, 2002 10:19 am    Post subject: Why don't people use b2's engine? Reply with quote

Code:
<?
//LAST XX POSTS HACK

//default number of posts you want to show
//can be sent as a parameter
if (!$posts) {
  $posts=5;
}

//default number of chars to display for the title
//this will add "..." right after the first space found in the title, starting from this value
//can be sent as a parameter
if (!$limitchars) {
  $limitchars=30;
}

$blog=1; include ("b2/blog.header.php");
while($row = mysql_fetch_object($result)) { start_b2();

$post_title=get_the_title();

if (strlen($post_title)>$limitchars) {
  $firstspacepos=strpos($post_title." "," ",$limitchars);
  if ($firstspacepos!=0) {
    $post_title=substr($post_title,0,$firstspacepos)."...";
  }
}
?>

<font face="arial" size="2"><b><a href="<?php echo "$siteurl/$blogfilename"; ?>?p=<?php the_id() ?>&c=1" target="_self"><?php echo $post_title ?></a></b> @ <i><?php the_time("d.m.Y") ?></i> by <a href="mailto:<?php the_author_email() ?>"><?php the_author() ?></a></font><br>

<?
}
?>


Demo 1: http://www.daprod.com/~newcomics/lastposts.php
Demo 2: http://www.daprod.com/~newcomics/lastposts.php?posts=7
Demo 3: http://www.daprod.com/~newcomics/lastposts.php?limitchars=4
Demo 4: http://www.daprod.com/~newcomics/lastposts.php?posts=7&limitchars=7

And as you guessed it, with this one you got all the b2 displaying possibilities Very Happy

Benjy.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
nessahead



Joined: 12 Mar 2002
Posts: 312
Location: Los Angeles, CA

PostPosted: Thu Jun 13, 2002 5:25 pm    Post subject: Reply with quote

Ooh, very nice Smile
_________________
raar!
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Xcalibur



Joined: 11 May 2002
Posts: 4

PostPosted: Fri Jun 14, 2002 5:03 am    Post subject: Reply with quote

this is a great hack!
i've been looking for something like this.. 1 question though.
is there a way we can specify the category? I only want it to display things i post in the general category and not the other ones.
Back to top
View user's profile Send private message
Benjy



Joined: 25 Jan 2002
Posts: 108
Location: Paris, France

PostPosted: Fri Jun 14, 2002 7:26 am    Post subject: Reply with quote

Put $cat="yourcatnumber" for a specific category, or $cat="0" or $cat="all" for all categories, before
Code:
$blog=1; include ("b2/blog.header.php");

You can also view only certain categories and block others... check this post: http://www.tidakada.com/board/viewtopic.php?p=1534#1534

Remember that blocked categories must be added first.

Benjy.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
jmarzo



Joined: 22 Feb 2002
Posts: 9
Location: Toronto, Canada

PostPosted: Fri Jun 14, 2002 4:06 pm    Post subject: Reply with quote

great hack.. thanks
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Xcalibur



Joined: 11 May 2002
Posts: 4

PostPosted: Sat Jun 15, 2002 6:47 am    Post subject: Reply with quote

I want to do it the original author's way instead of using the b2 way, because i will be inserting on the same page as another b2 blog, and cant limit the category properly that way..

anyway, i modified the syntax from this:

Code:
$sql="SELECT ID,post_author,post_date,post_title,post_content FROM $tableposts ORDER BY post_date DESC LIMIT 5";


to this:

Code:
$sql="SELECT ID,post_author,post_date,post_title,post_content FROM $tableposts ORDER BY post_date DESC LIMIT 5 WHERE post_category=1";


but it doesnt work.. i get a mysql error.. although i'm pretty sure my syntax is right.. can anyone help please? ;)
Back to top
View user's profile Send private message
nessahead



Joined: 12 Mar 2002
Posts: 312
Location: Los Angeles, CA

PostPosted: Sat Jun 15, 2002 8:14 pm    Post subject: Reply with quote

You have to put the 'WHERE post_category = 1' after your FROM thingy, like this, I think:

Code:
SELECT ID,post_author,post_date,post_title,post_content FROM b2posts  WHERE post_category = 1 ORDER BY post_date DESC LIMIT 5

_________________
raar!
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Xcalibur



Joined: 11 May 2002
Posts: 4

PostPosted: Sun Jun 16, 2002 3:48 am    Post subject: Reply with quote

Thanks! it works .. hehehe :D
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    boardom Forum Index -> Hacks All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
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