Jump to content

[SOLVED] Need some help on selecting from and removing entries from arrays.


eugeniu

Recommended Posts

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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] => セ
)

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

I think I finally got this mess sorted out. Apparently != is just fine for mySQL :P. 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;
}

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.