dexthageek
IS-IT--Management
I am in the process of writing a script that will take an array and output the results in a table. I have been successful in outputting the results in the same row. However, I want to start a new row, after it displays a specified number of items;
Example Output:
Number of Items Per Row: 4
dog cat mouse snake #then start a new line
hamster pig cow #end of table since no more items in array
Let me show you the code I have so far.
Example Output:
Number of Items Per Row: 4
dog cat mouse snake #then start a new line
hamster pig cow #end of table since no more items in array
Let me show you the code I have so far.
Code:
# Testing create new row if more then X number of items
$maxItemsPerRow = "3"; # Dont Forget 0 counts as 1
$pets = array('dog','cat','mouse','snake','hamster','pig','cow');
$numberItemsInArray = count($pets);
$numberRows = ceil($numberItemsInArray / $maxItemsPerRow);
print "<br><br>Array Contents<br><br>";
print "<br><br>";
print "Testing Creation of Table Rows";
print "<br>";
print "<table width=\"300\" cellpadding=\"0\" cellspacing=\"0\">";
for ($i = 1; $i <= $numberRows; $i++) {
print "<tr>";
for ($j = 0; $j <= $maxItemsPerRow; $j++) {
print "<td>" . $pets[$j] . "</td>";
}
print "</tr>";
}
print "</table>";