Forum Moderators: coopster

Message Too Old, No Replies

How to create an array

Pulling random items from db

         

abbeyvet

8:53 am on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to pull random items from a product database, and am having a bit of a problem getting it exactly as I want it.

I am using this:

$sql = "SELECT * from products WHERE pSection=7 ORDER BY RAND() LIMIT 0,3";

Which is fine, it pulls products from pSection 7 fine. But I want it to pull from several sections at once. I tried making an array like this:

$randpsections = array("9", "10", "11", "12");
and then
$sql = "SELECT * from products WHERE pSection=$randpsections ORDER BY RAND() LIMIT 0,3";

But I get an error.

Obviously I have very limited PHP knowledge so would appreciate any help.

Thanks

Little_G

10:05 am on Oct 13, 2006 (gmt 0)

10+ Year Member



Hi,

try:

$randpsections = array("9", "10", "11", "12");
and then
$sql = "SELECT * from products WHERE pSection=" . $randpsections[mt_rand(0,count($randpsections)-1] . "ORDER BY RAND() LIMIT 0,3";

Andrew

abbeyvet

10:52 am on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your help, looks like it should be right, but is getting an error:

Parse error: parse error, unexpected ']' in /home/sites/www.example.com/web/test.php on line 33

which is this line:

<?php $sql = "SELECT * from products WHERE pSection=" . $randpsections[mt_rand(0,count($randpsections)-1] . " ORDER BY RAND() LIMIT 0,3";

Don't see the error - any suggestions?

The rest of the code, in case it is relevant, is below.

<?php
$randpsections = array("9", "10", "11", "12");
$sql = "SELECT * from products WHERE pSection=" . $randpsections[mt_rand(0,count($randpsections)-1] . " ORDER BY RAND() LIMIT 0,3";
$result = mysql_query($sql) or die(mysql_error());

while ($newArray = mysql_fetch_array($result)) {
$name = $newArray['pName'];
$image = $newArray['pImage'];
$doit = $newArray["pDescription"];
$max=70;
$modstr=((strlen($doit)>$max)?substr($doit,0,$max) . "..." : $doit);
echo "stuff here";
}
@mysql_free_result($queryprod);
?>

andye

10:58 am on Oct 13, 2006 (gmt 0)

10+ Year Member



I'm a Perl guy rather than a PHP one so apologies if this is off base, but maybe you should count the brackets in

$randpsections[mt_rand(0,count($randpsections)-1]

?

hth, a.

abbeyvet

11:10 am on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, thanks, that was it! This works:

<?php $sql = "SELECT * from products WHERE pSection=" . $randpsections[mt_rand(0,count($randpsections)-1)] . " ORDER BY RAND() LIMIT 0,3";

whoisgregg

2:00 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A slightly different approach... This one has the DB do all the heavy lifting. (May not be faster, but included here because learning the "IN (value1,value2,...)" method is helpful for many situations.)

$randpsections = array("9", "10", "11", "12"); 
$sql = "SELECT * from products WHERE pSection IN (" .implode(",", $randpsections). ") ORDER BY RAND() LIMIT 0,3";

jatar_k

3:37 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm with whoisgregg

IN is a better way to go

>> learning the "IN (value1,value2,...)" method is helpful for many situations

another good one is BETWEEN ;)

abbeyvet

8:56 am on Oct 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you whoisgregg - that works great and I was unaware of the IN thing, so have learned something new.

:)