我正在尝试构建一个显示书籍信息的简单页面,我打算将其全部存储在数据库中,但我决定构建一个多维数组并将其存储在其中会更好。
下面的示例中只有 4 本书,但最终可能会达到 100 本书。
我需要帮助迭代这个数组并在 HTML 中显示每个项目,<li>
但我只需要连续显示 4 个然后开始新行。有人可以告诉我如何做到这一点吗?
<?php
$books = array(
1 => array(
'name' => 'book 1 name here',
'url' => '',
'big_photo' => 'big photo of book 1',
'small_photo' => 'small photo of book 1',
'bio' => 'short bio 1'
),
2 => array(
'name' => 'book 2 name here',
'url' => '',
'big_photo' => 'big photo of book 2',
'small_photo' => 'small photo of book 2',
'bio' => 'short bio 2'
),
3 => array(
'name' => 'book 3 name here',
'url' => '',
'big_photo' => 'big photo of book 3',
'small_photo' => 'small photo of book 3',
'bio' => 'short bio 3'
),
4 => array(
'name' => 'book 4 name here',
'url' => '',
'big_photo' => 'big photo of book 4',
'small_photo' => 'small photo of book 4',
'bio' => 'short bio 4'
)
);
echo $books["1"]["small_photo"];
?>
示例输出
<ul>
<li>books data</li>
<li>books data</li>
<li>books data</li>
<li class="lastRow">books data</li>
<li>books data</li>
<li>books data</li>
<li>books data</li>
<li class="lastRow">books data</li>
<li>books data</li>
<li>books data</li>
<li>books data</li>
<li class="lastRow">books data</li>
</ul>