array to table with specified fields per row. list entries or images with
as many entries per row you specify. perfect for an image gallery where
you get a large array of entries and want to show i.e. 3 per row.
“entries per row” makes use of the built-in PHP functions
count( ) and
end( ).
01<?php 02 03// your data in an array 04$images=array( 05'here\'s', 06'some', 07'data', 08'or', 09'images', 10'you', 11'want', 12'to', 13'show', 14'with', 15'as', 16'many', 17'entries', 18'per', 19'row', 20'as', 21'you', 22'specify'); 23 24// how many entries per row to display 25$per_row=3; 26 27// display the table 28echo' 29<table style="width: 30em; margin: 1em; border: 0.1em solid #eee;">'; 30 31$count=0; 32$total=count($images); 33for($x=0;$x<$total;$x++) 34{ 35// if we start a line 36if($count==0) 37{ 38echo' 39 <tr>'; 40} 41 42// display the entry - change this to i.e. display image etc 43echo' 44 <td>'.$images[$x].'</td>'; 45 46// increase the count 47$count++; 48 49// if the max number per row is reached or it's the last entry 50if(($count==$per_row)||($images[$x]==end($images))) 51{ 52echo' 53 </tr>'; 54// reset the count 55$count=0; 56} 57} 58 59echo' 60</table>'; 61 62?>