我需要像这个例子这样的代码。我需要限制每个 li 3 个类别。
<ul>
<li>
<div> Categpry 1 </div>
<div> Category 2 </div>
<div> Category 3 </div>
</li>
<li>
<div> Categpry 4 </div>
<div> Category 5 </div>
<div> Category 6 </div>
</li>
<li>
<div> Categpry y </div>
</li>
</ul>
页 1 -> 类别 1 [项目列表]、类别 2 [项目列表]、类别 3 [项目列表]
第 2 页 -> 类别 4 [项目列表]、类别 5 [项目列表]、类别 6 [项目列表]
页面 x -> 类别 y [项目列表]。
<?php
$category = array(
array('category_name' => 'Category1', 'channel_name' => 'Item 1'),
array('category_name' => 'Category3', 'channel_name' => 'Item 2'),
array('category_name' => 'Category1', 'channel_name' => 'Item 2'),
array('category_name' => 'Category3', 'channel_name' => 'Item 1'),
array('category_name' => 'Category2', 'channel_name' => 'Item 2'),
array('category_name' => 'Category4', 'channel_name' => 'Item 4'),
array('category_name' => 'Category5', 'channel_name' => 'Item 5'),
array('category_name' => 'Category1', 'channel_name' => 'Item 6'),
array('category_name' => 'Category3', 'channel_name' => 'Item 7'),
array('category_name' => 'Category2', 'channel_name' => 'Item 8'),
array('category_name' => 'Category4', 'channel_name' => 'Item 8'),
array('category_name' => 'Category3', 'channel_name' => 'Item 3'),
array('category_name' => 'Category5', 'channel_name' => 'Item 4'),
array('category_name' => 'Category6', 'channel_name' => 'Item 2'),
array('category_name' => 'Category2', 'channel_name' => 'Item 5'),
array('category_name' => 'Category2', 'channel_name' => 'Item 7')
);
$newOptions = array();
$i=1;
foreach ($category as $option) {
$cname = $option['category_name'];
$itemname = $option['channel_name'];
$newOptions[$cname][$i] = $itemname;
$i++;
}
?>
PHP array_chunk() 无法帮助我,或者我不直接使用它。
我怎样才能使这段代码像在上面一样。
如果你有建议,请帮助我。非常感谢。
$pagesize = 3;
$category_pages = array_chunk($category, $pagesize);
echo "<ul>n";
foreach ($category_pages as $page) {
echo "t<li>n";
foreach ($page as $category) {
echo "tt<div> ", htmlspecialchars($category['category_name'], ENT_NOQUOTES, 'UTF-8'), " </div>n";
}
echo "t</li>n";
}
echo "</ul>n";