Your code can be a little bit streamlined, right, but that's normal. There were already some hints about that you can fetch the data from the database more easily:
// Query the MySQL db
$sthandler = $dbhandler->prepare("SELECT col1, col2 FROM sampletable");
$sthandler->execute();
$allitems = $sthandler->fetchAll(PDO::FETCH_ASSOC);
Then you want to have both col1 and col2 in a paragraph of it's own. That's already per each sql result row, so this is fine.
Then you want to have three of those pairs per each table-row in the output. There is a function in PHP that does this for you:
array_chunk($allitems, $items_per_row, 1);
This will result in the table-rows you're looking for. As this might be empty but still would represent one table, I wrap it into an additional array again:
### PROCESS DATA (here obviously for viewing)
$allitems = array(array_chunk($allitems, $items_per_row, 1));
So now the data is in the right structure for the output. But for this data's presentation each element needs to get one HTML tag around it - recursively. From the outer to the inner the tags are:
### RENDERER
$tags = array('table', 'tr', 'td', 'p');
What's left is a decoration function to add these tags around each member:
$decorate = function($array, $tags, $f) {
$tag = array_shift($tags);
foreach($array as $element)
echo "<$tag>",
is_array($element)
? $f($element, $tags, $f)
: htmlspecialchars($element),
"</$tag>";
};
It will wrap the tags to the appropriate level of the array recursively, that means the function calls another function if there are more items inside (see the is_array test).
Now everything is ready to be fired up, the array get's decorated with the tags, the inner elements of the array will use the same function, so it's passed as parameter as well:
$decorate($allitems, $tags, $decorate);
Done. Demo. Example code at a glance:
<?php
### CONFIGURATION
$items_per_row = 3; // How many <td> I want to add for every <tr>
### GET DATA FROM STORE
IF (0):
// Query the MySQL db
$sthandler = $dbhandler->prepare("SELECT col1, col2 FROM sampletable");
$sthandler->execute();
$allitems = $sthandler->fetchAll(PDO::FETCH_ASSOC);
ELSE:
// Static test data
$allitems = array(
array('col1', 'col2'),
array('col1', 'col2'),
array('col1', 'col2'),
array('col1', 'col2'),
array('col1', 'col2'),
array('col1', 'col2'),
);
ENDIF;
### PROCESS DATA (here obviously for viewing)
$allitems = array(array_chunk($allitems, $items_per_row, 1));
### RENDERER
$tags = array('table', 'tr', 'td', 'p');
$decorate = function($array, $tags, $f) {
$tag = array_shift($tags);
foreach($array as $element)
echo "<$tag>",
is_array($element)
? $f($element, $tags, $f)
: htmlspecialchars($element),
"</$tag>";
};
$decorate($allitems, $tags, $decorate);
$allitems = $sthandler->fetchAll(PDO::FETCH_ASSOC);