Jump to content

loops and variables


bluemicrobyte

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.
Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.