By Pushkar Gaikwad on
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
Maybe this?
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
This seems to work, too...
Personal: justinstanley.net
Nerdly: nerdliness.com
You are the Man buddy, you
You are the Man buddy, you are the man..working like charm... :)
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.
Ok, now after getting the
Ok, now after getting the body, I am trying to put html to it like making it BOLD. Here is my code
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
=-=
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 : )
Missing {'s
First, you'll probably want to go VeryMisunderstood's route.
But... you're missing the { } for the if block:
Personal: justinstanley.net
Nerdly: nerdliness.com
[ edited by: VeryMisunderstood; Added code tags so HTML would be readable ]
Yep the code is working :)
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
=-=
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 : )
The views module is
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.
Unfortunately, Views 2.0 is
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.