samedi 6 juin 2015

How to display mysql result by category

I have problem with display results of mysql query. I would like to display only once the category code before the foreach loop running. I already tried to make this with if clause and foreach also, but without result. I need help of some php expert.

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

?>

My current result structure:

<table border="1px">
<tr>
  <td>Argentina Primera Division - 21:00<br>River Plate - Boca Juniors
</td>
<td>Brazil Serie A - 22:00<br>Flamengo - Gremio
</td>
<td>Brazil Serie A - 23:00<br>Fluminense - Coritiba
</td>

</tr>
<tr>
<td>England Premier League - 14:00<br>Liverpool - Manchester City
</td>
<td>England Championship - 16:00<br>Watford - Fulham
</td>
<td>England Championship - 16:00<br>Oldham - Bolton
</td>
</tr>
</table>

I would like to display structure like this:

<table border="1px">
    <tr>
      Argentina Primera Division
      <td>21:00<br>River Plate - Boca Juniors
    </td>
      <td width="200px">
        </td>
     <td width="200px">
        </td>
      </tr>
        </table>
<table border="1px">
  <tr>
        Brazil Serie A 
    <td>22:00<br>Flamengo - Gremio
    </td>
    <td>23:00<br>Fluminense - Coritiba
    </td>
        <td width="200px">
          </td>

    </tr>
  </table>
<table border="1px">
     <tr>
       England Premier League
    <td>14:00<br>Liverpool - Manchester City
    </td>
      <td width="200px">
         </td>
       <td width="200px">
         </td>
       </tr>
  </table>
<table border="1px">
  <tr>
    England Championship
    <td>16:00<br>Watford - Fulham
    </td>
    <td>16:00<br>Oldham - Bolton
    </td>
    <td width="200px">
      </td>
    </tr>
    </table>

I hope that some php expert could help for me to modify my foreach loop or add some if clauses. Many thanks!

Aucun commentaire:

Enregistrer un commentaire