简单而白痴的循环,我不知道该怎么做

发布于 2024-12-03 02:38:39 字数 912 浏览 0 评论 0原文

我必须迭代一个数组并显示它的项目。我必须对其进行分页,以便我可以渲染分页器。分页器结构类似于:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜警司 2024-12-10 02:38:39

假设你的问题不仅仅是一个简单的语法问题,我认为你需要
$arr[$i * 4 + $j]
而不是
$arr[$i * $j]
其中“4”是每行的大小。
看这个例子

0  1  2  3 (4 elements in this row which starts from 0)
4  5  6  7 (this row starts from 4)
8  9 10 11 (this starts from 4 * 2)

每行以 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

0  1  2  3 (4 elements in this row which starts from 0)
4  5  6  7 (this row starts from 4)
8  9 10 11 (this starts from 4 * 2)

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

记忆里有你的影子 2024-12-10 02:38:39
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>";

您忘记了分号和引号。

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>";

You forgot semicolons and quote.

晨曦÷微暖 2024-12-10 02:38:39

看起来您正在尝试将 $i$j 变量相乘,这可能不是您想要的:

echo "<img src='" . $arr[$i * $j] . "'/>"

您需要在此处放置的内容将取决于$arr 数组的结构。您的意思是:

$arr[$i][$j]

这适用于具有此结构的数组(仅是一个示例):

$arr[0][0] = 'page 1 image 1';
$arr[0][1] = 'page 1 image 2';
//....
$arr[1][3] = 'page 2 image 4';

It looks like you're trying to multiply the $i and $j variables, which probably isn't what you want:

echo "<img src='" . $arr[$i * $j] . "'/>"

What you need to put here will depend on the structure of the $arr array. Do you mean:

$arr[$i][$j]

That would work for an array with this structure (just an example):

$arr[0][0] = 'page 1 image 1';
$arr[0][1] = 'page 1 image 2';
//....
$arr[1][3] = 'page 2 image 4';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文