Reputation: 5
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:
$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'>"
),
);
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).
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.
Upvotes: 0
Views: 2759
Reputation: 723
You can request the data based on a parameter and then assign that to the array you're looping through.
For example:
$data_array_one = 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'>"
)
);
$data_array_two = 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'>"
)
);
switch ($_GET['item']) {
case 'kindergarten':
$classProject = $data_array_one;
break;
case 'tiergarten':
$classProject = $data_array_two;
break;
default:
// Define the default data here
$classProject = array();
break;
}
foreach ($classProjects as $grade => $item) {
echo '<li>'. $item['image'] .'<a href="grade.php?item='. $grade .'">'. $item['title'] .'</a><br /><p>'. $item['sentence'] .'</p><br /><a href="grade.php?item='. $grade .'">More ››</a></li>';
}
Upvotes: 0
Reputation: 1679
$count = count($classProjects);
for($i = 0 ;$i < $count ; $i++){
echo $classProjects[$i];
echo $anotherArray[$i];//or what ever you want to print here.
}
Upvotes: 1