Jump to content

[SOLVED] ways to use the shuffle


czukoman20

Recommended Posts

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

 

That is the code that will shuffle numbers one through ten.

and output like this

 

4 9 8 10 7 1 6 2 5 3

 

Is there a way to use those numbers, and use them as variables for a code?

if there is.. how would you do it?

Link to comment
Share on other sites

Is there a way to add the number inside the variable.

like $numbers[1] + 2 so it would = $numbers[3]

 

If you tell us what your trying to accomplish, I'm sure we can show you the best way to do it =]

 

But yes, you can do something like this

 

<?php

$i = 1;
$i +=3;

echo $numbers[$i]; //will print out as $numbers[4]

?>

Link to comment
Share on other sites

Ok well i tried to do this earlier but nobody replied so.

I have code already

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){

This code gets the userid, and pulls information from the user and displays it.

For my site, they want it to be random, and so that the user doesn't see doubles until he has gone through every user.

 

So i thought ok. if i could generate a list of random numbers like we did up there, and use those to input into that userid_1 variable then i could display random. Im stuck at how to keep it all at 5 per page, and to keep it going after each time the variables change

 

Thats my goal

Link to comment
Share on other sites

[quote author=play_ link=topic=171232.msg757780#msg757780 date=1197171113]
as variable name or values?

if it's name, then no.
[/quote]

absolutely you can use them as name you can have dynamic variable naming

<?php
$numbers = range(1, 10);
srand((float)microtime() * 1000000);
shuffle($numbers);
foreach ($numbers as $number) {
    $.number = "Its a variable ".$number;
}
$i = 1;
while($i <11){
if(ISSET($.$i)){
echo $.$i."<br />";
}
$i++;
}
?>

should show 10 variables

Link to comment
Share on other sites

If you want to display random, there is no need to do all that extra work with PHP, you can do it right in your query :)

 

SELECT * FROM users ORDER BY rand()

 

That will make the results from the database come back random. If you want to sort it by pages, look into pagination.

 

That gave me a syntax error on line 6

$.number = "Its a variable ".$number;

Link to comment
Share on other sites

ok so i have the code

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){

 

 

"SELECT * FROM users WHERE userid_2='$userid_1'" is the part. how would i impliment this. to get the userid_2 variable randomly instead of it = userid_1

Link to comment
Share on other sites

mysql_select_db('db215169417')
or die('Error, cannot select mysql database');
$query = mysql_query("SELECT * FROM users WHERE userid_2 > 0 ORDER BY rand()");
$values = mysql_fetch_array($query);
$nrRows = mysql_num_rows($query);

 

I used that.... and it doesn't work.... is my code messed up in the

$query = mysql_query("SELECT * FROM users WHERE userid_2 > 0 ORDER BY rand()");

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.