Jump to content

ORDER BY = random?


Guest

Recommended Posts

hmmm that site you gave me won't work

 

 

can you give me an example how i could randomize this query? It's currently ordered by ids

 

$f5 = "$bbuserinfo[field5]";
$query = "SELECT * FROM quebec_userfield WHERE field5='$f5' ORDER BY userid DESC";
$res = mysql_query($query) or die(mysql_error());  
while($row = mysql_fetch_array($res)) { 
$id = $row["userid"];

$query = "SELECT username, userid FROM quebec_user WHERE userid=$id ORDER BY userid DESC";
$res = mysql_query($query) or die(mysql_error());  
while($row = mysql_fetch_array($res)) { 
$nom = $row["username"];
echo "<a href=\"member.php?username=$nom\">";
echo $nom;
echo "</a><br>";
}

}

Link to comment
https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513667
Share on other sites

strange, try www.php.net/shuffle

 

also - try this:

 

while($row = mysql_fetch_array($res)) { 
   $res[] = $row;
}
shuffle($res);
foreach($res As $row){
$id = $row["userid"];

$query = "SELECT username, userid FROM quebec_user WHERE userid=$id ORDER BY userid DESC";
$res = mysql_query($query) or die(mysql_error());  
while($row = mysql_fetch_array($res)) { 
$nom = $row["username"];
echo "<a href=\"member.php?username=$nom\">";
echo $nom;
echo "</a><br>";
}

 

may be long way around but should work.

Link to comment
https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513674
Share on other sites

Opps i missed an ending parenthesis, try this:

 

$f5 = "$bbuserinfo[field5]";
$query = "SELECT * FROM quebec_userfield WHERE field5='$f5' ORDER BY userid DESC";
$res = mysql_query($query) or die(mysql_error());  
while($row = mysql_fetch_array($res)) { 
   $res[] = $row;
}
shuffle($res);
foreach($res As $row){
$id = $row["userid"];

$query = "SELECT username, userid FROM quebec_user WHERE userid=$id ORDER BY userid DESC";
$res = mysql_query($query) or die(mysql_error());  
while($row = mysql_fetch_array($res)) { 
$nom = $row["username"];
echo "<a href=\"member.php?username=$nom\">";
echo $nom;
echo "</a><br>";
}
}

 

replace the last code you gave me with this.

 

hope this helps,

Link to comment
https://forums.phpfreaks.com/topic/100445-order-by-random/#findComment-513773
Share on other sites

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.