brem13 Posted June 2, 2010 Share Posted June 2, 2010 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 Quote Link to comment Share on other sites More sharing options...
brem13 Posted June 2, 2010 Author Share Posted June 2, 2010 any help please? i cant really find any info when i google it. Quote Link to comment Share on other sites More sharing options...
hcdarkmage Posted June 2, 2010 Share Posted June 2, 2010 I had a similar problem using random. I don't know if this helps but try looking at http://www.phpfreaks.com/forums/index.php/topic,149411.0.html that is the topic I started and it helped a lot. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted June 2, 2010 Share Posted June 2, 2010 Put the user into an array and then use array_rand($array, $number_needed); HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
hcdarkmage Posted June 2, 2010 Share Posted June 2, 2010 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 . . . Quote Link to comment Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 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 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.