Jump to content

To Loop or not to Loop?


Go to solution Solved by QuickOldCar,

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/292715-to-loop-or-not-to-loop/
Share on other sites

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.

  • Solution

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 by QuickOldCar

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!

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.