Re: problems with random numbers

From: Date: Sun, 03 Dec 2000 19:12:23 +0000
Subject: Re: problems with random numbers
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-28457@lists.php.net to get a copy of this message
Yes it works fine, it is just that I need both the 'id' and the 'text' field from the database, as it is now I only have the id. I tried with multidimensional arrays: while ($row = mysql_fetch_array($rand_text_result)) { $arrRes[$a]["id"] = $row["id"]; $arrRes[$a]["text"] = $row["text"]; $a++; } $arrRandRes = array_rand($arrRes, 3); echo $arrRandRes[0]["id"]; echo $arrRandRes[0]["text"]; echo $arrRandRes[1]["id"]; echo $arrRandRes[1]["text"]; but it does not work, do you have any ideas how I can do it another way? ----- Original Message ----- From: "nicola" <key5@key5.com> To: "Broder B" <broder_b@chello.se> Cc: "Php General Mailing List" <php-general@lists.php.net> Sent: Friday, December 01, 2000 2:16 AM Subject: Re: [PHP] problems with random numbers > hI, > > Why don't you use this instead? > > $a = 0; > $rand_text_result = query("SELECT * FROM poems"); > while ($row = mysql_fetch_array($rand_text_result)) { > $arrResults[$a] = $row["id"]; > ++$a; > } > > > //using this function: mixed array_rand (array input [, int num_req]) > // NB:_ only available with PHP4 > > $arrResultsRand = mixed array_rand ($arrResults, 3); > > echo $arrResultsRand[0] . "<br>"; > echo $arrResultsRand[1] . "<br>"; > echo $arrResultsRand[2] . "<br>"; > > > ?> > > Broder B ha scritto: > > > I am trying to three random generated numbers so I can get three diferent queries to my > > MySQL database. The problem I have is that sometimes I only get two numbers instead of three, I > > sense the problem is that one of the randomized number does not exists in the database. Here is the > > code, maybe someone could help me? > > > > srand ((double) microtime() * 1000000); > > $randval1 = rand(1, $num_rows); > > $randval2 = rand(1, $num_rows); > > $randval3 = rand(1, $num_rows); > > > > $rand_text_result = query("SELECT * FROM poems WHERE id=$randval1 or id=$randval2 or > > id=$randval3"); > > while ($row = mysql_fetch_array($rand_text_result)) { > > echo $row["id"] . "<br>"; > > } > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#28457) next »