 |
boardom b2 message board
|
View previous topic :: View next topic |
Author |
Message |
.Chris
Joined: 30 Apr 2002 Posts: 186 Location: Hawaii
|
Posted: Sat Aug 31, 2002 1:50 am Post subject: |
|
|
I kind of modified this to show the post titles also.. Code: |
<?php
/* Last 5 Comments for b2 by [email protected]
1. Change line 9 so that it points to your b2config.php file.
2. Wherever you want it to display on your page, just put
<?php include("path/to/last5Comments.php"); ?>
3. Feel free to change it how you see fit & enjoy :) */
include("journal/b2config.php"); ?>
<?php
$db = mysql_connect("$dbhost", "$dbusername", "$dbpassword");
mysql_select_db("$dbname",$db);
$sql="SELECT comment_post_ID,post_title, comment_author, comment_date FROM b2posts, b2comments WHERE b2posts.ID=b2comments.comment_post_ID ORDER BY b2comments.comment_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);
$comment_post_ID = $row["comment_post_ID"];
$post_title = stripslashes($row["post_title"]);
$comment_author = $row["comment_author"];
$comment_date = $row["comment_date"];
// Convert datetime to happiness
$string = $row["comment_date"];
$stringArray = explode("-", $string);
$date = mktime(0,0,0,$stringArray[1],$stringArray[2],$stringArray[0]);
$convertedDate = date("M jS", $date);
// Display each record
echo "$convertedDate $comment_author (<a href=\"index.php?p=$comment_post_ID&c=1#comments\">$post_title</a>)<br>";
$cur++;
}
?> |
|
|
Back to top |
|
 |
daisha
Joined: 23 Nov 2002 Posts: 1
|
Posted: Sat Nov 23, 2002 7:09 pm Post subject: |
|
|
hello. i was wondering if there is anyway that rather than having the subject and date, that you could have it show the author of the post and maybe a potion of the comment and have a link going to the comments area so you can get the rest of what they posted in the comments?
am i making sense????  |
|
Back to top |
|
 |
Mister44

Joined: 31 Oct 2002 Posts: 237 Location: Philadelphia, PA, USA
|
Posted: Sun Dec 01, 2002 5:15 pm Post subject: |
|
|
Code: | <?php
// last_comments - based on .Chris's modified version of Last 5 Comments for b2 by [email protected]
// slightly more configurable, takes into account the extended query string syntax
// handles selection and limits in the db for greater efficiency
// could be improved to find latest comment on $lfc_comment_limit posts.
// could be improved to open comments as popups/new window/current window based on a toggle.
// this, unlike nessa's code, will not operate in standalone mode.
// lfc_comment_limit: the number of comments to return
$lfc_comment_limit = 5;
// lfc_ignore_tb|pb: 0/1 - Trackbacks and Pingbacks are considered comments
$lfc_ignore_tb= 1;
$lfc_ignore_pb= 1;
$query = 'SELECT comment_post_ID, post_title, comment_author, comment_date';
$query .= " FROM $tableposts, $tablecomments";
$query .= " WHERE $tableposts.ID = $tablecomments.comment_post_ID";
$query .= ( $lfc_ignore_tb ) ? " AND $tablecomments.comment_content NOT LIKE '<trackback />%'" : "";
$query .= ( $lfc_ignore_pb ) ? " AND $tablecomments.comment_content NOT LIKE '<pingback />%'" : "";
$query .= " ORDER BY $tablecomments.comment_date DESC";
$query .= " LIMIT $lfc_comment_limit";
// echo htmlentities( $query ).'<br />';
$result = mysql_query($query);
$querycount++;
while ( $lfc_row_r = mysql_fetch_array( $result ) ) {
// Convert datetime to happiness
$commdate = $lfc_row_r['comment_date'];
$commdate_r = explode( '-', $commdate );
$commdate = mktime( 0, 0, 0, $commdate_r[1], $commdate_r[2], $commdate_r[0]);
$commdate = date("M jS", $commdate);
$lfc_title = stripslashes( $lfc_row_r['post_title'] );
$lfc_title = empty( $lfc_title ) ? 'Untitled' : $lfc_title;
echo "$commdate ".$lfc_row_r['comment_author'].' (<a href="'.$blogfilename.$querystring_start.'p'.$querystring_equal.$lfc_row_r['comment_post_ID'].$querystring_separator.'c'.$querystring_equal.'1#comments">'.$lfc_title.'</a>)<br />';
}
?> |
I think the mkdate could be done more efficiently as well, but it's not critical unless you're generating seriously large numbers of pageviews. |
|
Back to top |
|
 |
JEDOK
Joined: 19 Dec 2002 Posts: 4 Location: korea
|
Posted: Thu Dec 19, 2002 4:16 pm Post subject: |
|
|
I've found a strange problem
When i include last5Comments.php before the b2 loop, the whole b2 loop seems to be ignored. Everything is ok but only the lines within b2 loop doesn't show up in the browser. Last 5 comments are listed fine, but i can't see my posts at all.
yeah, when i put <?php include(last5Comments); ?> after the end of the b2 loop, then everyting works fine, and i can see the posts too.
mmm... is there any answer??? |
|
Back to top |
|
 |
mikelittle
Joined: 11 May 2002 Posts: 374 Location: UK
|
Posted: Fri Dec 20, 2002 12:43 am Post subject: |
|
|
This is because it is using a variable called $result, which is also setup by blog.header.php and used by the main loop.
When you include this hack it wipes out $result for the main loop.
Hope this helps,
Mike _________________ Mike Little
http://zed1.com/b2/
"Share what you know. Learn what you don't." |
|
Back to top |
|
 |
JEDOK
Joined: 19 Dec 2002 Posts: 4 Location: korea
|
Posted: Fri Dec 20, 2002 8:05 am Post subject: |
|
|
Thanks, Mike. Now the problem is not a "strange" thing....
But ... you mean that there is no way to see the last 5 comments before the main posts? |
|
Back to top |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 178 Location: Cambridge, MA
|
Posted: Fri Dec 20, 2002 2:56 pm Post subject: |
|
|
In that hack, just rename all instances of $result to something else, like $comresult. I do that for all my hacks so they'll work |
|
Back to top |
|
 |
mikelittle
Joined: 11 May 2002 Posts: 374 Location: UK
|
Posted: Fri Dec 20, 2002 3:13 pm Post subject: |
|
|
It's not a problem to see the last 5 comments before the main loop.
The thing to do is stick the code into a function (which takes the variables out of scope of the main script) and call the function wherever you want.
Try this:
Code: |
/** recent_comments()
** Originally by [email protected]. Modified by Chris, Benjy, and [email protected]
** echo the n most recent comments: Output is date, author, and post title
** as a link to the full story.
** Parameters:
** $limit (default 5) -- the number of comments to extract
** $ignore_tb (default 1) -- 1 = don't include trackbacks, 0 = do include them
** $ignore_pb (default 1) -- 1 = don't include pingbacks, 0 = do include them
** You can change the date format in the function itself
**/
function recent_comments($limit = 5, $ignore_tb = 1, $ignore_pb = 1) {
global $tableposts, $tablecomments;
$query = 'SELECT comment_post_ID, post_title, comment_author, comment_date';
$query .= " FROM $tableposts, $tablecomments";
$query .= " WHERE $tableposts.ID = $tablecomments.comment_post_ID";
$query .= ( $ignore_tb ) ? " AND $tablecomments.comment_content NOT LIKE '<trackback />%'" : "";
$query .= ( $ignore_pb ) ? " AND $tablecomments.comment_content NOT LIKE '<pingback />%'" : "";
$query .= " ORDER BY $tablecomments.comment_date DESC";
$query .= " LIMIT $limit";
$result = mysql_query($query);
$querycount++;
while ($row = mysql_fetch_array($result)) {
// Convert datetime to happiness
$commdate = $row['comment_date'];
$commdate_r = explode( '-', $commdate );
$commdate = mktime( 0, 0, 0, $commdate_r[1], $commdate_r[2], $commdate_r[0]);
$commdate = date("M jS", $commdate);
$title = stripslashes( $row['post_title'] );
$title = empty( $title ) ? 'Untitled' : $title;
echo "$commdate ".$row['comment_author'].' (<a href="'.$blogfilename.$querystring_start.'p'.$querystring_equal.
$row['comment_post_ID'].
$querystring_separator.'c'.
$querystring_equal.'1#comments">'.
$title.'</a>)<br />';
}
}
?>
|
Stick this at the top of your file, then wherever you want the last five comments to appear just type in
Code: | <?php recent_comments(); ?> |
And that should do it.
Hope this helps,
Mike _________________ Mike Little
http://zed1.com/b2/
"Share what you know. Learn what you don't." |
|
Back to top |
|
 |
JEDOK
Joined: 19 Dec 2002 Posts: 4 Location: korea
|
Posted: Fri Dec 20, 2002 3:50 pm Post subject: |
|
|
Thank you~! finally it works fine.
I think using it as a function is better than the original one.
(well, I know nothing about PHP, though.. hehe) |
|
Back to top |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 178 Location: Cambridge, MA
|
Posted: Fri Dec 20, 2002 4:16 pm Post subject: |
|
|
Nice Mike. Since I wasn't a comp sci guy in school, I totally forgot about 'scope' and global variables, etc that my roommate has been trying to teach me in previous cases. Keep up the good work  |
|
Back to top |
|
 |
wteening
Joined: 12 Jul 2002 Posts: 71
|
Posted: Fri Dec 27, 2002 6:14 pm Post subject: |
|
|
How would I create a function from the following?
<?php include("b2config.php"); ?>
<p>
<?php
$d4 = mysql_connect("$dbhost", "$dbusername", "$dbpassword");
mysql_select_db("$dbname",$d4);
$sql="SELECT comment_post_ID,comment_author, post_title FROM $tablecomments, $tableposts WHERE comment_post_ID=ID ORDER BY comment_date DESC LIMIT 4";
$result=mysql_query($sql,$d4);
$num = mysql_num_rows($result);
$cur = 1;
// Loop-dee-loop to display last 5 records
while ($num >= $cur) {
$row = mysql_fetch_array($result);
$comment_post_ID = $row["comment_post_ID"];
$comment_author = $row["comment_author"];
$post_title = $row["post_title"];
if (strlen($comment_author)>10){
$comment_author=substr($comment_author,0, ."..";
}
if (strlen($post_title)>1 {
$post_title=substr($post_title,0,16)."..";
}
// Display each record
echo "<tr><td><a href=\"index.php?p=$comment_post_ID&c=1#comments\">$comment_author: </a></td><td><a href=\"index.php?p=$comment_post_ID&c=1#comments\">$post_title</a></td></tr>";
$cur++;
}
?> |
|
Back to top |
|
 |
mikelittle
Joined: 11 May 2002 Posts: 374 Location: UK
|
Posted: Sat Dec 28, 2002 1:28 am Post subject: |
|
|
OK, try this...
Code: |
function last_five_comments() {
global $tablecomments, $tableposts;
global $dbhost, $dbusername, $dbpassword, $dbname;
$d4 = mysql_connect($dbhost, $dbusername, $dbpassword);
mysql_select_db($dbname, $d4);
$sql=" SELECT comment_post_ID,comment_author, post_title " .
" FROM $tablecomments, $tableposts " .
" WHERE comment_post_ID=ID ".
" ORDER BY comment_date DESC LIMIT 5";
$result = mysql_query($sql, $d4) or die("Couldn't execute query" .
mysql_error());;
while ($row = mysql_fetch_array($result)) {
$comment_post_ID = $row["comment_post_ID"];
$comment_author = $row["comment_author"];
$post_title = $row["post_title"];
if (strlen($comment_author) > 10){
$comment_author = substr($comment_author, 0, 8) . "..";
}
if (strlen($post_title) > 18){
$post_title = substr($post_title, 0, 16) . "..";
}
// Display each record
echo("<tr>" .
"<td><a href=\"index.php?p=$comment_post_ID&c=1#comments\">$comment_author: </a></td>" .
"<td><a href=\"index.php?p=$comment_post_ID&c=1#comments\">$post_title</a></td>" .
"</tr>");
}
}
|
and then to call it on your page...
Code: |
<?php include_once("b2config.php"); ?>
<table>
<?php last_five_comments(); ?>
</table>
|
I've taken out the counting loop, because the query already limits the results to 5 (actually it was 4 but the comment seemed to suggest you wanted 5), and used the fact that mysql_fetch_array() will return false when there are no more results. So this will work even if you have less than 5 comments.
Hope this helps,
Mike _________________ Mike Little
http://zed1.com/b2/
"Share what you know. Learn what you don't."
Last edited by mikelittle on Sat Dec 28, 2002 7:26 pm; edited 1 time in total |
|
Back to top |
|
 |
wteening
Joined: 12 Jul 2002 Posts: 71
|
Posted: Sat Dec 28, 2002 1:41 pm Post subject: |
|
|
Thanks! It works! I had to remove a space in the second part though:
<?php instead of <? php
For the people as dumb as me.
Check out the result:
http://www.krabbels.com |
|
Back to top |
|
 |
mikelittle
Joined: 11 May 2002 Posts: 374 Location: UK
|
Posted: Sat Dec 28, 2002 7:27 pm Post subject: |
|
|
Great! I'm glad to hear it.
I've fixed the space now.
Mike _________________ Mike Little
http://zed1.com/b2/
"Share what you know. Learn what you don't." |
|
Back to top |
|
 |
Aredubya
Joined: 19 Nov 2003 Posts: 5
|
Posted: Sat Nov 22, 2003 12:08 am Post subject: |
|
|
Hi folks,
I recently installed this hack on my b2-run site (http://blog.netho.net), and so far, it's working great. I did make one adjustment to the date/timestamp query some of y'all might find useful. I replaced it like so:
Code: | // Convert datetime to happiness
$arrDateTime = explode(" ", $comment_date);
$arrDate = explode("-", $arrDateTime[0]);
$arrTime = explode(":", $arrDateTime[1]);
$date = mktime($arrTime[0], $arrTime[1], $arrTime[2], $arrDate[1], $arrDate[2], $arrDate[0]);
$convertedDate = date("m-j, g:i:sa", $date);
|
This displays the timestamp as well like 11-21, 5:04:59pm. The time itself can be adjusted to however you like with the various PHP time values, handy chart available at http://www.php.net/manual/en/function.date.php. Hope this helps some of you who may wish to install this useful hack. Enjoy  |
|
Back to top |
|
 |
|
|
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
|