eugeniu Posted July 10, 2009 Share Posted July 10, 2009 I have an array with many entries that contains the fields "romaji", "group", "hiragana", and "katakana". The field "group" contains a number between 1 and 11. What I want to do is create a script that can select a random entry with a specific "group" field, add it to a new array, and delete it from the main array. I then want to select three more random entries with no specific group (even if the group # is the same as the first entry), and add them to a new database. How would I be able to do that? I retrieve this array from a database, so if there's also a way I can do this directly from the database to two arrays, then please let me know. Thank you for your time. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 10, 2009 Share Posted July 10, 2009 yeah, i would do it from the DB level. your select statement would look something like: select 1 random entry from a specific group: SELECT * FROM tableName WHERE `group` = 'foobar' ORDER BY RAND() LIMIT 1 select 3 random entries from any group: SELECT * FROM tableName ORDER BY RAND() LIMIT 3 if you don't want the one from the first query to be in the 3 random ones, just add a WHERE clause to exclude it Quote Link to comment Share on other sites More sharing options...
eugeniu Posted July 10, 2009 Author Share Posted July 10, 2009 Ok, thank you. Now for the 3 random entries, I don't want the first entry to ever be selected, so what would be the "!=" equivalent for a mysql query? Also, I'm getting an error when I try to use WHERE in my query: $good = mysql_query("SELECT * FROM kana WHERE group=$g ORDER BY RAND() LIMIT 1") or die(mysql_error()); You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group=12 ORDER BY RAND() LIMIT 1' at line 1 This usually doesn't happen, and wrapping any ", `, or 's around it seems to make it worse. Edit: For reference, when I take out the "WHERE group=$g" and print_r the array, I get... ( [0] => se [romaji] => se [1] => 3 [group] => 3 [2] => せ [hiragana] => せ [3] => セ [katakana] => セ ) Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 10, 2009 Share Posted July 10, 2009 First, what is the Unique ID field for the table? You will need that for the second query Second, you should REALLY change the name of your column to something besides "group". GROUP is a reserved word in MySQL. But, the backticks should solve that: $good = mysql_query("SELECT * FROM kana WHERE `group`='$g' ORDER BY RAND() LIMIT 1") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
eugeniu Posted July 10, 2009 Author Share Posted July 10, 2009 I think I finally got this mess sorted out. Apparently != is just fine for mySQL . Thanks. This is the final code: $good = mysql_query("SELECT * FROM kana WHERE `group`='".$groups[$g]."' ORDER BY RAND() LIMIT 1") or die(mysql_error()); $good = mysql_fetch_array($good); $bad = mysql_query("SELECT * FROM kana WHERE `romaji`!='". $good['romaji'] ."' ORDER BY RAND() LIMIT 3") or die(mysql_error()); while ($r = mysql_fetch_array($bad)) { $badar[] = $r; } 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.