Re: Problems with rand and arrays
| From: | Broder B | Date: | Thu, 07 Dec 2000 17:36:28 +0000 |
| Subject: | Re: Problems with rand and arrays | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-29165@lists.php.net to get a copy of this message | ||
Yeah I think it would work if it were't for this error:
Parse error: parse error, expecting `']'' in C:\Program\Apache
Group\Apache\htdocs/boa/home.php on line 24
I just can't figure this one out.
$arrRandIdx = array_rand($arrRes, 3);
$rand_text_result = query("SELECT * FROM poems WHERE id=$arrRes[$arrRandIdx[0]]")
----- Original Message -----
From: "Pavel Jartsev" <papi@digitalfruit.ee>
To: "Broder B" <broder_b@chello.se>; <php-general@lists.php.net>
Sent: Thursday, December 07, 2000 5:33 PM
Subject: Re: [PHP] Problems with rand and arrays
> 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
>