Jump to content

Selecting random numbers from results


mattyvx

Recommended Posts

Hi,

 

I have various users on my site who each have a unique ID some of which have profile pages.

 

What i want to do is dynamically  create a hyperlink which when clicked will take me to a random members profile page, the profile page.php retrieves the user ID using $_GET and then displays the details for that user.

 

Not all users have profile pages, if they do the value y is stored in the Profile field of the database.

 

*NOTE i have the profile page up and running its more the random selection bit im after *

 

im trying the following:

 

<?php 

include 'myconnectionscript.php';  

$sql = "SELECT ID FROM Users WHERE Profile = 'y'";

$result = mysql_query($sql);       
if (!$result)       
die("Error");       

$row = mysql_fetch_array($result); 

$randID = array_rand(array_flip($row), 1);

?>
.....
<a href="profile/profile.php?ID=<?php echo $randID; ?></a>

 

which is only ever giving me one ID value.

 

Any ideas? Im sure its something simple...

Link to comment
https://forums.phpfreaks.com/topic/184126-selecting-random-numbers-from-results/
Share on other sites

<?php 

...

$sql = "SELECT ID FROM Users WHERE Profile = 'y' ORDER BY Rand() LIMIT 1";

$result = mysql_query($sql);       
if (!$result)       
die("Error");       

$row = mysql_fetch_array($result);

$randID = array_rand(array_flip($row), 1);


?>

 

seems to do the trick just nice! Solved

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.