realjumper Posted March 9, 2006 Share Posted March 9, 2006 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 Quote Link to comment Share on other sites More sharing options...
wickning1 Posted March 9, 2006 Share Posted March 9, 2006 If you don't want the page to reload, you have to write it in Javascript, or use Javascript to pull off an AJAX maneuver.Not to put too sharp a point on it, but there's another forum for that. :) Quote Link to comment Share on other sites More sharing options...
realjumper Posted March 9, 2006 Author Share Posted March 9, 2006 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 Quote Link to comment Share on other sites More sharing options...
psyion Posted March 9, 2006 Share Posted March 9, 2006 [!--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. Quote Link to comment Share on other sites More sharing options...
wickning1 Posted March 9, 2006 Share Posted March 9, 2006 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. Quote Link to comment Share on other sites More sharing options...
realjumper Posted March 9, 2006 Author Share Posted March 9, 2006 Thanks very much for clarifying that.....ORDER BY rand() wins the prize for what I'm doing!!Cheers,Neil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.