gpt4 book ai didi

php - 如何对输出进行分类

转载 作者:行者123 更新时间:2023-11-29 22:21:14 25 4
gpt4 key购买 nike

我想对我的输出进行分类,因为当前结果对我来说不是最好的。

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 |

关于php - 如何对输出进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30690925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com