Jump to content

shuffle()


realjumper

Recommended Posts

Hi,

I have a html table of 4 rows and four columns and the table is poulated with a random array of 16 out of 20 possibilities. The data is Japanese Kanji characters. I also have little mp3's of the pronounciation of each character and the idea is that the user has to click the table cell that contains the Kanji character they think matches the sound they hear. When they get it correct, they will recieve a score of 1....then they will move onto the next sound out of the original 16, which they will have to match etc etc. When they get sick of this array of 16, they can go back and select a new test, which will repeat the process with a different random array of 16.

What I'm trying to is......Lets assume they get the first one correct.....or incorrect come to that. Now I need to play another sound from the same 16 for the user to match to a table cell. Is it possible to shuffle the array?

[code]
// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM kanji.temp")
or die(mysql_error());

// store the record of the table into $row
$row = mysql_fetch_array( $result99 );

srand ();
$myarray = array("$row[cpd_id_0]","$row[cpd_id_1]","$row[cpd_id_2]",
"$row[cpd_id_3]","$row[cpd_id_4]","$row[cpd_id_5]","$row[cpd_id_6]",
"$row[cpd_id_7]","$row[cpd_id_8]","$row[cpd_id_9]","$row[cpd_id_10]",
"$row[cpd_id_11]","$row[cpd_id_12]","$row[cpd_id_13]","$row[cpd_id_14]",
"$row[cpd_id_15]");
$myarray = $myarray[rand(0,count($doo)-1)];

echo "$myarray";

[/code]

When the page loads, the result of the echo "$myarray" might be say, 2.mp3 (cpd_id_1). That's great...but...how do I stay in the same page and 'replay' the above, so-to-speak, so that the user can randomly work their way through the entire 16 possibilities? I doesn't matter if, for example, cpd_id_2 gets called twice or even three times as long it is random.

Hope this is possible....or perhaps I'm looking at this wrong. I thought/think shuffle() could be the answer, but I don't know how to stay in the same page and 'shuffle' the array to get a new result. If it is possible, could the array shuffle be triggered by a button perhaps?

Thanks heaps,

Neil
Link to comment
Share on other sites

Okay, thanks for that. I was pretty sure that this wasn't going to fly, but I wanted to make sure that my understanding of the manual was correct. Rather than use JavaScript I'll go for the page reload option as this will be the smoothest way to go.

Just as an aside.....doesn't ORDER BY rand() do the same as shuffle()? If so, what's the point of having both? If not, what's the difference in the end result?

Thanks for your help,
Neil
Link to comment
Share on other sites

[!--quoteo(post=353360:date=Mar 10 2006, 03:59 AM:name=realjumper)--][div class=\'quotetop\']QUOTE(realjumper @ Mar 10 2006, 03:59 AM) [snapback]353360[/snapback][/div][div class=\'quotemain\'][!--quotec--]

Just as an aside.....doesn't ORDER BY rand() do the same as shuffle()? If so, what's the point of having both? If not, what's the difference in the end result?

[/quote]

err... one is supported by MySql and the other one is php???

but than this is what pop-up in dreamweaver:

[!--coloro:#FF9900--][span style=\"color:#FF9900\"][!--/coloro--]rand([int min], int max)

shuffle(array array_arg)[!--colorc--][/span][!--/colorc--]

hmmm, i know what is array. but, don't really know how to use them much.
Link to comment
Share on other sites

Shuffle really only makes sense if you're going to display more than one element from the array you're shuffling. If you're just returning one element, as it appears you are, then you are just picking a random element, not shuffling. Shuffling is like shuffling a deck of cards. You will never get two aces of hearts after shuffling a single deck.

If you want it to behave like a shuffle, i.e. you don't want to repeat anything, you'll have to come up with a solution for keeping persistent data (probably the data would be a full shuffled list and an index of where you currently are). You can store it with the user's session, put it in a cookie, or keep sending it through as form input.

To answer your aside, using ORDER BY RAND() on your query is exactly the same as using shuffle() on a PHP array. Doing it in MySQL is likely to perform better.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.