2017-07-20 4 views
0

Bitte Ich mag eine Tabelle erstellen mit PHP, aber ich fand es schwierig, dynamisch neue TABLE ROW hinzufügen, wenn die Schleife CELL bereits 4.Dynamische Tabellenzeile erstellen, wenn die Zelle bestimmte Grenze erreicht

Beispiel ist: Der Code unten wird mir diese:

<table> 
<td>CELL1</td><td>CELL2</td><td>CELL3</td><td>CELL4 </td><td>CELL5</td><td>CELL6</td><td>CELL7</td><td>CELL8</td> 
</table> 

Aber ich will, wenn die Zelle 4 ist es eine neue Zeile für andere Elemente

<table> 
<tr> 
<td>CELL1</td><td>CELL2</td><td>CELL3</td><td>CELL4 </td> 
</tr> 
<tr> 
<td>CELL5</td><td>CELL6</td><td>CELL7</td><td>CELL8</td> 
</tr> 
</table> 
schaffen

Mein Beispiel Code

<?php 
$string = 'wifi,pool,cafe,lunch, item1,item2,item3,item4,item5'; 
$tdlimit = 4; 
$converArry = explode(',', $string); 
$AddTable = '<table>'; 
foreach($converArry as $tds){ 
    //if(count_of_cell_is_4_create_tr){$AddTable .= '<tr>';} 
    $AddTable .= '<td>'.$tds.'</td>'; 
    //if(count_of_cell_is_4_create_tr){$AddTable .= '</tr>';} 
} 
$AddTable .= '</table>'; 
?> 

Antwort

0

Um Zeilen wie diese zu erstellen, müssen Sie nicht nur überprüfen, ob es bereits vier ist. Sie müssen überprüfen, ob es teilbar ist durch vier. Wenn Sie das nicht tun, erhalten Sie vier Elemente in der ersten Zeile, und viele davon bleiben in der zweiten Zeile, auch wenn das mehr als vier sind. Sie brauchen also eine Logik, um zu bestimmen, wann neue Zeilen zu beginnen sind.

Sie können dies tun, indem Sie auf den Index Ihres Arrays zugreifen und prüfen, ob es einen Rest gibt, wenn Sie ihn durch vier teilen.

<?php 
$string = 'wifi,pool,cafe,lunch, item1,item2,item3,item4,item5'; 
$tdlimit = 4; 
$converArry = explode(',', $string); 

// open a row at the beginning 
$AddTable = '<table><tr>'; 
foreach ($converArry as $i => $tds) { // add the array index to your loop definition 

    // if the index is a multiple of four, close the current row and open a new one 
    if ($i && ($i % 4 == 0)) $AddTable .= '</tr><tr>'; 
    $AddTable .= '<td>'.$tds.'</td>'; 
} 

// in case the total count of items is a multiple of four, close the last row 
if (($i + 1) % 4 == 0) $AddTable .= '</tr>'; 

$AddTable .= '</table>'; 
+0

Das ist sehr gut, das ist, was ich suche – Peter

+0

Großartig! Froh, dass ich helfen konnte. –

0

Versuchen Sie mit unten, ich habe es noch nicht versucht.

$string = 'wifi,pool,cafe,lunch, item1,item2,item3,item4,item5'; 
$tdlimit = 0; 
$converArry = explode(',', $string); 
$AddTable = '<table>'; 
$AddTable = '<tr>'; 
foreach($converArry as $tds){ 
    if($tdlimit%4 == 0){ 
     $AddTable .='</tr>'; 
     $AddTable .='<tr>'; 
    } 
    $AddTable .= '<td>'.$tds.'</td>'; 
    $tdlimit++; 
} 

$AddTable .='</tr>' 
$AddTable .= '<table>'; 
+0

Es funktionierte, aber wenn die Elemente 9 an der Zahl schafft es 4-Zellen in der ersten Reihe und 5 in der zweiten Reihe – Peter

+0

Dies ist aus dem Code zu setzen, kann es zusätzliche Zeilen für die letzten item5 erstellen da ich möchte, dass die Zelle 4 '

01 seinitem3
wifi Pool Café Mittagessen
item1 item2 item4 item5
' – Peter

+0

Bitte versuchen Sie die Bearbeitung! –

Verwandte Themen