brentech Posted July 9, 2008 Share Posted July 9, 2008 /* Get Names */ If (rand('1', '10') < { $nat = $u_a["tloc"]; } else { $foreign = array(1 => 'Brazil', 'Canada', 'Japan', 'Mexico', 'Russia', 'Thailand', 'U.S.A.'); $nat = $foreign[rand('1', '7')]; } $fq = "SELECT name FROM firstnames WHERE nat = '$nat'"; $result = mysql_query($fq, $conn); So the last 2 lines are what we're looking at here. I'm doing a query which returns all the first names in my table that holds ID|NAME|NAT (nationality). The code above is deciding which nationality the name should be coming from...based on an % chance that generated person doesn't come from the same country as the team. (kinda meaningless to my real question, but background always helps) My problem is with randomizing the selection of names. I can't seem come across a great way go about what I need done - and I feel like it should be simple. So I'm wanting to select all the names which would be set to the particular nationality: $fq = "SELECT name FROM firstnames WHERE nat = '$nat'"; I couldn't find a MySQL function that could randomize from my select criteria, so I began thinking that it would be easier to do this by storing all the names of that nationality into an array and picking a random element of the array - which would be the name.....then, I ran into the problem of not being able to find a PHP function that would store more than a single row's values into an array. I feel like I'm completely overlooking a function, or just WAY over-thinking the solution to my problem. A friend at work suggested creating MySQL table views, but I'm not real familiar with those, or if that would be the best answer. Anyone have any tips or suggestions on functions/methods? Thanks, -Brent Quote Link to comment Share on other sites More sharing options...
brentech Posted July 9, 2008 Author Share Posted July 9, 2008 Was definitely making things way too complicated. Edited my query to randomize the order and limit the result to 1 row...so it just returns the one name as need. Thought I'd share my solution. $fq = "SELECT name FROM firstnames WHERE nat = '$nat' ORDER BY RAND() LIMIT 1"; $result = mysql_query($fq, $conn); $name = mysql_fetch_row($result); $name[0] becomes the value I needed. 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.