Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Combining Php snippets to display date separator in Drupal group blog

Have a crack at this one!

   
7:27 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



Hi there -- A PHP question for your infinitely erudite perusal :-)

I've not had much luck with it on the D forums as yet.

The question is how to display aggregated blog posts (cf. Metafilter) like this:


WEDNESDAY, MARCH 22nd
> Node 1
> Node 2
> Node 3
TUESDAY, MARCH 21st
> Node 4
> Node 5
> Node 6

etc

Here's a php snippet [drupal.org] to display recent blog posts by date in descending order.


<?PHP
/**
* This php snippet displays the 10 most recent weblog entries with
* teaser & info.
*
* To increase/decrease the number of weblogs listed
* change the list_length field to suit.
*
* Works with drupal 4.6
*/
$list_length="10";
$result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), variable_get('default_nodes_main', $list_length));
while ($node = db_fetch_object($result1)) {
$output .= node_view(node_load(array('nid' => $node->nid)), 1);
}

print $output;

?>

AND, here's Drupal user Eaton's isolation [drupal.org] of the date header:


function date_header($timestamp, $format = 'F j, Y') {
static $last;
$date = format_date($timestamp, 'custom', $format);
if ($date!= $last) {
$last = $date;
return $date;
}
}

So how can I combine the two into a single snippet to return in descending order, date-separated blog entries from multiple users, preferably paging entries after a certain threshold value?

(Try saying that without breathing!)

Case.

11:58 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



there are quite a few custom functions in there

given that I am no fan of drupal and have no desire to dig into it's code I have some guesses and need more info

you should be able to play with this loop

while ($node = db_fetch_object($result1)) {
$output .= node_view(node_load(array('nid' => $node->nid)), 1);
}

change the format it pulls in. Instead of just appending to a var that is printed, use an array

what is pushed into $output?
is there a date somewhere I can sort on?
if you use an array then you could just sort the array

>> n.nid, n.created

id and created, is created the date we want to send through the date_header function?

1:48 am on Mar 22, 2006 (gmt 0)

5+ Year Member



>> no fan of Drupal

Thanks for the pointers though. I'll do some digging and come back with more info. I remain optimistic that someone else has come across this little problem before, hence my post.

11:03 pm on Mar 27, 2006 (gmt 0)

5+ Year Member



Yes --
created
is the value that needs to be pushed to the
date_header
function. It basically needs to check the query results to see if there is a new date, and if so, print it between results. If not, it needs to continue outputting the results.

I'm finding it quite difficult to get my head around it - how could it be refactored into an array?

11:18 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



we could start by analyzing passed data otherwise we don't know what we are working with

this bit needs to change

while ($node = db_fetch_object($result1)) {
$output .= node_view(node_load(array('nid' => $node->nid)), 1);

therefore we need to understand everything involved

we can start by stepping it out to see each piece

while ($node = db_fetch_object($result1)) {
echo '<p>node:<pre>';
print_r($node);
echo '</pre>';

$nloadarr = node_load(array('nid' => $node->nid))
echo '<p>nloadarr:<pre>';
print_r($nloadarr);
echo '</pre>';

$nview = node_view($nloadarr, 1);
echo '<p>nview:<pre>';
print_r($nview);
echo '</pre>';

$output .= $nview;
echo '<p>ouput',$output;
die();
}

this means everything will still work (though you would have to comment the echos and the die) but we can look at each step. This may spit out a ton of data so running it a single time is probably preferable which is why I added a die at the end.

run that once, document all output, then comment the echos and the die if this isn't a dev environment. You can then see what you are working with. It may require you to just comment the die and let it dump on every iteration of the loop so you can see how data is changed as the loop progresses.

You can paste the result from the single iteration here (I am hoping it isn't that long ;)).

11:47 am on Mar 28, 2006 (gmt 0)

5+ Year Member



Hi Jatar_K -- Thanks for taking the time to split that expression apart for me. I can see the logic quite clearly, but Drupal's not outputting any data from the expression.
4:00 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



it's not showing any data for any of those echo's or print_r's?

when you view source with the debug code in there can you see the <pre>'s and text?

4:22 pm on Mar 28, 2006 (gmt 0)

5+ Year Member



No, completely blank in source. Time to pay a Drupal developer, methinks. Thanks for the help.
8:54 pm on Apr 11, 2006 (gmt 0)

5+ Year Member



Finally cracked it on my lonesome:


<?php
$listlength="15";
$content_type = 'blog';
unset ($output);
$prev_day = ' ';
$result1 = pager_query("SELECT n.title, n.nid, n.created FROM {node} n WHERE n.type = '$content_type' AND n.status = 1 ORDER BY n.created DESC", $listlength);
while ($node = db_fetch_object($result1)) {
$day = format_date($node->created, 'custom', 'l, j F');
if ($day!= $prev_day) {
$output .= "<h1>$day</h1>";
$prev_day = $day;
}
$output .= "<li>" . l($node->title, "node/$node->nid") . "</li>";
}
$output .= theme('pager', NULL, $listlength);
print $output;
?>

Thanks again Jatar_k

9:06 pm on Apr 11, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



nice work