Forum Moderators: coopster
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 6etc
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.
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?
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?
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 ;)).
<?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