Jump to content

Would like to display random row from database table


Dirty Butter

Recommended Posts

I have always shown a scripture verse at the bottom of our pages. And I've always wanted some way to randomize, or at least automatically change, which verse was shown, but never felt like I could accomplish that. I still don't know what to do, but just maybe I have reached the point where I could follow someone's directions to accomplish this.

In preparation for any directions I hopefully can follow, I have populated a new CubeCart_scriptures table with 2 rows:

scripture_id index int(11) with autoindex

scripture_verse utf8_unicode_ci text

Right now there are 11 entries in the verse row, but I would hope to add more later.

I'm planning at this point to add whatever code is needed to the ccpower.php file, if that makes sense.

So - am I asking for something difficult, fairly easy to do, or maybe impossible???

Link to comment
Share on other sites

I couldn't resist working on this again on my own before giving up and emailing Daren. I was able to search and work out a query that does return a random row.

SELECT * FROM `CubeCart_scriptures` WHERE scripture_id >= (SELECT FLOOR( MAX(scripture_id) * RAND()) FROM `CubeCart_scriptures` ) ORDER BY scripture_id LIMIT 1

But I wasn't able to understand how to then select just the scripture_verse and get it on the page.

Link to comment
Share on other sites

The SELECT * is returning all columns of one record in an associated array of an indexed array:

[0] => ['scripture_id' of row 0] = somenumber, and ['scripture_verse' of row 0] = somephrase
 

So, the query results are being assigned to a variable,such as $scripture_random_record. To display what you want, use the variable this way:
$GLOBALS['smarty'] -> assign('RAND_VERSE', $scripture_random_record[0]['scripture_verse']);
 

Link to comment
Share on other sites

I think I was misleading - the query I came up with did not come from SFWS's code, but from searching the web. I have emailed Daren. Thank you for your help so far. Even my feeble attempts, though unsuccessful, teach me a little, thanks to your patience in explaining how things work.

Edited by Dirty Butter
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...