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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.