Jump to content

[SOLVED] shuffle() is giving me the two-step - expects parameter 1 to be array


DLR

Recommended Posts

HI there all,

 

The I want to randomly present the pictures from my gallery each time they are viewed.  I have constructed a query - it works fine - but as soon as I try to shuffle the $result array it tells me that "expects parameter 1 to be array".

 

I thought in this query, that $result is an array. Where am I going wrong?

 

Please help!

 

$query = "SELECT image_id,image_caption FROM gallery WHERE sub_cat_id = $select";   
$result = mysql_query($query) or die(mysql_error());

//without the following line everything displays correctly, but always in the same order
//with this line, I get "expects parameter 1 to be array"
shuffle($result);  

echo "<table border='0'>";
$rowcounter = "0";
while ($row = mysql_fetch_assoc($result)) {

if ($rowcounter == "0"){
	echo "<tr><td width='200' >";
} else {
	echo "<td width='200' >";
}

echo "<img src='Gallery/" . $row['image_id'] . ".jpg'><br>";
echo $row['image_caption'];
$rowcounter = $rowcounter + 1;

if ($rowcounter >> 2) {
	echo "</td></tr>";
	$rowcounter = "0";
} else {
	echo "</td>";
}
}

echo "</tr></table>";

Link to comment
Share on other sites

There is also a problem with your script that it will create an unneeded ending TR tag depending on the number of records returned. This may work better (may be typos, but the process should be valid)

 

<?php
$query = "SELECT image_id,image_caption
          FROM gallery
          WHERE sub_cat_id = $select
          ORDER BY RAND()";

$result = mysql_query($query) or die(mysql_error());

$maxcolumns = 2;
$colcounter = 1;

echo "<table border='0'>";

while ($row = mysql_fetch_assoc($result)) {

    if ($colcounter == 1) { echo "<tr>"; }

    echo "<td width='200' >";
    echo "<img src='Gallery/" . $row['image_id'] . ".jpg'><br>";
    echo $row['image_caption'];
    echo "</td>";

    if ($colcounter == $maxcolumns) { echo "</tr>"; }

    $colcounter = (($colcounter+1)>$maxcolumns) ? $colcounter++ : 1;
}

if ($colcounter!=1) { echo "</tr>"; }

echo "</table>";
?>

Link to comment
Share on other sites

Thanks for the help. Your solution is far more elegant than my attempts - I've learn't plenty!

1. Your solution using rand() works.

2. Thank you for the way to avoid the hanging <tr>

2. However, I only out put a single column. I suppose the problem lies in this line of code;

 

    $colcounter = (($colcounter+1)>$maxcolumns) ? $colcounter++ : 1;

 

I simply do not understand the logic, so cannot see where to problem-solve.

 

I assumed that the first $colcounter should actually be $maxcolumns. When I made the change, this always has the first image by itself (i.e one row, one column), the number of images in the next rext row varies, depending on the value set for $maxcolumns before the while loop.

 

I'm really guessing as I do not follow your logic. I'm also at a loss to know where else to go to get someone to explain it to me!

 

Your further input would be greatly apprectiated.

David

 

Link to comment
Share on other sites

Whoops, got that backward!

 

And, just to elaborate, that code is just a shorthand method of an if/else condition (there is a technical name for it, but I can never remember it)

 

Basically it works like this:

 

$variable = (test) ? true condition : false condition;

 

So this line

 

$colcounter = ($colcounter<$maxcolumns) ? $colcounter+1 : 1;

 

Tests to see if the current column ($colcounter) is less than the $maxcolumns. If so, it sets $colcounter to itself plus 1; if not then it sets $colcounter to 1.

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.