Php在循环内列出产品规格和类别 [英] Php listing product specifications and categories inside a loop

查看:74
本文介绍了Php在循环内列出产品规格和类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个修复程序,我希望在任何电子商务网站上显示产品规格。此处的属性组应该出现一次,包括其中的所有属性和值。但是,父属性组多次出现属性和值只有一次。它在这里以我期望的方式反向显示。为了解释我将尽可能清楚地包括我的数据库结构,显示的视图和我迄今为止尝试过的代码。为了与我在一起,我将首先从数据库结构开始。假设我有三张桌子。



attribute_groups

 atg_id atg_name atg_order 
1 内存 2
2 主板 3
3 处理器 4
4 技术 1



属性

 atr_id atr_group atr_name atr_order 
1 3 时钟速度 2
2 3 说明 1
3 3 号核心 3
4 4 测试 1 2
5 1 测试 2 1
6 4 测试 3 1



product_attributes

 pat_id pat_product pat_attribute pat_value 
1 1 1 2.8Ghz
2 1 2 一些描述
3 1 3 8
4 1 4 测试值 1
5 1 5 测试值 2



使用此信息我想显示这样的规格(如电子商务网站)。

< pre lang =PHP>技术
测试 1 测试值 1

内存
测试 2 测试值 2

处理器
时钟速度2.8Ghz
描述这个 一些描述
核心数 8



但是从我目前的代码来看,它看起来像这样

技术
测试 1 测试值 1

内存
测试 2 测试值 2

处理器
时钟速度2.8Ghz

处理器
描述这个一些描述

处理器
核心数 8





处理器属性组出现三次,其中包含属性和值,而不是一次出现一次属性在一起。



我尝试过:



我的PHP逻辑到现在为止

  //  获取产品规格 
$ 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->执行();

$ smarty-> assign(' attributes',$ specs) ;



视图的模板代码

 {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}

解决方案

specs =

pdo-> prepare( SELECT * FROM product_attributes
LEFT JOIN属性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'


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

// 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

{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}

解决方案

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',


这篇关于Php在循环内列出产品规格和类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆