I have run into a fix where I want to display product specifications like they display on any ecommerce sites. The attribute group here should appear once including all the attributes and values in it. But, the parent attribute group is appearing multiple times with attributes and value in it just once. Its being reverse displayed here with what I expected. To explain I will be as clear as possible including my database structures, displayed view and the codes I tried till now. To being with I will start with the database structure first. Suppose I have three tables.
attribute_groups
atg_id atg_name atg_order
1 Memory 2
2 Motherboard 3
3 Processor 4
4 Technical 1
attributes
atr_id atr_group atr_name atr_order
1 3 Clock Speed 2
2 3 Description 1
3 3 No. of Cores 3
4 4 Test 1 2
5 1 Test 2 1
6 4 Test 3 1
product_attributes
pat_id pat_product pat_attribute pat_value
1 1 1 2.8Ghz
2 1 2 This is some description
3 1 3 8
4 1 4 Test Value 1
5 1 5 Test value 2
Using this information I want to display specifications like this (as on ecommerce sites).
Technical
Test 1 Test Value 1
Memory
Test 2 Test value 2
Processor
Clock Speed 2.8Ghz
Description This is some description
No. of Cores 8
But as of my current code its appearing like this
Technical
Test 1 Test Value 1
Memory
Test 2 Test value 2
Processor
Clock Speed 2.8Ghz
Processor
Description This is some description
Processor
No. of Cores 8
The processor attribute group is appearing thrice with the attribute and value in it once instead of appearing once with all the three attributes together.
What I have tried:
My PHP logic till now
$specs = $pdo->prepare("SELECT * FROM product_attributes
LEFT JOIN attributes ON product_attributes.pat_attribute = attributes.atr_id
LEFT JOIN attribute_groups ON attributes.atr_group = attribute_groups.atg_id
WHERE pat_product = :id
ORDER BY attribute_groups.atg_order ASC");
$specs-> bindValue(':id', $_GET['id']);
$specs-> execute();
$smarty->assign('attributes', $specs);
Template code for view
{foreach $attributes as $attribute}
<h3>{$attribute.atg_name}</h3>
<table class="table table-striped table-hover">
<tr>
<td class="col-md-4">{$attribute.atr_name}</td>
<td>{$attribute.pat_value}</td>
</tr>
</table>
{/foreach}