我如何对输出进行分类

时间:2015-06-07 06:54:59

标签: php mysql loops if-statement foreach

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

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 |   -    |   - 

我希望有人可以帮助我,因为我尝试了很多变化,但没有成功。非常感谢!

1 个答案:

答案 0 :(得分:0)

我让你试图表明你的需要。

  • 您可以按行设置项目数(目前为3)
  • 您必须将此代码与结果查询相关联。
  • 如果查询结果不同 - &gt;修改$ 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 |