I have a page with several links such as grade.php?item=kindergarten, where the item values are different. My goal is to have detail pages for each class.
On the target page, a detail page, I'm using a foreach loop, but I can't get it to work dynamically.
<?php foreach ($classProjects as $grade => $item) { ?>
<li><?php echo $item[image]; ?><a href="grade.php?item=<?php echo $grade; ?>"><?php echo $item[title]; ?></a><br /><p><?php echo $item[sentence]; ?></p><br /><a href="grade.php?item=<?php echo $grade; ?>">More ››</a></li>
< } ?>
As long as $classProjects is used, the details are listed correctly, but there are different details depending on the item name. I have separate arrays for each class. My attempts at making the array name dynamic have not worked.... even when I was able to echo the correct string.... and change it's type to an array.
Perhaps I'm approaching this the wrong way? Would I be better off modifying my array to include another layer of depth, rather than trying to capture the item value and have that dynamically name my array in the foreach loop?
I could use an example. Thanks!
@DarylGill Additional Information:
- The contents of your array which you are working with (This is one of 6 arrays set up the same way)
$kinderProjects = array(
array(
title => "Fall Leaves",
blurb => "Our Kinders have enjoyed learning the difference between the warm colors (yellow, orange, red) and the cool colors (green, blue, violet) and are putting this knowledge to use by painting fall leaves in watercolor and oil pastel. They are also reviewing the use of the visual art elements of line and shape.",
image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
),
array(
title => "Francis",
blurb => "Francis knows her stuff. The big sister of Frankie himself, she runs the show. Don't miss her Margherita Mondays!",
image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
),
array(
title => "Carlos",
blurb => "Carlos is the epitome of the phrase “Don't judge a book by it's cover” — You simply cannot find a better chef.",
image => "<img src='images/pic3.jpg' class='img-thumbnail pull-left' width='200' height='150' border='0'>"
),
);
- your expected results
That selecting each link on the prior page will direct users to a detail page for that specific grade level (i.e., pull from the correct array for that grade).
- what information is required for the expected results
The foreach loop on the details page can't have a hardwired pointer to a specific array, or all the 6 links will go to that data. It needs to capture the item value that's passed in the URL. I was thinking that a way to do that would be to make another variable which would dynamically update the array name in the foreach loop.
<?php foreach ($classProjects as $grade => $item) { ?>
if $classProjects could dynamically update to '$kinderProjects' when the kindergarden link is clicked, it would pull the correct data from that array.
$item['image']instead of$item[image]itemtypes we are dealing with and what should happen differently for each one of them.