View previous topic :: View next topic |
Author |
Message |
Agent
Joined: 12 Jun 2002 Posts: 2
|
Posted: Wed Jun 12, 2002 3:20 am Post subject: Last 5 Posts hack |
|
|
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 |
|
 |
nessahead
Joined: 12 Mar 2002 Posts: 312 Location: Los Angeles, CA
|
Posted: Wed Jun 12, 2002 4:05 am Post subject: |
|
|
Nice!  _________________ raar! |
|
Back to top |
|
 |
Agent
Joined: 12 Jun 2002 Posts: 2
|
Posted: Wed Jun 12, 2002 4:11 am Post subject: |
|
|
Thanks. :D |
|
Back to top |
|
 |
feelso
Joined: 30 May 2002 Posts: 52 Location: Paris, France
|
Posted: Wed Jun 12, 2002 7:10 pm Post subject: |
|
|
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 |
|
 |
nessahead
Joined: 12 Mar 2002 Posts: 312 Location: Los Angeles, CA
|
Posted: Wed Jun 12, 2002 7:45 pm Post subject: |
|
|
For changing what's displayed, take a look at the line that starts off 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
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 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
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  _________________ raar! |
|
Back to top |
|
 |
feelso
Joined: 30 May 2002 Posts: 52 Location: Paris, France
|
Posted: Wed Jun 12, 2002 8:03 pm Post subject: |
|
|
nessahead wrote: |
If you want to add in the name of the post author, I think you'd add in |
--> $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 |
|
 |
nessahead
Joined: 12 Mar 2002 Posts: 312 Location: Los Angeles, CA
|
Posted: Wed Jun 12, 2002 9:07 pm Post subject: |
|
|
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
For stripping slashes, where it says Code: | $post_title = $row["post_title"]; | replace it with Code: | $post_title = stripslashes($row["post_title"]); |
 _________________ raar! |
|
Back to top |
|
 |
Benjy
Joined: 25 Jan 2002 Posts: 108 Location: Paris, France
|
Posted: Thu Jun 13, 2002 10:19 am Post subject: Why don't people use b2's engine? |
|
|
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
Benjy. |
|
Back to top |
|
 |
nessahead
Joined: 12 Mar 2002 Posts: 312 Location: Los Angeles, CA
|
Posted: Thu Jun 13, 2002 5:25 pm Post subject: |
|
|
Ooh, very nice  _________________ raar! |
|
Back to top |
|
 |
Xcalibur
Joined: 11 May 2002 Posts: 4
|
Posted: Fri Jun 14, 2002 5:03 am Post subject: |
|
|
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 |
|
 |
Benjy
Joined: 25 Jan 2002 Posts: 108 Location: Paris, France
|
Posted: Fri Jun 14, 2002 7:26 am Post subject: |
|
|
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 |
|
 |
jmarzo
Joined: 22 Feb 2002 Posts: 9 Location: Toronto, Canada
|
Posted: Fri Jun 14, 2002 4:06 pm Post subject: |
|
|
great hack.. thanks |
|
Back to top |
|
 |
Xcalibur
Joined: 11 May 2002 Posts: 4
|
Posted: Sat Jun 15, 2002 6:47 am Post subject: |
|
|
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 |
|
 |
nessahead
Joined: 12 Mar 2002 Posts: 312 Location: Los Angeles, CA
|
Posted: Sat Jun 15, 2002 8:14 pm Post subject: |
|
|
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 |
|
 |
Xcalibur
Joined: 11 May 2002 Posts: 4
|
Posted: Sun Jun 16, 2002 3:48 am Post subject: |
|
|
Thanks! it works .. hehehe :D |
|
Back to top |
|
 |
|