PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
fariquzeli
Forum Contributor
Posts: 144 Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:
Post
by fariquzeli » Wed Dec 11, 2002 2:29 pm
I'm looking to make the first 5 products on my main page random numbers between 2 and nine, so it will look something like this:
Code: Select all
$first = rand(2,9);
$second = rand(2,9);
$third = rand(2,9);
$fourth = rand(2,9);
$fifth = rand(2,9);
My question is, how do I make it so that when getting a random integer, there are no duplicates, because I only want each product to show up once. How do I make it so that number 2 doesn't come up twice as a random integer?
puckeye
Forum Contributor
Posts: 105 Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:
Post
by puckeye » Wed Dec 11, 2002 2:47 pm
Pretty much the only way I can see is to check the second throuh fifth to make sure there's no duplicate.
Code: Select all
<?php
function random($min, $max, $taken) // $taken is an array;
{
do
{
$draw = rand($min, $max);
} while(in_array ( $draw, $taken));
$taken[count($taken)] = $draw; // add the new value in the array
return $draw;
}
$taken = array();
$first = random(2, 9, $taken);
$second = random(2, 9, $taken);
$third = random(2, 9, $taken);
$fourth = random(2, 9, $taken);
$fifth = random(2, 9, $taken);
?>
I think this will do the trick. This is untested though.
Michel
fariquzeli
Forum Contributor
Posts: 144 Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:
Post
by fariquzeli » Wed Dec 11, 2002 3:04 pm
didn't work, returns multiples
i echoed out all the variables and got
54452
2 4's and 2 5's that's no good
gyardleydn
Forum Commoner
Posts: 27 Joined: Tue Dec 03, 2002 8:27 am
Post
by gyardleydn » Thu Dec 12, 2002 12:20 am
puckeye wrote: Pretty much the only way I can see is to check the second throuh fifth to make sure there's no duplicate.
Code: Select all
<?php
function random($min, $max, $taken) // $taken is an array;
Global array will not be changed. Minor glich, you need a reference here. try:
Code: Select all
<?php
function random($min, $max, &$taken) // $taken is an array;
fariquzeli
Forum Contributor
Posts: 144 Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:
Post
by fariquzeli » Thu Dec 12, 2002 12:36 am
I figured it out, I just wrote it out, wasn't soo bad:
Code: Select all
<?php
$first = rand(2,9);
do {
$second = rand(2,9);
}
while( $second == $first);
do {
$third = rand(2,9);
}
while( $third == $first | $third == $second);
do {
$fourth = rand(2,9);
}
while( $fourth == $first | $fourth == $second | $fourth == $third);
do {
$fifth = rand(2,9);
}
while( $fifth == $first | $fifth == $second | $fifth == $third | $fifth == $fourth);
echo $first;
echo $second;
echo $third;
echo $fourth;
echo $fifth;
?>
Traduim
Forum Newbie
Posts: 11 Joined: Tue Dec 10, 2002 4:21 am
Location: Catalunya
Post
by Traduim » Thu Dec 12, 2002 11:05 am
Perhaps it's not the case, but if you want to recover 5 random records out a MySQL database, you just need to do one only SQL query:
Code: Select all
mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 5");
I hope it might be useful.
fariquzeli
Forum Contributor
Posts: 144 Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:
Post
by fariquzeli » Thu Dec 12, 2002 11:11 am
the only thing is there's about 60 entries in the table and I want to select between 9 of them that have a special variable set.
It's alright though it all worked out in the end.
Traduim
Forum Newbie
Posts: 11 Joined: Tue Dec 10, 2002 4:21 am
Location: Catalunya
Post
by Traduim » Thu Dec 12, 2002 11:52 am
You can use a where clause in conjunction with order by and limit.
fariquzeli
Forum Contributor
Posts: 144 Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:
Post
by fariquzeli » Thu Dec 12, 2002 11:55 am
would that return multiples of the same integer? ex. two number 4's?
Traduim
Forum Newbie
Posts: 11 Joined: Tue Dec 10, 2002 4:21 am
Location: Catalunya
Post
by Traduim » Thu Dec 12, 2002 12:02 pm
Example:
SELECT * FROM table WHERE nice = 1 AND psicothic = 0
It would select all the records matching the where clause, and it will not sort the results in any way.
SELECT * FROM table WHERE nice = 1 AND psicothic = 0 ORDER BY RAND()
The same as before, but the results have been shuffled at random.
If you run this select twice, you will have the same results sorted in two different ways
SELECT * FROM table WHERE nice = 1 AND psicothic = 0 ORDER BY RAND()
The same as before, but this time you just get the 5 top records.
If you run this select twice, you will have different results.
Simply give it a try and see if it works.
There are some cases where this is not functional, but it saves a lot of time in several cases.
fariquzeli
Forum Contributor
Posts: 144 Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:
Post
by fariquzeli » Thu Dec 12, 2002 12:23 pm
works very well. Alot simpler too and it will make it much easier to display all the records. Thanks a bunch!