Re: Problems with rand and arrays
| From: | Pavel Jartsev | Date: | Thu, 07 Dec 2000 16:33:07 +0000 |
| Subject: | Re: Problems with rand and arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-29152@lists.php.net to get a copy of this message | ||
Broder B wrote:
>
> I have some problems with the following code. I want to query my database for three randomly
> texts every time the script runs. But I keep running in to problems.
> Sometimes $arrRandRes[0] contains a '0' witch it can't do, since I want to query
> the database with that number as the ID. Please Help Me!!!
>
> ...
>
<from php-manual>
If you are picking only one entry, array_rand() returns the key for a
random entry.
Otherwise, it returns an array of keys for the random entries.
</from php-manual>
So, you should use $arrRandRes as array of keys to select items from
$arrRes.
And the line of your code will be then:
$rand_text_result = query("SELECT text FROM poems WHERE
id=$arrRes[$arrRandRes[0]] or id=$arrRes[$arrRandRes[1]] or
id=$arrRes[$arrRandRes[2]]");
Hope this helps.
:)
--
Pavel a.k.a. Papi