Jump to content

how to select random without having duplicates?


brem13

Recommended Posts

hey, i'm selecting users randomly from a database and displaying them in a table but i'm getting duplicates, is there any way to overcome this?

code is

$i = 1;
					while($i < 4)
					{

						echo "<td width=225px height=125px align=left valign=top>";
						include("config.php");
						mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); 
						$result = mysql_db_query($database, "select * from $table ORDER BY rand()") or die (mysql_error());
						while($qry = mysql_fetch_array($result))
						{ 
							$user = $qry['username'];
							$pic = $qry['mainPic'];								
							$lastOnline = $qry['lastLogin'];
							$lookingFor = $qry['lookingFor'];
						}//end while
						list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic);

						if($height>=101)
						{
							echo "<a href=viewProfile.php?user=$user><font face=tahoma size=2>$user</font><br><img src=userImages/thumbs/$pic height=75px></a><br>";
						}//end if $height
						else
						{
							echo "<a href=viewProfile.php?user=$user><font face=tahoma size=2>$user</font><br><img src=userImages/thumbs/$pic width=75px></a><br>";
						}
						echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>";
						echo "</td>";

						$i++;
					}//end while

Another thing you can try is modify your DB so that it has a column like "used" with a default value of 0. When that user is used you set the value to 1. Then in your query, you just check to see if "used = 0".

 

Just a thought. But, then again, you would have to change the query if you wanted them to be displayed again . . .

ok, i'm trying to put the users into an array and use the unique option to filter them but i'm not sure i'm doing it right because its still giving me duplicates

modified code to this, the array is under the sql while loop

while($i < 10)
					{
						if($i == 4 || $i == 7)
							echo "</tr><tr>";

						echo "<td width=225px height=125px align=left valign=top>";
						include("config.php");
						mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); 
						$result = mysql_db_query($database, "select * from $table ORDER BY rand()") or die (mysql_error());
						while($qry = mysql_fetch_array($result))
						{ 
							$user = $qry['username'];
							$pic = $qry['mainPic'];
							$lastOnline = $qry['lastLogin'];
							$lookingFor = $qry['lookingFor'];
						}//end while
						list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic);
						$users = array($user);
						$distinct = array_unique($users);

						if($height>=101)
						{
							echo "<a href=viewProfile.php?user=$user><font face=tahoma size=2>$distinct</font><br><img src=userImages/thumbs/$pic height=75px></a><br>";
						}//end if $height
						else
						{
							echo "<a href=viewProfile.php?user=$user><font face=tahoma size=2>$distinct</font><br><img src=userImages/thumbs/$pic width=75px></a><br>";
						}
						echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>";
						echo "</td>";


						$i++;
					}//end while

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.