PHP Sort files in a table

I currently have a table order on

ABCD EFGH 

but I would like to order a table

 AE BF CG DH 

the code:

 for($i=0;$i<=count($aFiles);$i++) { if($i%5==0) { echo "</tr><tr>"; } echo '<td><a>'.$aFiles[$i].'</a></td>'; } echo '</tr>'; echo '</table>'; 

Files are already sorted by az in $aFiles .

+4
source share
6 answers

 $aFiles = array('a','b','c','d', 'e', 'f', 'g'); $iColumnNumber = 2; $iRowMaxNumber = ceil(count($aFiles) / $iColumnNumber); $iTableRow = 0; $iTableColumns = 1; $aTableRows = array(); // make array with table cells foreach ($aFiles as $sFile) { if ($iTableRow == $iRowMaxNumber) { $iTableRow = 0; $iTableColumns++; } if (!isset($aTableRows[$iTableRow])) { $aTableRows[$iTableRow] = array(); } $aTableRows[$iTableRow][] = '<td>' . $sFile . '</td>'; $iTableRow++; } // if there is odd number of elements // we should add empty td elements for ($iTableRow; $iTableRow < $iRowMaxNumber; $iTableRow++) { if (count($aTableRows[$iTableRow]) < $iTableColumns) { $aTableRows[$iTableRow][] ='<td>empty</td>'; } } // display table echo '<table>'; foreach ($aTableRows as $aTableRow) { echo '<tr>'; echo implode('', $aTableRow); echo '</tr>'; } echo '</table>'; 
0
source

Assuming you know the number of lines you need, you don't have to resort to anything, just add some logic to your loop that the <td> generates:

 $size = count($aFiles); echo '<table><tr>'; for($i=0;$i<=ceil($size/2);$i++) { if($i%2==0) { echo '<td><a>'.$aFiles[$i].'</a></td>'; echo "</tr><tr>"; } else { if(isset($aFiles[$i+floor($size/2)])) { echo '<td><a>'.$aFiles[$i+floor($size/2)].'</a></td>'; } } } echo '</tr></table>'; 
0
source

Simple HTML trick approach:

 echo '<div class="tbl_group"><table>'; for($i=0;$i<=count($aFiles / 2);$i++) { echo '<tr>'; echo '<td><a>'.$aFiles[$i].'</a></td>'; echo '</tr>'; } echo '</tr>'; echo '</table></div>'; echo '<div class="tbl_group"><table>'; for($i=$aFiles / 2;$i<=count($aFiles);$i++) { echo '<tr>'; echo '<td><a>'.$aFiles[$i].'</a></td>'; echo '</tr>'; } echo '</tr>'; echo '</table></div>'; 

For CSS:

 .tbl_group { float: left; } 

With this approach, two tables are built side by side. float: left CSS will automatically adjust the position of the tables.

0
source

Not optimized, but this should work more or less:

 $aFiles = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N",); $rows = 4; $columns = floor((count($aFiles)/$rows))+1; echo '<table>'; for ($i=0;$i<$rows;$i++) { echo '<tr>'; for ($j=0;$j<$columns;$j++) { echo '<td>'; if (isset($aFiles[$i+$j*$rows])) echo $aFiles[$i+$j*$rows]; else echo "&nbsp;"; echo '</td>'; } echo '</tr>'; } echo '</table>'; 

You can change the number of rows if you want.

0
source

The example below also works with associative arrays + arrays with odd elements.

Code for associative AND indexed array:

 $aFiles = ['test'=>"A","B",'c'=>"C","D","E","F",'g'=>"G","H",'iii'=>"I"]; $aKeys = array_keys($aFiles); $aRows = ceil(count($aFiles) / 2); echo '<table>'; for($i=0; $i < $aRows; $i++) { echo '<tr>' . ' <td>' . $aFiles[$aKeys[$i]] . '</td>' . ' <td>' . (isset($aKeys[$i+$aRows]) ? $aFiles[$aKeys[$i+$aRows]] : 'empty') . '</td>' . '</tr>'; } echo '</table>'; 

Code for an ONLY indexed array:

 $aFiles = ["A","B","C","D","E","F","G","H","I"]; $aRows = ceil(count($aFiles) / 2); echo "<table>"; for($i=0; $i < $aRows; $i++) { echo "<tr>" . " <td>" . $aFiles[$i] . "</td>" . " <td>" . (isset($aFiles[$i+$aRows]) ? $aFiles[$i+$aRows] : "empty") . "</td>" . "</tr>"; } echo "</table>"; 

The output for both examples is identical:

 <table> <tr> <td>A</td> <td>F</td> </tr> <tr> <td>B</td> <td>G</td> </tr> <tr> <td>C</td> <td>H</td> </tr> <tr> <td>D</td> <td>I</td> </tr> <tr> <td>E</td> <td>empty</td> </tr> </table> 
0
source

Try it. Suppose your $ aFiles contain az in alphabetical order.

 for ($i = 0; $i < count($aFiles/2); $i++){ echo '<tr><td><a>'.$aFiles[$i].'</a><td><td><a>'.$aFiles[$i+13].'</a><td></tr>' } 
-one
source

Source: https://habr.com/ru/post/1502364/


All Articles