2017-04-11 3 views
0

Ich versuche, eine Tabelle in PHP zu generieren.Erzeuge eine Tabelle mit PHP

Ich brauche diese Tabelle hat 365 Zellen.

Jede Zeile muss 30 Zellen enthalten.

Wie ist es möglich, bitte?

Eigentlich habe ich:

echo ' 
    <table class="table"> 
'; 

$dates = getDatesFromRange('2017-01-01', '2018-01-01'); 

$i=1; 
$limit=30; 

// $dates contains an array of 365 dates 
foreach($dates as $date){ 

    if($i <= $limit) { 
     echo '<td width="20">'.-.'</td>'; 
     $i++; 
    } 
    else { 
     echo '<tr><td width="20">'.-.'</td></tr>'; 
     $i=1; 
    } 
} 

echo ' 
    </table> 
'; 

Antwort

0

Verwendung verschachtelten Schleifen für diese:

$cells_per_row = 30; 
$rows = ceil(count($dates)/$cells_per_row); 

echo '<table class="table">'; 
for($i=0;$i<$rows;$i++){ 
    echo '<tr>'; 
    for($u=0;$u<$cells_per_row;$u++){ 
     if(!isset($dates[$i*$cells_per_row+$u])) // stop if end of array is reached 
     break; 
     echo '<td width="20">'.$dates[$i*$cells_per_row+$u].'</td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
+0

Oh wow! Es klappt. Danke, Mann. –