samedi 6 juin 2015

Q: How can I categorize outputs

I would like to categorize my outputs, because the current result is not the best for me.

Mysql table:

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

My current code:

<?php

$today = date('d');   
$counter = 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

?>

Currently I got this result:

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

I would like to get this result: (3 items in every row, and if category id changed then start a new row)

Cat 2
------

Liverpool - Man City   |  Arsenal - Swansea  | -

Cat 3
------

Bolton - Fulham  |  -    |  - 

Cat 4
------

Nagoya - Kashima | Kawasaki - Tokyo |  -  

Cat 6
------

Real Madrid - Elche |   -    |   - 

I hope somebody could help for me because I tried a lot of variations but without success. Many thanks!

Aucun commentaire:

Enregistrer un commentaire