Jump to content


Photo

loops and variables


  • Please log in to reply
3 replies to this topic

#1 bluemicrobyte

bluemicrobyte
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 06 June 2006 - 02:05 AM

The following code:

    $imagefilearray = array();

    while ($row = mysql_fetch_row($result)) {
        $imagefilearray = array_push($imagefilearray, $row[2]);
    }

    $newimage = array_rand($imagefilearray);

Gives me:

Warning: array_rand(): First argument has to be an array


What I'm trying to do is put all the $row[2]s in an array then select a random item from that array.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 June 2006 - 02:33 AM

Try this instead:
<?php
    $imagefilearray = array();
    while ($row = mysql_fetch_row($result))
        $imagefilearray[] = $row[2];
    $newimage = array_rand($imagefilearray);
?>

Ken

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 06 June 2006 - 02:52 AM

If you just want to pull a random image from the db, use only MySQL, it will be way faster - especially if you have a lot of records:

$res = mysql_query("SELECT * FROM images ORDER BY RAND() LIMIT 1") or die(mysql_error());

$newimage = mysql_result($res, 2);

~ D Kuang

#4 bluemicrobyte

bluemicrobyte
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 06 June 2006 - 04:10 AM

Thanks, poirot, that works nicely.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users