PHP array, foreach

  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

I have some image data stored in an array, usually 0-5 images, like this:

PHP Code: [ Select ]
$gallery = array(
  array(
         "href" => "large1.jpg",
          "img" => "<img src=\"img/1.jpg\"",
          "rel" => "lightbox",
        "title" => "image title 1",
       "photographer" => "P. Griffin",
       "date" => "23 June 1972",
       "copyright" => "cc-by-sa 3.0",
        ),
 
  array(
         "href" => "large2.jpg",
          "img" => "<img src=\"img/2.jpg\"",
          "rel" => "lightbox",
        "title" => "image title 2",
       "photographer" => "P. Griffin",
       "date" => "23 June 1972",
       "copyright" => "cc-by-sa 3.0",
        ),
 
  array(
         "href" => "large3.jpg",
          "img" => "<img src=\"img/3.jpg\"",
          "rel" => "lightbox",
        "title" => "image title 3",
       "photographer" => "P. Griffin",
       "date" => "23 June 1972",
       "copyright" => "cc-by-sa 3.0",
        ),
       
        ....
       
             );
 
  1. $gallery = array(
  2.   array(
  3.          "href" => "large1.jpg",
  4.           "img" => "<img src=\"img/1.jpg\"",
  5.           "rel" => "lightbox",
  6.         "title" => "image title 1",
  7.        "photographer" => "P. Griffin",
  8.        "date" => "23 June 1972",
  9.        "copyright" => "cc-by-sa 3.0",
  10.         ),
  11.  
  12.   array(
  13.          "href" => "large2.jpg",
  14.           "img" => "<img src=\"img/2.jpg\"",
  15.           "rel" => "lightbox",
  16.         "title" => "image title 2",
  17.        "photographer" => "P. Griffin",
  18.        "date" => "23 June 1972",
  19.        "copyright" => "cc-by-sa 3.0",
  20.         ),
  21.  
  22.   array(
  23.          "href" => "large3.jpg",
  24.           "img" => "<img src=\"img/3.jpg\"",
  25.           "rel" => "lightbox",
  26.         "title" => "image title 3",
  27.        "photographer" => "P. Griffin",
  28.        "date" => "23 June 1972",
  29.        "copyright" => "cc-by-sa 3.0",
  30.         ),
  31.        
  32.         ....
  33.        
  34.              );
  35.  


I'm trying to get these images and data in to an unordered list that looks like this:

HTML Code: [ Select ]
   
<ul>
<li><a href="large1.php" rel="lightbox" title="image title 1"><img src="img/1.jpg alt=""/><br>
P. Griffin, 23 June 1972, cc-by-sa 3.0.</li>
 
<li> ... etc
 
</ul>
 
 
  1.    
  2. <ul>
  3. <li><a href="large1.php" rel="lightbox" title="image title 1"><img src="img/1.jpg alt=""/><br>
  4. P. Griffin, 23 June 1972, cc-by-sa 3.0.</li>
  5.  
  6. <li> ... etc
  7.  
  8. </ul>
  9.  
  10.  


What I have so far (obviously not very smart) is just putting each piece of info in to its own <li></li>

PHP Code: [ Select ]
 
    foreach($gallery[$row] as $key => $value)
    {
        echo "<li>".$value."</li>";
    }
 
  1.  
  2.     foreach($gallery[$row] as $key => $value)
  3.     {
  4.         echo "<li>".$value."</li>";
  5.     }
  6.  


Any ideas how to do this?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

Based on the info you provided what about this

PHP Code: [ Select ]
$gallery = array(
         array(
            "href" => "large1.jpg",
            "img" => "<img src=\"img/1.jpg\"",
            "rel" => "lightbox",
            "title" => "image title 1",
            "photographer" => "P. Griffin",
            "date" => "23 June 1972",
            "copyright" => "cc-by-sa 3.0",
            ),
         
         array(
            "href" => "large2.jpg",
            "img" => "<img src=\"img/2.jpg\"",
            "rel" => "lightbox",
            "title" => "image title 2",
            "photographer" => "P. Griffin",
            "date" => "23 June 1972",
            "copyright" => "cc-by-sa 3.0",
            ),
         
         array(
            "href" => "large3.jpg",
            "img" => "<img src=\"img/3.jpg\"",
            "rel" => "lightbox",
            "title" => "image title 3",
            "photographer" => "P. Griffin",
            "date" => "23 June 1972",
            "copyright" => "cc-by-sa 3.0",
            )
         );
 
foreach($gallery as $key => $value) {
   echo '<li><a href="'. $value['href'] .'" rel="'. $value['rel'] .'" title="'. $value['title'] .'">'. $value['img'] .' alt="'. $value['title'] .'" border="0" /></a><br />'. $value['photographer'] .', '. $value['date'] .', '. $value['copyright'] .'</li>' . "\r\n";
}
 
  1. $gallery = array(
  2.          array(
  3.             "href" => "large1.jpg",
  4.             "img" => "<img src=\"img/1.jpg\"",
  5.             "rel" => "lightbox",
  6.             "title" => "image title 1",
  7.             "photographer" => "P. Griffin",
  8.             "date" => "23 June 1972",
  9.             "copyright" => "cc-by-sa 3.0",
  10.             ),
  11.          
  12.          array(
  13.             "href" => "large2.jpg",
  14.             "img" => "<img src=\"img/2.jpg\"",
  15.             "rel" => "lightbox",
  16.             "title" => "image title 2",
  17.             "photographer" => "P. Griffin",
  18.             "date" => "23 June 1972",
  19.             "copyright" => "cc-by-sa 3.0",
  20.             ),
  21.          
  22.          array(
  23.             "href" => "large3.jpg",
  24.             "img" => "<img src=\"img/3.jpg\"",
  25.             "rel" => "lightbox",
  26.             "title" => "image title 3",
  27.             "photographer" => "P. Griffin",
  28.             "date" => "23 June 1972",
  29.             "copyright" => "cc-by-sa 3.0",
  30.             )
  31.          );
  32.  
  33. foreach($gallery as $key => $value) {
  34.    echo '<li><a href="'. $value['href'] .'" rel="'. $value['rel'] .'" title="'. $value['title'] .'">'. $value['img'] .' alt="'. $value['title'] .'" border="0" /></a><br />'. $value['photographer'] .', '. $value['date'] .', '. $value['copyright'] .'</li>' . "\r\n";
  35. }
  36.  


I wasn't sure where you wanted the ending a tag so i added it at the end of the image
  • Zwirko
  • Guru
  • Guru
  • User avatar
  • Posts: 1417
  • Loc: 55° N, 3° W

Post 3+ Months Ago

Works perfectly. Thanks,
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

NP

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 152 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.