Click here to Skip to main content
15,995,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
PHP
atg_id    atg_name    atg_order
1         Memory         2
2         Motherboard    3
3         Processor      4
4         Technical      1

attributes
PHP
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
PHP
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).
PHP
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
PHP
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
PHP
// Fetch Product Specifications
$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
PHP
{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}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900