czukoman20 Posted December 9, 2007 Share Posted December 9, 2007 <?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? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 The numbers are all stored in the $numbers array. echo $numbers[0]; Will print out the first number in the array. $number[1] will print out the second, and so on. Is that what you want? I guess it depends on what your wanting to do. Quote Link to comment Share on other sites More sharing options...
play_ Posted December 9, 2007 Share Posted December 9, 2007 as variable name or values? if it's name, then no. Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 9, 2007 Author Share Posted December 9, 2007 what poco said should work, but dont go away, because i need to see if this works. Thanks Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 9, 2007 Author Share Posted December 9, 2007 Is there a way to add the number inside the variable. like $numbers[1] + 2 so it would = $numbers[3] Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 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] ?> Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 9, 2007 Author Share Posted December 9, 2007 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 Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 9, 2007 Author Share Posted December 9, 2007 ideas? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 9, 2007 Share Posted December 9, 2007 [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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 9, 2007 Author Share Posted December 9, 2007 well i have some users in my database at different levels. like guest premium and admin premium users are the only ones that i'd use for this. and those are the ones with userid's greater than 0 so how would that work? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 You would just change your query up a bit, like this SELECT * FROM users WHERE userid > 0 ORDER BY rand() Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 9, 2007 Author Share Posted December 9, 2007 ok i will try that tomorrow Quote Link to comment Share on other sites More sharing options...
play_ Posted December 9, 2007 Share Posted December 9, 2007 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; Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 Why do you have a period in front of the dollar sign? $.number Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 11, 2007 Author Share Posted December 11, 2007 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 Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 11, 2007 Author Share Posted December 11, 2007 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()"); Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 11, 2007 Author Share Posted December 11, 2007 Nobody has any IDeA? seriously now. Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 11, 2007 Author Share Posted December 11, 2007 Ok i figured it out. 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.