Jump to content

Archived

This topic is now archived and is closed to further replies.

bluemicrobyte

loops and variables

Recommended Posts

The following code:

[code]
    $imagefilearray = array();

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

    $newimage = array_rand($imagefilearray); [/code]

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.

Share this post


Link to post
Share on other sites
Try this instead:
[code]<?php
    $imagefilearray = array();
    while ($row = mysql_fetch_row($result))
        $imagefilearray[] = $row[2];
    $newimage = array_rand($imagefilearray);
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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:

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

$newimage = mysql_result($res, 2);[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.