【发布时间】:2014-12-10 15:29:48
【问题描述】:
我有一个包含多个链接的页面,例如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 附加信息:
- 您正在使用的数组的内容(这是以相同方式设置的 6 个数组之一)
$kinderProjects = 数组(
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 可以在点击幼儿园链接时动态更新为“$kinderProjects”,它会从该数组中提取正确的数据。
【问题讨论】:
-
你能举一个你的方法不起作用的例子吗?
-
你应该引用你的数组索引
$item['image']而不是$item[image] -
我可以举个例子,但恐怕我走错了路。有人可以谈谈是否最好有单独的数组并以某种方式反映在目标页面的 foreach 循环中?
-
不确定如何帮助您处理所提供的数据。详细说明我们正在处理的
item类型以及每种类型的处理方式不同。 -
数组之间的项目类型(文本字符串和图像)没有区别。我只有每个年级的数组。第一页上的每个链接都是一个年级。