PDA

View Full Version : [PHP] Array Problems, man I'm dumb lately



nobody
March 8th, 2005, 07:03 PM
Hey guys, I have this code


$rowsy = mysql_query ( " SELECT * FROM mainpage WHERE page = 'Random'" );
$IDS = array ("17");
while ($rows = mysql_fetch_assoc($rowsy)) {
$IDS[] = $rows['ID'] ;
}
$numRows = mysql_num_rows($rowsy);
$real = $numRows - 1;
$random = rand(0,$rea);
$final = $IDS[$random];
$query17 = mysql_query( " SELECT * FROM mainpage WHERE ID = '$final'" );

Which should..
Find the amount of rows in the table with the page set as 'Random'
Which it does
Then it should create an array with those ID's.
Then it should find the rows again, this time subtracting one to correspond to the array
Then, find a random number between zero and the amount of rows, which I'm not really sure if it does
Then it should take that number and use it to pull an ID out of the array, which I'm pretty sure it doesn't do.
Finally, it should pull use that ID to pull out the info from the array.

Not working.

Any brilliant ideas, I'm getting frustrated here.

Yeah, I realize this is a really stupid way to do this, but it's about my only option with the way I set up the table like a moron.

nobody
March 8th, 2005, 07:08 PM
Just a little helpful info.

$random always outputs 0.
So the problem is somewhere around there I guess

binime
March 9th, 2005, 01:41 AM
Hey guys, I have this code


$rowsy = mysql_query ( " SELECT * FROM mainpage WHERE page = 'Random'" );
$IDS = array ("17");
while ($rows = mysql_fetch_assoc($rowsy)) {
$IDS[] = $rows['ID'] ;
}
$numRows = mysql_num_rows($rowsy);
$real = $numRows - 1;
$random = rand(0,$rea);
$final = $IDS[$random];
$query17 = mysql_query( " SELECT * FROM mainpage WHERE ID = '$final'" );

just check ur code variables, especially the line


$random = rand(0,$rea);

try it as


$random = rand(0,$real);

edit:-
if you want it to be random...

in your sql code


ORDER BY rand() LIMIT 1

teiz77
March 9th, 2005, 03:06 AM
if you want it to be random...

in your sql code


ORDER BY rand() LIMIT 1


yeah... let mysql do the job... much easier

nobody
March 10th, 2005, 02:32 PM
Whoa thanks guys, didn't see anyone responded. I got it to work my stupid cracked up way, I had no idea I could put rand() inside an sql query like that. Very cool, thanks a lot :)