I've spent most of the day trying to wrap my head around multidimensional arrays, in order to build a PHP function that I can call to populate an image gallery that could potentially hold 100s of elements. At this point it would have been easier to just copy and paste the HTML portion over and over again, but I'd like to figure out how to make this work, for future projects.
I have found solutions to other questions about foreach loops with multidimensinoal arrays, but I haven't been able to adapt them to my case. In one instance, the closest to my own situation, the solution no longer works--but I don't have the rep to make a comment about it. I used the code from that example as the basis for my own attempt. I welcome any assistance! My comments may help explain what I'm trying to achieve, if it isn't already clear.
Here is my PHP code so far:
<?php
// Use PHP array to populate the image path, title (for alt tag), and longer description (for image caption)
function build_html ($imgsrc, $title, $desc) {
echo
'<div class="responsive">
<div class="gallery">
<img src="$imgsrc" alt="$title" width="300" height="200">
<div class="desc">$desc</div>
</div>
</div>';
}
// List of images; filenames will be numerical, so later I may switch to using $i++ shortcut in place of imgsrc
$gallery = array (
array (
"imgsrc" => "i/Alice0.jpg",
"title" => "Front of House",
"desc" => "View of the house from the front sidewalk"
),
array (
"imgsrc" => "i/Alice1.jpg",
"title" => "Front door",
"desc" => "Close-up of front door"
)
);
// This is just for debugging, to confirm that I really have a multidimensional array
print_r($gallery);
// BROKEN: this should use foreach loop(s) to gather the elements and pass them to the build_html function defined above.
function display_gallery () {
foreach ($gallery as $img) {
$imgsrc = $img['imgsrc'];
$title = $img['title'];
$desc = $img['desc'];
build_html($imgsrc, $title, $desc);
}
}
// Calling the function in body of page
display_gallery();
?>
Edit: The error message I'm currently getting is:
Warning: Invalid argument supplied for foreach() in /home/varlokkur/house.anthropo.org/index.php on line 121
Edit: In case anyone stumbles across this question in the future, here is my modified code that works as expected. Note that the solution offered by aendeerei is a better example to follow, but this solution is closest to the original code. 2 changes to make it work:
- Fixed built_html function to properly use the variables that are passed
- Fixed display_gallery function to use the $gallery variable
Fixed code:
<?php
// Use PHP array to populate the image path, title (for alt tag), and longer description (for image caption)
function build_html ($imgsrc, $title, $desc) {
echo
'
<div class="responsive">
<div class="gallery">
<img src="' . $imgsrc . '" alt="' . $title . '" width="300" height="200">
<div class="desc">'. $desc . '</div>
</div>
</div>
';
}
// List of images; filenames will be numerical, so later I may switch to using $i++ shortcut in place of imgsrc
$gallery = array (
array (
"imgsrc" => "i/Alice0.JPG",
"title" => "Front of House",
"desc" => "View of the house from the front sidewalk"
),
array (
"imgsrc" => "i/Alice1.JPG",
"title" => "Front door",
"desc" => "Close-up of front door"
)
);
// This is just for debugging, to confirm that I really have a multidimensional array
//print_r($gallery);
// BROKEN: this should use foreach loop(s) to gather the elements and pass them to the build_html function defined above.
function display_gallery ($gallery) {
// global $gallery;
foreach ($gallery as $img) {
$imgsrc = $img['imgsrc'];
$title = $img['title'];
$desc = $img['desc'];
build_html($imgsrc, $title, $desc);
// var_dump($gallery);
}
}
//var_dump($gallery);
// Calling the function in body of page
display_gallery($gallery);
?>