1

I would like to show 3 random images from database in my website. Below is its code:

$query = mysql_query ("SELECT id,url FROM tbl_gallery2");
if (mysql_num_rows($query) >= 3) {
    $my_array = array();
    $last_array = array();
    while ($r = mysql_fetch_row($query)) {
        $my_array[] = $r[1];
    }

    function makernd () {
        $number = array_rand($my_array,1);
        if (in_array($number,$last_array)) {
            makernd();
        } else {
            $last_array[] = $number;
            return $number;
        }
    }

    for($i = 1 ; $i < 3 ; $i++) {
        $item = makernd();
        echo '<img src="./images/slider/'.$item.'.jpg" alt="" class="slider" />';
    }

}

But whenever I run this code, I get the error below:

Undefined variable: my_array in line ... // The first line of makernd() function.

But I expected $my_array to be an accessible array for this function.
What's the problem?

2
  • You either have lo pass the array to your function or define it as a global var. Commented Dec 1, 2012 at 14:28
  • Why are you conditionally defining the function inside the if? That doesn't add much other than confusion. Commented Dec 1, 2012 at 14:28

1 Answer 1

3

To simply fix your problem, you should pass $my_array to makernd() as a parameter:

$query = mysql_query ("SELECT id,url FROM tbl_gallery2");
if (mysql_num_rows($query) >= 3) {
    $my_array = array();
    $last_array = array();
    while ($r = mysql_fetch_row($query)) {
        $my_array[] = $r[1];
    }

    function makernd ($my_array) {
        $number = array_rand($my_array,1);
        if (in_array($number,$last_array)) {
            makernd($my_array);
        } else {
            $last_array[] = $number;
            return $number;
        }
    }

    for($i = 1 ; $i < 3 ; $i++) {
        $item = makernd($my_array);
        echo '<img src="./images/slider/'.$item.'.jpg" alt="" class="slider" />';
    }

}

HOWEVER, I strongly suggest putting the randomization in MySQL, to

  1. Simplify your code
  2. Significantly improve the performance, and
  3. Eliminate excessive loops & recursion in PHP

Example:

$sql = "SELECT id,url 
        FROM tbl_gallery2 
        ORDER BY RAND() 
        LIMIT 3";
$query = mysql_query ($sql);
if (mysql_num_rows($query) >= 3) {
    while ($r = mysql_fetch_row($query)) {
        echo '<img src="./images/slider/' . $r[1] . '.jpg" alt="" class="slider" />';
    }
}

PS - I also suggest you update your code to use mysqli, as mysql is deprecated

PPS - I also suggest you look into mysqli_fetch_assoc so you can reference query results by name instead of index (e.g. $r['url'] instead of $r[1] - as if you ever change the order of your query, you will break references by index.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.