我有一个包含几个链接的页面,例如grade.php?item = kindergarten,其中项目值不同。我的目标是为每个班级提供详细信息页面。
在目标网页上,一个详细信息页面,我使用的是foreach循环,但我无法动态地使用它。
<?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>
< } ?>
只要使用$ classProjects,就会正确列出详细信息,但根据项目名称有不同的详细信息。我为每个类都有单独的数组。我将数组名称设置为动态的尝试无效....即使我能够回显正确的字符串....并将其类型更改为数组。
也许我以错误的方式接近这个?我是否应该更好地修改我的数组以包含另一层深度,而不是尝试捕获项值并在foreach循环中动态命名我的数组?
我可以用一个例子。谢谢!
@DarylGill其他信息:
$ 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'>"
),
);
选择前一页上的每个链接会将用户引导至该特定年级的详细信息页面(即,从该等级的正确数组中提取)。
详细信息页面上的foreach循环不能有指向特定阵列的硬连线指针,或者所有6个链接都将转到该数据。它需要捕获在URL中传递的项值。我想这样做的方法是创建另一个变量,它将动态更新foreach循环中的数组名称。
<?php foreach ($classProjects as $grade => $item) { ?>
如果$ classProjects可以动态更新为&#39; $ kinderProjects&#39;当点击幼儿园链接时,它会从该阵列中提取正确的数据。
答案 0 :(得分:1)
$count = count($classProjects);
for($i = 0 ;$i < $count ; $i++){
echo $classProjects[$i];
echo $anotherArray[$i];//or what ever you want to print here.
}
答案 1 :(得分:0)
您可以根据参数请求数据,然后将其分配给您要循环的数组。
例如:
$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>';
}