DLR Posted June 3, 2007 Share Posted June 3, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/54109-solved-shuffle-is-giving-me-the-two-step-expects-parameter-1-to-be-array/ Share on other sites More sharing options...
Psycho Posted June 3, 2007 Share Posted June 3, 2007 No, $resut is a resource id pointing to the query results. A better option is to use your query to grab results in a random order: SELECT image_id,image_caption FROM gallery WHERE sub_cat_id = $select ORDER BY RAND() Quote Link to comment https://forums.phpfreaks.com/topic/54109-solved-shuffle-is-giving-me-the-two-step-expects-parameter-1-to-be-array/#findComment-267512 Share on other sites More sharing options...
Psycho Posted June 3, 2007 Share Posted June 3, 2007 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54109-solved-shuffle-is-giving-me-the-two-step-expects-parameter-1-to-be-array/#findComment-267522 Share on other sites More sharing options...
DLR Posted June 4, 2007 Author Share Posted June 4, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54109-solved-shuffle-is-giving-me-the-two-step-expects-parameter-1-to-be-array/#findComment-267623 Share on other sites More sharing options...
sasa Posted June 4, 2007 Share Posted June 4, 2007 change line $colcounter = (($colcounter+1)>$maxcolumns) ? $colcounter++ : 1; to $colcounter = ($colcounter<$maxcolumns) ? $colcounter+1 : 1; Quote Link to comment https://forums.phpfreaks.com/topic/54109-solved-shuffle-is-giving-me-the-two-step-expects-parameter-1-to-be-array/#findComment-267635 Share on other sites More sharing options...
DLR Posted June 4, 2007 Author Share Posted June 4, 2007 Thanks very much! it works great! Quote Link to comment https://forums.phpfreaks.com/topic/54109-solved-shuffle-is-giving-me-the-two-step-expects-parameter-1-to-be-array/#findComment-267669 Share on other sites More sharing options...
Psycho Posted June 4, 2007 Share Posted June 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/54109-solved-shuffle-is-giving-me-the-two-step-expects-parameter-1-to-be-array/#findComment-267832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.