homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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)

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:

> Node 1
> Node 2
> Node 3
> Node 4
> Node 5
> Node 6


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

* 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
$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!)




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

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)

>> 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)

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)

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>';
echo '</pre>';

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

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

$output .= $nview;
echo '<p>ouput',$output;

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)

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)

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)

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)

Finally cracked it on my lonesome:

$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)

nice work

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved