简体   繁体   English

打印产品属性及其值

[英]print product attributes with their values

How do I print the attributes and values together by placing each value under the attribute it belongs. 如何通过将每个值放在其所属的属性下来将属性和值一起打印。 So one attribute and several values under. 因此有一个属性和多个值。

//i can have the array the normal way if it helps, like [0] => Red...
[attribute] => Array
    (
        [0] => Array
            (
                [Colour] => Red
            )

        [1] => Array
            (
                [Colour] => Blue
            )

        [2] => Array
            (
                [Colour] => Green
            )

        [3] => Array
            (
                [Size] => Small
            )

        [4] => Array
            (
                [Size] => Medium
            )

        [5] => Array
            (
                [Size] => Large
            )

    )

If this can be done in the query by returning one attribute instead of multiple attributes and the values that go with it, that's great. 如果可以在查询中通过返回一个属性而不是多个属性以及与之相关的值来完成此操作,那就太好了。 But I'd also like to see this done by printing the array key once and then the values, I will +1 it. 但我也想通过先打印数组键然后再打印值来完成此操作,我将对其进行+1。

SELECT `attribute`, `value` 
FROM (`attribute_value`) 
JOIN `product_attr_value` ON `product_attr_value`.`attribute_value_id` = `attribute_value`.`id`  
JOIN `products` ON `products`.`id` = `product_attr_value`.`product_id` 
WHERE `products`.`item_name` LIKE '%qwe2%'

EDIT: 编辑:

file1 文件1

for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row);

return $set;

file2 文件2

foreach ($attr_value as $val)
{
echo $val['attribute'] ."<br>";

$values = $val['group_concat(`value`)'];
$value = explode(",", $values);
echo $value."<br>";
}

You want a group by : 您想要一个group by

SELECT attribute, group_concat(`value`)
FROM attribute_value av JOIN
     product_attr_value pav
     ON pav.attribute_value_id = av.id JOIN
     products p
     ON p.id = pav.product_id
WHERE p.`item_name` LIKE '%qwe2%'
group by attribute;

I also added table aliases to avoid repeating the long table names throughout the query. 我还添加了表别名,以避免在整个查询中重复长表名。

group_concat() will concatenate the values into a single string. group_concat()将这些值连接到一个字符串中。 By default, these are separated by a comma. 默认情况下,这些用逗号分隔。 You can control that with the SEPARATOR keyword. 您可以使用SEPARATOR关键字进行控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将带有值的产品属性添加到 Woocommerce 中的产品 - Add Product Attributes with values to a product in Woocommerce Woocommerce 在购物车和结帐时打印简单的产品属性,如变量属性 - Woocommerce print simple product attributes on cart and checkout, like variable attributes 从 Woocommerce 中的产品属性值过滤产品 - Filter products from product attributes values in Woocommerce Laravel Eloquent:产品、属性和属性值关系 - Laravel Eloquent: Product, Attributes and Attribute Values Relationship 通过 Woocommerce 中的产品属性 slug 值对获取产品变体 ID - Get product variation id by their product attributes slug values pairs in Woocommerce 获取 Woocommerce 中特定产品属性值的所有产品变体 - Get all the product variations for specific product attributes values in Woocommerce 显示 WooCommerce 产品循环中特定类别的产品属性值 - Display product attributes values for specific categories in WooCommerce product loops 以编程方式为可变产品设置变体默认属性值 - Set variations default attributes values for a variable product Programmatically 获取产品变体属性值术语ID和名称 - Get the product variations attributes values term ID and name 存储在哪里WooCommerce产品属性数据库中的术语值 - Where are stored WooCommerce Product Attributes term values in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM