1

I have a SQL database that contains a field. I can get all of the elements in that field by:

      $result=mysql_query($sql);
      while($row=mysql_fetch_array($result)){
         $ftype  =$row['ftype']; //name of field is 'ftype'
         print $ftype}  //do something  

I want a random element to be printed out each time the page is opened/refreshed.

Knowing that the result is an array containing the information i want, I want to randomly choose an element of the array. Also, if there are N elements in the array, I want to choose all N elements exactly once before I show any element again for second time. The process repeats itself. I know how to code something like this in java or python, but I don't think that's the way I should go.

I think I need to use javascript, but I'm just not sure what technology to use.

UPDATE:

Patrick's idea seems to be exactly what i was looking for. I thought I would share what I have now and maybe you can suggest optimization. I hope the intent in the code is obvious.

<?
session_start();
if (!isset($_SESSION['count']) || !isset($_SESSION['randomArray'])) {
      $count = 0;
      $randomArray = array();
      $sql="SELECT youtubeurl FROM Foodlist";
      $result=mysql_query($sql);
      while($row=mysql_fetch_array($result)){
           array_push($randomArray,$row['youtubeurl']);
      }
      shuffle($randomArray);
      $_SESSION['randomArray'] = $randomArray;
      $_SESSION['count'] = $count; 
} elseif ($_SESSION['count'] >= sizeof($_SESSION['randomArray'])){
   $_SESSION['count'] = 0;
   $randomArray = $_SESSION['randomArray'];
   shuffle($randomArray);
   $_SESSION['randomArray'] = $randomArray;
} else{
   $randomArray = $_SESSION['randomArray'];
   $count = $_SESSION['count'];
   echo $randomArray[$count];
   $_SESSION['count']++;
}
?>
1
  • I don't think this is going to involve Javascript! Commented Jun 23, 2011 at 0:26

2 Answers 2

1

You can do this without Javascript, however you'll need to open/maintain a session.

Pseudocode:

data = data_from_mysql()

choice = random.choice(data, exclude = SESSION['choices'])
SESSION['choices'].append(choice)

print choice

if len(SESSION['choices']) == len(data):
    SESSION['choices'] = []
Sign up to request clarification or add additional context in comments.

6 Comments

i dont know anything about sessions. can you point me to some good information so that I know i'm looking at the right stuff?
Absolutely. Check this out, it's the PHP manual for Sessions. They're pretty straightforward.
Do you think there is a more server-sided approach to this? I don't really want to create cookies unless I have to.
This is about as server-sided as it gets. There are no cookies necessitated by sessions (although they are often used in tandem). session.start(); if(!is_set($_SESSION['choices'])) { $_SESSION['choices'] = array(); } should be all you need to get started. From there, just access $_SESSION['choices'] like any other array.
I forgot i was in PHP for a minute.
|
0

If you want a random result let the dbms take care of it. Add this to the bottom of your sql query.

ORDER BY RAND()
LIMIT 1

1 Comment

that would work, but I have a constraint on the amount of times a result can appear randomly. everything has to appear at least once before it can show up again. I bet you can't guess what type of content would need this kind of structure! (Hint: It's ads)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.