Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create new row if more then X number of items

Status
Not open for further replies.

dexthageek

IS-IT--Management
Mar 23, 2002
59
US
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.

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>";
 
I already have a pagination script I wrote a few months back. However I do not want to change pages, I just want to display the content in another row.

So if I have 10 items and I want 4 items per row the output should be

item 1 item 2 item 3 item 4
item 5 item 6 item 7 item 8
item 9 item 10
 
Then have you tried maintaining a counter and outputting a "<tr>" and resetting the counter when the appropriate number of items have been displayed on a line?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Just before your loop, initialize a counter to zero. Inside the loop, every time you output an array element also increment that counter.

Inside your loop, after the output, add an if-statement that will test whether the counter has reached the value matching the number of columns you want to output. If it has, reset the counter to zero and output a "<tr>" tag.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Got it to work using the following code
thanks for the help
Code:
$maxItemsPerRow = "4";

$pets = array('dog','cat','mouse','snake','hamster','pig','cow');

$numberItemsInArray = count($pets);

print "<table width=\"300\" cellpadding=\"0\" cellspacing=\"0\">";

$count = 0;

print "<tr>";
for ($i = 0; $i <= $numberItemsInArray; $i++) {
 $count++;
 print "<td>" . $pets[$j] . "</td>";
                
 if($count == $maxItemsPerRow) {
  print "</tr><tr>";
  $count = 0;
 }
}       
print "</tr>";

print "</table>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top