我需要像这个例子一样的代码。我需要限制每个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页 - &gt;类别1 [项目列表],类别2 [项目列表],类别3 [项目列表]
第2页 - &gt;类别4 [项目列表],类别5 [项目列表],类别6 [项目列表]
第x页 - &gt;类别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()无法帮助我,或者我没有使用它。
如何让这段代码在顶部做到。
如果您有消化症,请帮助我。 THX很多。
答案 0 :(得分:2)
$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 "\t\t<div> ", htmlspecialchars($category['category_name'], ENT_NOQUOTES, 'UTF-8'), " </div>\n";
}
echo "\t</li>\n";
}
echo "</ul>\n";