Php Output Layout Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

Php Output Layout Help

Post 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,
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Php Output Layout Help

Post 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>";

?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

Re: Php Output Layout Help

Post by spec36 »

Thanks for the help
Post Reply