简单而白痴的循环,我不知道该怎么做
我必须迭代一个数组并显示它的项目。我必须对其进行分页,以便我可以渲染分页器。分页器结构类似于:
<div class='paginator'>
<div class="page">
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
</div>
<div class="page">
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
</div>
</div>
最后,我有一个包含路径的 PHP 数组。我想要这样的东西:
echo "<div class='paginator'>";
for ($i = 0; $i < 3; $i++) {
echo "<div class='page'>";
for($j = 0; $j < 4; $j++) {
echo "<img src='" . $arr[$i * $j] . "'/>"
}
echo "</div>
}
echo "</div>";
但是,它不起作用。我错误地执行了循环,并且不知道它们应该如何。
I have to iterate over an array and display its items. I have to paginate it so I can render a paginator. The paginator structure is something like:
<div class='paginator'>
<div class="page">
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
</div>
<div class="page">
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
<img src="path/to/picture"/>
</div>
</div>
Finally, I have a PHP array with the paths. I want something like this:
echo "<div class='paginator'>";
for ($i = 0; $i < 3; $i++) {
echo "<div class='page'>";
for($j = 0; $j < 4; $j++) {
echo "<img src='" . $arr[$i * $j] . "'/>"
}
echo "</div>
}
echo "</div>";
But, it doesn't work. I'm doing the loops incorrectly and I can't figure out how they should be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设你的问题不仅仅是一个简单的语法问题,我认为你需要
$arr[$i * 4 + $j]
而不是
$arr[$i * $j]
其中“4”是每行的大小。
看这个例子
每行以 4(每行元素数)乘以行数(从 0 开始)开始。要迭代行中的每个元素,您必须添加行中元素的索引(示例中为 $j),如下所示:
$index = $row_index * $elements_per_row + $column_index
Assuming yours is not just a simple sintax problem I think you need
$arr[$i * 4 + $j]
instead of
$arr[$i * $j]
Where "4" is the size of each row.
Look at this example
Each row starts with 4 (number of elements per row) multiplied for the number of the row (starting from 0). To iterate each element in the row you have to add the index of the element in the row ($j in your example) so:
$index = $row_index * $elements_per_row + $column_index
您忘记了分号和引号。
You forgot semicolons and quote.
看起来您正在尝试将
$i
和$j
变量相乘,这可能不是您想要的:您需要在此处放置的内容将取决于
$arr
数组的结构。您的意思是:这适用于具有此结构的数组(仅是一个示例):
It looks like you're trying to multiply the
$i
and$j
variables, which probably isn't what you want:What you need to put here will depend on the structure of the
$arr
array. Do you mean:That would work for an array with this structure (just an example):