Page 1 of 1

Php Output Layout Help

Posted: Fri Apr 15, 2011 7:39 pm
by spec36
Hi All,

I have the following code:

Code: Select all

echo "<table border=\"1\">";
foreach($html->find('td[class=statBox]') as $stats){

//PUT in the delimeter
$add_delimeter = $stats->plaintext . "|";

$token = strtok($add_delimeter, '|');

	echo "<td>" . $token . "</td>";
}
	echo "</table>";
When this outputs to the screen the data looks like this (consider | a table cell below).

DATA | DATA | DATA | DATA | DATA | etc....

Of course it outputs this way, because this is what the above code is telling it to do, but I am having issues trying to make the data output a new table row after say every 5 tokens. So for example I want the output to look like this:

DATA | DATA | DATA | DATA |DATA
DATA | DATA | DATA | DATA |DATA
DATA | DATA | DATA | DATA |DATA
DATA | DATA | DATA | DATA |DATA
DATA | DATA | DATA | DATA |DATA
DATA | DATA | DATA | DATA |DATA

I'm stuck for how to do this. Any help would be greatly appreciated.

Thanks for your time,

Re: Php Output Layout Help

Posted: Sat Apr 16, 2011 3:52 am
by social_experiment
You can use a for loop. If the remainder of $i modolus 5 is 0, a closing and opening <tr> element is printed. This code untested though but the idea should work. Hth

Code: Select all

<?php 
echo "<table border=\"1\">";
// from the code $html->find('td[class=statBox]') is / or returns an 
// array
$count = count($html->find('td[class=statBox]');
echo '<tr>';
for ($i = 1; $i <= $count; $i++) {
foreach($html->find('td[class=statBox]') as $stats){

//PUT in the delimeter
$add_delimeter = $stats->plaintext . "|";

$token = strtok($add_delimeter, '|');

        echo "<td>" . $token . "</td>";
       if ($i % 5 == 0) {
        echo '</tr></tr>';
       }
}
}
       echo '</tr>';
        echo "</table>";

?>

Re: Php Output Layout Help

Posted: Sat Apr 16, 2011 11:52 am
by spec36
Thanks for the help