Servus, folks.

I want a floating block showing a random quote. I can't figure how to get a random selection from these CCK records called 'quotes' using the view.module... I found some notes that the former "random node" option was dropped in spring because of some new feature, but I couldn't find any further hint. The online help and some docs still say chosing "random node" should do the trick, but that's gone now.
So how do I select a random record from CCK using view?

TIA,

Norbert

Comments

sillygwailo’s picture

Really? I have it on one of my sites, which has the latest version of the module for 4.7. Here's how I did it (instructions may be verbose, but this may be useful for those new to Views and searching for how to display a random post in a block):

  1. create a new view in administer » views » add tab.
  2. give it a name. I called mine "Random".
  3. click the "Block" section heading to get its settings.
  4. enable "Provide block"
  5. choose a "View type". I went with "List View" so that I could choose which fields display in the block.
  6. type in a title. This will be the title of the block.
  7. type in a number for the amount of nodes you want to display in the block. You can use 1 (number one) if you like.
  8. click the "Fields" section heading to get its settings.
  9. add the fields you want. I chose just "Node: Title" since I wanted just links to the nodes, not the node text themselves. You should be able to choose whichever field has the quote in your CCK node.
  10. click the "Filters" section heading to get its settings.
  11. select "Node: Type" and click "Add Filter"
  12. choose the CCK node type(s) you want to appear in the block.
  13. select "Node: Published" and click "Add Filter". (I keep forgetting this one, but it's important, since you probably don't want to display unpublished nodes.
  14. click the "Sort criteria" section heading to get its settings.
  15. select "Random" and click "Add criteria".
  16. click "Save"
  17. click administer » blocks and enable the block you titled in step #6.

(Username formerly my full name, Richard Eriksson.)

trevortwining’s picture

That was on my list of things to figure out this week. Scratch it off now! Thanks a bunch.

Trevor Twining
Freelance Drupal Dev
Theme/Modules/Sitebuilding

nofue’s picture

Works like a charm -- you made my week.

Norbert

-- form follows function

rich.yumul’s picture

I just wanted to add my thanks for this little how-to. THANKS!

Rich Yumul
www.sagetree.com

sillygwailo’s picture

I simplified this how-to a little, adding the PHP that views exports to save a little time, in a handbook page for linking to a random blog post in a block.

(Username formerly my full name, Richard Eriksson.)

chrissearle’s picture

This works extremely well - with one tiny little problem (there's always one who complains isn't there).

The block section of a view allows for checking a checkbox which will then go to the page view of the same view. This is useful - it keeps the same filters etc etc - but - of course it also keeps the random sort.

Is there any approach which will allow for the block sort order to be random but to get a link to a page view where the sort is something else?

I'm wondering about contemplate module and manually adding the link in the template (having two views, one for the block, one for the page) - but is this the right way?

kweisblatt’s picture

I did this as well because of a few issues with contemplate recently. But, I didn't see the "random" option which is great! I want it to look more like a teaser and give the view a length so it will cut off after so many lines. I tries changeing the block height but it didn't work. Any ideas?

thomasmuirhead’s picture

Thanks Richard. Generally people aren't willing to make a step by step tutorial for something that seems so simple. It helps so much. Thank you.

pharma’s picture

Excellent step by step guidelines

binford2k’s picture

In views2, simply add the Sort criteria of Global:Random

dgoldenbaum’s picture

thanks for the Views2 addition. big help.

pharma’s picture

Thanks binford2k . That short tip was a saving of 2 hr stress

tiwiex’s picture

I was about to pull my hair out when i saw your post. I was using views 2 and i did not see random at first. Thanks alot. I wish we had quick tips like this for themes. Themes always gives me a head ache. Especially css stuff. ahhhh.

newtoid’s picture

just used this method on a page view, cheers dude

asfahaan’s picture

Excellent tutorial works really well.

Just wanted to know:

Is it possible to make the block refresh via javascript or some other means? E.g. every 5 seconds another random quote appears, etc... ? Is this possible wiht viewS?

vm’s picture

investigate the views cycle module or one of the other views addons that may do what you ask.

Jenechka’s picture

I dont like this variant. Look at the query

SELECT DISTINCT(node.nid) AS nid,
   RAND() AS _random
 FROM node node 
 WHERE node.type in ('node')
   ORDER BY _random ASC

IT SELECTS ALL NODES!!! But i have 7000 of nodes and for one random do this query? nooo... You need something like this

mattsplatw’s picture

Is there a way in views to select a random set of content that meets the filter criteria, and then apply a different sort? For example, I would like to select a random set of something and then sort the selected set by name.

jaypabs’s picture

Thanks for the tutorial. It saves me a lot of time...

:)

markpetherbridge’s picture

Thank You

This solved my problem and works a treat! :)

carlito’s picture

thanks richard, works a treat :)

e3ekiel’s picture

I've just tried this method. It works fine when im logged in as admin, but when im logged out and viewing the page as any normal person would the random function doesnt work.

Any ideas?

aliak’s picture

I have same problem. did you find out what it was?
I tried refreshing the page
I'm using drupal 6

gost_’s picture

and don't forget that it is always a good idea to exclude current node, so the page you are viewing the random block on doesn't show up in your block!

Drupal 5 http://drupal.org/node/131547

Drupal 6 requires no code! another reason to switch :)

To exclude the current node from the view:

1) Add another argument = Node:Nid
2) Select "Action to take if argument is not present" = "Provide default argument"
3) Select "Default argument type" = "Node ID from URL"
4) Select "Exclude the argument"

source:drupaleasy.com

izmeez’s picture

This is very helpful,

Thanks,

Izzy

summit’s picture

Bookmarking, anyone knows how to deal with this using the taxonomy term as argument, so showing random nodes in a block related to the term in the url (taxonomy/term/%) ?

Thanks a lot for your reply!
Greetings,
Martijn

Gary Coleman’s picture

I have a tricky question
Is there any way to make the randomizing content-type a block instead of a node?

Because my blocks are grey and my nodes are white with rounded edges
When I follow these instructions my grey block shows the randomizing article nodes with the node with the white background inside it

http://img197.imageshack.us/img197/9240/croppedoffadproblem.jpg

It also pushes the ad off the right edge of the page because of the left margin space
And it puts the node comment count below it which is no good

In blocks I can copy and paste the ad html and it fits perfectly centered and doesn't put a different background color, but I don't know how to randomize it

Is there any way to randomize ads in a block with views?
Or is there a way to set the content-type to be a block instead of a node?

binford2k’s picture

That's a styling question, not a Views question. Install firebug, then inspect your block. It will quickly become obvious how to style the block to achieve what you want.

That said, there are several ad managing modules here, which depending on context may already do what you want. You might check to make sure you're not re-inventing the wheel.

AlviSyhrn’s picture

Can it be used for Drupal 7.12?

I am dying to create a randomly quote every time it's loaded, but I never know how to. Anyone here can help me, please? Explain step by step? :)

I am so thankful to anyone who want to help us!

vm’s picture

create a content type to store quotes in the DB
create a view, create a display in the view, filter the quote content type in the view, set the sort to random, save the view.

visit your new display

if you want it in a block be sure to create a block display

stratos’s picture

Random sort criteria works great! Thanks for that simply solution!

But i cant figure out some additional thing. How to exclude from this block with random nodes current displayed node. I mean i dont wanna display randomly nodes in block when user is visiting that specific node. I hope thats clear. Somefing like filter for NID: 'is not equal to current NID from URL '

Thank for your help!

mcfilms’s picture

I believe this IS possible in Views. Unfortunately I am only going to supply some hints because I haven't done it. (But at least it is better than just saying "subscribe" because I am interested in the reply.)

My understanding is you can add a "relationship" (in the third column of the Views UI). You can set this to Node ID and I think there is some is/is not type logic here.

In Views 2 a Block View could not get information from the url, but I think in Views 3, this is what the relationship allows. Please let me know if that helps at all.

A list of some of the Drupal sites I have designed and/or developed can be viewed at motioncity.com

stratos’s picture

reletionship, but which and how? i have no idea.

I tried contextual filter: Content: Nid, but i am able only to do something opposite to what i need ;-) I can use option "Provide default value: Content ID from URL. This way i can display exactly the same node i am currently visiting. But maybe using this contextual filter it is possible to do it somehow with PHP Code filter? Anyone can provide php code for that task?

mcfilms’s picture

You are so close, I think. Isn't there a check box in the contextual filter: Content: Nid that allows you to invert the results?

A list of some of the Drupal sites I have designed and/or developed can be viewed at motioncity.com

stratos’s picture

Your clue was very helpful and you are totally right. There is an option 'Exclude':

Exclude
If selected, the numbers entered for the filter will be excluded rather than limiting the view.

In the last section 'more' of the Contextual filter: Nid, under the administrative title.

Also useful hint i found here: http://drupal.stackexchange.com/questions/17284/inverse-of-drupal-contex...

So, i think question in this thread is solved :)

mstornelli’s picture

Muchas gracias por compartir, me ha servido justo para lo que necesitaba.

Importante indicar en el parámetro de cache en las opciones avanzadas "NONE" de la vista.

Saludos