I’ve hit a bit of a dead end with this so I’m hoping someone can help me out. I’m not sure if this should be in a support forum or here.
So I’m getting my data from my database table then looping through the rows and setting keys and values in an array. (I think the problem is around here somewhere) There is an if statement so that blank fields aren’t included in the build of the array and table. Once the data is ready the script moves on to generating the table. The table header row is created using the array keys and then the rows and tables cells are populated.
The table is outputting fine, the problem is each row of data is the same. The first row of data is being repeated. I think the array values are being overwritten in the foreach loop but I’m not really sure. I think I need to get the data formatted so it is correctly read from $data.
This maybe more of a php issue than directly related to xPDO. I hope that all makes sense, it’s been a long day. Here is the code (sorry it’s a bit long), any help greatly appreciated.
<?php
$xpdo= new xPDO('mysql:host=localhost;dbname=database','username','password','dwcat_');
$xpdo->addPackage('dwcatalogue',$sources['model'],'dwcat_');
// Get products based on category id
$rows = $xpdo->getCollection('Products', array(
'cat_id' => $sectionId,
));
$data = array();
foreach ($rows as $row) {
$title = $row->get('title');
$data['code'] = $row->get('code');
if ($row->get('length')) {
$data['length'] = $row->get('length');
}
if ($row->get('width')) {
$data['width'] = $row->get('width');
}
}
// Create an XML object starting with the table tags
$table = new SimpleXMLElement('<table></table>');
// Add attributes to the table tag. Here we are adding a class.
$table->addAttribute('class','table-data');
// Add the header row
$table_row = $table->addChild('tr');
// Add a class to the header
$table_row->addAttribute('class','table-header');
// Loop through the array and build the headers based on the array keys
foreach(array_keys($data) as $head){
$row_col = $table_row->addChild('th', $head);
}
// Loop through each row in the data
foreach($rows as $row){
$table_row = $table->addChild('tr');
// Loop through each column
foreach(array_values($data) as $col){
$row_col = $table_row->addChild('td', $col);
}
}
// Print the HTML table on the page. This could also be returned in a function.
echo $table->asXML();
?>