我想对输出进行分类,因为当前结果对我而言不是最好的。
Mysql表:
DATE TITLE CAT
2012-11-23 | Liverpool - Man City | 2
2012-11-23 | Arsenal - Swansea | 2
2012-11-24 | Bolton - Fulham | 3
2012-11-23 | Nagoya - Kashima | 4
2012-11-23 | Kawasaki - Tokyo | 4
2012-11-24 | Real Madrid - Elche | 6
我当前的代码:
<?php
$today = date('d');
$field = 0;
foreach($data['posts'] as $p)
{
if (new DateTime() <= new DateTime("$p->date"))
{
$originalDate = "$p->date";
$newDate = date("H:i", strtotime($originalDate));
if ($field % 3 == 0) echo "<div class=\"row\">"; // start line before field 0 .. 3 .. 6 etc.
echo " <div class=\"col-md-4 portfolio-item\">
<h3><small><a href=\"".Url::post($p->id)."\">".$p->country.", ".Categories::name($p->cat)." - $newDate</small><br><h2><center><b>$p->title</b></center></h2> </a>
</h3>
</div>";
// output as table cell
if ($field % 3 == 2) echo "</div>"; // end line alter field 2 .. 5 .. 8 etc.
$field++; // increase field counter
}
}
if ($field % 3 != 0) echo "</div>"; // close last line, unless total count was multiple of 3
?>
目前,我得到了以下结果:
Cat 2 Liverpool - Man City | Cat 2 Arsenal - Swansea | Cat 3 Bolton - Fulham
Cat 4 Nagoya - Kashima | Cat 4 Kawasaki - Tokyo | Cat 6 Real Madrid - Elche
我想得到以下结果:(每行3个项目,如果类别ID更改,则开始新行)
Cat 2
------
Liverpool - Man City | Arsenal - Swansea | -
Cat 3
------
Bolton - Fulham | - | -
Cat 4
------
Nagoya - Kashima | Kawasaki - Tokyo | -
Cat 6
------
Real Madrid - Elche | - | -
我希望有人能为我提供帮助,因为我尝试了很多变体但没有成功。非常感谢!
最佳答案
我让您尝试显示您的需求。
您可以按行设置项目数(当前为3)
您必须将此代码与结果查询链接。
如果查询结果不同->修改$ row [number]
。
$array_test = array( array("2012-11-23", "Liverpool - Man City", 2),
array("2012-11-23", "Arsenal - Swansea", 2),
array("2012-11-23", "Arsenal - Swansea", 2),
array("2012-11-23", "Arsenal - Swansea", 2),
array("2012-11-24", "Bolton - Fulham", 3),
array("2012-11-23", "Nagoya - Kashima", 4),
array("2012-11-23", "Kawasaki - Tokyo", 4),
array("2012-11-24", "Real Madrid - Elche", 6));
$current_cat = 0;
$max_item_by_row = 3;
$curr_nb_item = 0;
foreach($array_test as $row)
{
if(($curr_nb_item % $max_item_by_row) == 0)
{
// Case : new cat & 3 lines where displayed
// EDITED here
if($current_cat != $row[2])
{
$current_cat = $row[2];
echo "<br>" . "<br>" . "Cat " . $row[2] . "<br>" . "------" . "<br>" . "<br>";
echo $row[1] . " | ";
++$curr_nb_item;
}
// EDIT : else block added
// Case : same Cat & nb item by row reached
else
{
echo "<br>" . "<br>" . $row[1] . " | ";
++$curr_nb_item;
}
// EDIT : Missing this brace :(
}
else
{
// Case : New cat BUT incomplete curr line - Fill with " - | "
if($current_cat != $row[2])
{
$current_cat = $row[2];
while((($curr_nb_item + 1) % $max_item_by_row) != 0)
{
//echo " value : " . $curr_nb_item;
echo "- | ";
++$curr_nb_item;
}
// Reset curr_nb_item - Otherwise misscount to fill next incomplete lines
$curr_nb_item = 0;
echo "-";
// Display New Cat
echo "<br>" . "<br>" . "Cat " . $row[2] . "<br>" . "------" . "<br>" . "<br>";
echo $row[1] . " | ";
++$curr_nb_item;
}
// Case : same cat + curr_nb_item < 3 - Standard display
else
{
echo $row[1] . " | ";
++$curr_nb_item;
}
}
}
?>
显示方式
Liverpool - Man City | Arsenal - Swansea | Arsenal - Swansea |
Arsenal - Swansea | - | -
Cat 3
------
Bolton - Fulham | - | -
Cat 4
------
Nagoya - Kashima | Kawasaki - Tokyo | -
Cat 6
------
Real Madrid - Elche |