Well with code I am able to get a random node

<?php
$result = db_query("SELECT nid FROM {node} WHERE type = 'page' ORDER BY RAND() LIMIT 1");
if ($node = db_fetch_object($result)) {
  print(node_view(node_load(array("nid" => $node->nid, "type" => "page")), 1));
}
?>

and able to display in a block but I want to get only the BODY and not the whole node info. How can I extract body from this code ?

Thanks

Comments

eljustino’s picture

We use Views and CCK to do something similar on Nerdliness.com. We have a custom type called "random_goodie" and a view that creates the block that randomly selects one of the pieces of content with that type.

Personal: justinstanley.net
Nerdly: nerdliness.com

eljustino’s picture

$result = db_query("SELECT nid FROM {node} WHERE type = 'page' ORDER BY RAND() LIMIT 1");
if ($node = db_fetch_object($result)) {
  print(node_load(array("nid" => $node->nid, "type" => "page"))->body);
}

Personal: justinstanley.net
Nerdly: nerdliness.com

Pushkar Gaikwad’s picture

You are the Man buddy, you are the man..working like charm... :)

Now what does node_view and node_load are for ?

mooffie’s picture

Now what does node_view and node_load are for ?

They are needed because fetching the 'body' column directly from the database is not enough --for two reasons:

- The 'body' column is the raw text the user typed. It needs some further processing, aka filtering, e.g. converting URLs to links and adding <P> tags.

- Some modules may add data to the body, e.g. CCK fields.

Pushkar Gaikwad’s picture

Ok, now after getting the body, I am trying to put html to it like making it BOLD. Here is my code

<?php
$result = db_query("SELECT nid FROM {node} WHERE type = 'page' ORDER BY RAND() LIMIT 1");
if ($node = db_fetch_object($result)); 
?>
  <b><?php print (node_load(array("nid" => $node->nid, "type" => "page"))->body);
 ?></b>
<?php endif; ?>

but it is giving sql error -
Parse error: syntax error, unexpected T_ENDIF in /home/example/public_html/includes/common.inc(1347) : eval()'d code on line 7

vm’s picture

you should probably get the blocks delta and use CSS to style.

Another possible method may be its own .tpl.php for that block.

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

eljustino’s picture

First, you'll probably want to go VeryMisunderstood's route.

But... you're missing the { } for the if block:

<?php
$result = db_query("SELECT nid FROM {node} WHERE type = 'page' ORDER BY RAND() LIMIT 1");
if ($node = db_fetch_object($result)) {
?>
<p><strong><?php print (node_load(array("nid" => $node->nid, "type" => "page"))->body);

?></strong></p>
<?php 
} ?>

Personal: justinstanley.net
Nerdly: nerdliness.com

[ edited by: VeryMisunderstood; Added code tags so HTML would be readable ]

Pushkar Gaikwad’s picture

Yep the code is working :)

Honestly I didn't understood what verymisunderstood said about CSS and tpl.php! I will really appreciate if someone can explain me that or can point me to some related articles, Thanks.

Also is using too many such direct queries slows down loading my website ? because now since I have understood how to construct such queries, I am planning to use lot of them in blocks to get lot of customization.

Regards

vm’s picture

The more querries you use the more querries needed to run the site. That said, you have to watch for MySQL bottlenecks.

More information on custom themeing using tpl.php files in your theme can be found in the handbook -> theme developement section.

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

benkenney’s picture

The views module is definitely the one you want. Here's a how-to on how to make a random node show up on a page: http://drupal.org/node/161292 it's easy to modify this into making a random node show up in a block.

mrtoner’s picture

Unfortunately, Views 2.0 is not feature complete at this point and does not support the Random sort criteria, so eljustino's suggestion seems to be the best way to go right now for those using Drupal 6.x.