dtyson2000 Posted November 25, 2014 Share Posted November 25, 2014 Hello. I have a simple SQL query that returns a list of names, this is good and all is right with the world. I would like to associate a number with each person, a RANDOM number that will change each time I call the page. Desired result: Number | Name 18 | Jeff 2 | Tammy 12 | Steve 10 | Phil 5 | Michelle etc. etc. Pool of numbers will be from 1 to 30. Sometimes the names pool will result in 18 rows or 27 rows or any number in between. I just need to assign a number from 1 to 30 to each name that is not the same each time. Here's my loop. while ($row= mysql_fetch_array($result)) { $lastname = $row["lastname"]; $firstname = $row["firstname"]; $numbers = range(1, 30); shuffle($numbers); foreach ($numbers as $number) { } echo "<tr> <td class='results'>$number</td> <td class='results'>$firstname $lastname</td> </tr>"; } Here's the problem I'm encountering: 1) If I leave the FOREACH inside the loop, I wind up with one or two repeated $number(s). 2) If I take the FOREACH outside the loop, I wind up with the first integer from the $numbers range repeated as many times as there are names I'm just not sure how to achieve a unique designation for each name. Could someone point me in the right direction? Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/292715-to-loop-or-not-to-loop/ Share on other sites More sharing options...
mac_gyver Posted November 26, 2014 Share Posted November 26, 2014 all the foreach() loop is doing is giving you the last element in the array. to eliminate duplicates, you need to remove each number from the array as you use them. array_pop() would be a good choice to both get the last element from the array and remove it at the same time. you would put your range() and shuffle() statements before the start of your while(){} loop. Quote Link to comment https://forums.phpfreaks.com/topic/292715-to-loop-or-not-to-loop/#findComment-1497686 Share on other sites More sharing options...
Solution QuickOldCar Posted November 26, 2014 Solution Share Posted November 26, 2014 (edited) As mac_gyver stated, but a different approach. $numbers = range(1, 30); shuffle($numbers); $i = 0; while ($row= mysql_fetch_array($result)) { $lastname = $row["lastname"]; $firstname = $row["firstname"]; echo "<tr> <td class='results'>$numbers[$i]</td> <td class='results'>$firstname $lastname</td> </tr>"; $i++; } Edited November 26, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/292715-to-loop-or-not-to-loop/#findComment-1497687 Share on other sites More sharing options...
dtyson2000 Posted November 26, 2014 Author Share Posted November 26, 2014 That's the ticket. I swear I tried something similar but messed something up. I think it was the [$i] in the echo. I think I recall doing it but just putting , which threw an error of course. Then I became so "cornfused" that I tried 17 different things before asking. Thank you so much for your time and for enlightening me! Quote Link to comment https://forums.phpfreaks.com/topic/292715-to-loop-or-not-to-loop/#findComment-1497688 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.