Jump to content

RandID/shuffle


czukoman20

Recommended Posts

Ok i have a question

How would i implement this code that i wrote to use a rand id function.

The setup i would like to create is.

 

Randomly put 10 userid's in a lineup. pick the first random 5, and i display those five on a page, then when the user hits next page, it will exclude the five that it showed already, and display the last five.

 

Here is what i have written for the displaying part.

This setup below searches for the user with a userid of 1, then displays information on them

<?mysql_select_db('db215169417')
or die('Error, cannot select mysql database');
$userid_1 = 1;
$query = mysql_query("SELECT * FROM users WHERE userid_2='$userid_1'");
$values = mysql_fetch_array($query);
$nrRows = mysql_num_rows($query);
if($nrRows == 1){
   $username = $values['username'];
   echo " <img src=\"http://www.adworld-online.com/images/banners/$username"."_banner.jpg\"> <br>";
   echo ".::. Company: " . $values['companyname'] . "<br>  ";
   echo "Short info: " . $values['info'] . "<br> ";
   echo "<a href=\"http://www.adworld-online.com/submit.php?user=$username\">.::. Contact Us </a> <br />";
} else{
   echo "There is no user with that id";
}
?>

 

I have been experimenting with different random types like

<?php
session_start();
$IDs = range(1,10);// your ID array
$randID = array(5,7);
$per_page = 3;
if (isset($_SESSION['rID'])) $randID = $_SESSION['rID'];
if (count($randID) < $per_page){
shuffle($IDs);
$randID = array_merge($randID,$IDs);
$randID = array_values(array_unique($randID));
}
$curIDs = array(); 
for ($i = 0; $i < $per_page; $i++){ 
$curIDs[] = $randID[$i];
unset($randID[$i]);
}
$_SESSION['rID'] = array_values($randID);

foreach ($curIDs as $v) echo $v, "<br />\n";
?>

This code was given to me from sasa

 

That picks 3 numbers randomly and displays them, but it will display the same number twice if it is randomly picked

output looks like this

6

10

8

?>
<?php
$numbers = range(1, 10);
srand((float)microtime() * 1000000);
shuffle($numbers);
foreach ($numbers as $number) {
    echo "$number ";
}

This one is the beginning part of what i was going for. It picks 10 numbers in order, and the output looks like this

7 2 8 3 4 10 9 6 1 5

 

I would greatly appreciate people to help me out on this one, because I'm not fluent with php just yet, and I would like some help please

And please just keep the snottyness to a minimum thanks.

 

 

 

 

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.