-3

I'm trying to make an image gallery. On the index file I want to show the albums with the images, see; http://www.robcnossen.nl/

I want to randomize the images that are inside these albums but I get all sorts of errors like:

warning: rand() expects parameter 1 to be long, string given in.

My code is;

foreach ($albums as $album) {
?><div><h2><?php
echo'<a href="view_album.php?album_id=', $album['id'],'">',$album['name'], '</a>';?>       </h2><?php       
    echo'<a href="view_album.php?album_id=', $album["id"],'"><img src="uploads/thumbs/', $album["id"], '/', $album["imagename"],'" title="" /></a>';
    ?></div><?php
}

The $album["imagename"] are the images inside the albums and I want to randomize this part. I tried for example:

rand($album["imagename"], 0)

but that gives an error.

I also tried shuffle;

foreach ($albums as $album) {
shuffle($album["imagename"]);
?><div><h2><?php
echo'<a href="view_album.php?album_id=', $album['id'],'">',$album['name'], '</a>';?></h2><?php      
    echo'<a href="view_album.php?album_id=', $album["id"],'"><img src="uploads/thumbs/', $album["id"], '/', $album["imagename"],'" title="" /></a>';
    ?></div><?php
}

But also there I get only errors.

Can anybody help me with this?

var_dump($albums); gives

array(2) {
   [0]=> array(8) { 
      ["id"]=> string(1) "8" 
      ["timestamp"]=> string(10) "1373890251" 
      ["name"]=> string(7) "Holland" 
      ["description"]=> string(19) "Fantastische foto's" 
      ["count"]=> string(1) "2" 
      ["imagename"]=> string(38) "KONICA MINOLTA DIGITAL CAMERA_428.jpeg" 
      ["image"]=> string(2) "63" 
      ["ext"]=> string(0) ""
   } 
   [1]=> array(8) { 
      ["id"]=> string(1) "9" 
      ["timestamp"]=> string(10) "1376914749" 
      ["name"]=> string(6) "Belgie" 
      ["description"]=> string(11) "Mooi Belgie" 
      ["count"]=> string(1) "2" 
      ["imagename"]=> string(12) "PICT0170.JPG" 
      ["image"]=> string(2) "66" 
      ["ext"]=> string(0) "" 
   }
  }

as result.

Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
Robske
  • 57
  • 1
  • 8
  • http://php.net/shuffle – hakre Sep 04 '13 at 07:54
  • 2
    And apply shuffle to the array not to a single value of that array. – tea2code Sep 04 '13 at 07:55
  • @Robske: We expect users having a problem with a specific PHP function to read about that function in the manual and explain then why they think their code should work. "Not working" is not a question. And that you tried multiple things is fine and normally expected. – hakre Sep 04 '13 at 07:57
  • 1
    What is `$albums` btw? Please add a plain-text `var_export($albums);` and `var_dump($albums);` to your question. – hakre Sep 04 '13 at 08:02
  • I looked first on php.net/shuffle and other sites about rand() and shuffle, that is why I thought that I was doing the right thing with shuffle, but the problem is that $album["imagename"] is not seen as an array but it is in the foreach loop. I already did try all the solutions given here but all gives errors and I don't know why... – Robske Sep 04 '13 at 08:17
  • @ the edit. Can you put it as code? The last piece – Loko Sep 04 '13 at 08:33

2 Answers2

0

shuffle should work for you, but not if you put it inside the foreach loop, like your example code. You need to shuffle before you start the loop. Also, shuffle needs to be called with the array itself, not an item of the array:

shuffle($albums);
foreach ($albums as $album) {
    ...
}
Nicole
  • 32,841
  • 11
  • 75
  • 101
-1

you should shuffle array out side the loop

<?php
 shuffle($albums);
 foreach ($albums as $album) 
 {
  ?><div><h2>
 <?php
  echo'<a href="view_album.php?album_id=', $album['id'],'">',$album['name'], '</a>';
  ?>
  </h2>
  <?php      
      echo'<a href="view_album.php?album_id=', $album["id"],'"><img src="uploads/thumbs/', $album["id"], '/', $album["imagename"],'" title="" /></a>';
  ?>
  </div>
  <?php
 }
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41