MySQL php从数据库中获取项目并以XML格式存储(重复项目)

时间:2017-03-07 13:48:48

标签: php mysql xml

首先,这是我的代码,并且它有效,只是有一个主要缺陷,我不知道如何解决。我已经清除了所有元素的代码,除了那些导致问题的元素以便更好地阅读。

    $servername = "localhost";
    $username = DB_USERNAME;
    $password = DB_PASSWORD;
    $database = DB_DATABASE;

    // Start of table variables

    // End of table variables
    // Create connection
    $conn = new mysqli($servername, $username, $password, $database);

    $sql = "SELECT pd.name as product_name, p.model as product_model, p.quantity as product_quantity, cd.name as category_name , p.product_id as product_id , p.price as product_price , m.name as product_manufacturer ,  p.image as product_image , pd.description as product_description
            FROM product p
            INNER JOIN product_description pd ON pd.product_id = p.product_id
            INNER JOIN product_to_category ptc ON ptc.product_id = p.product_id
            INNER JOIN category_description cd ON cd.category_id = ptc.category_id
            INNER JOIN manufacturer m ON m.manufacturer_id = p.manufacturer_id
            WHERE pd.language_id = 2 AND cd.language_id = 2 AND p.status = 1 AND p.product_id = p.product_id AND p.manufacturer_id = p.manufacturer_id";

    $conn->set_charset('utf8mb4');
    $result = $conn->query($sql);

    $xml = new XMLWriter();

    $xml->openURI("php://output");
    $xml->startDocument();
    $xml->setIndent(true);

    $xml->startElement('products');

    if ($result->num_rows > 0) {
        while ($product = $result->fetch_assoc()) {
            $product_id             = $product["product_id"];
            $product_category       = $product["category_name"];

                $xml->startElement("product");

                    $xml->startElement("itemid");
                        $xml->writeRaw($product_model);
                    $xml->endElement();


                    $xml->startElement("category");
                        $xml->writeCDATA($product_category);
                    $xml->endElement();
                    //end

                $xml->endElement();

        }
    }else{

    }

    $xml->endElement();

    $xml->flush();

?>

当我运行我的代码时,一切都开始,它生成和XML结构。我在下面发布的XML是第一个ID 23793

的产品

代码重复我的产品 3次,因为每个产品都有一个主要类别游戏,子类别 PS3 和子子类别的格斗

<product>
    <itemid>pls-23793</itemid>
    <category>
    <![CDATA[ Games ]]>
    </category>
</product>
<product>
    <itemid>pls-23793</itemid>
    <category>
    <![CDATA[ PS3 ]]>
    </category>
</product>
<product>
    <itemid>pls-23793</itemid>
    <category>
    <![CDATA[ Fighting ]]>
    </category>
</product>

这些都是数据库中的所有商店

示例:

这是INNER JOIN INNER JOIN product_to_category ptc ON ptc.product_id = p.product_id

product_to_category结构:

product_id | category_id

   23793   |  58  (This is the ID of Games)
   23793   |  135 (This is the ID of PS3)
   23793   |  777 (This is the ID of Fighting)

如何制作它以使其不会同时响应相同的产品3次,但对于产品23793,可以获得类似游戏&gt;的类别。 PS3&gt;战斗

我尝试在类别上做一个foreach循环,但我不认为它来自php,但问题来自MySQL选择代码

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

您需要在查询中添加GROUP_CONCAT

SELECT 
    pd.name as product_name, 
    p.model as product_model, 
    p.quantity as product_quantity, 
    GROUP_CONCAT(cd.name SEPARATOR ' > ') as category_name, 
    p.product_id as product_id, 
    p.price as product_price, 
    m.name as product_manufacturer,  
    p.image as product_image, 
    pd.description as product_description
FROM product p
INNER JOIN product_description pd ON pd.product_id = p.product_id
INNER JOIN product_to_category ptc ON ptc.product_id = p.product_id
INNER JOIN category_description cd ON cd.category_id = ptc.category_id
INNER JOIN manufacturer m ON m.manufacturer_id = p.manufacturer_id
WHERE 
    pd.language_id = 2 
    AND cd.language_id = 2 
    AND p.status = 1 
    AND p.product_id = p.product_id 
    AND p.manufacturer_id = p.manufacturer_id
GROUP BY p.product_id

答案 1 :(得分:0)

试试这个:

select case when@var <> brand then @Var :=p.product_id end as product_id
(
SELECT pd.name as product_name, p.model as product_model, p.quantity as product_quantity, cd.name as category_name , p.product_id as product_id ,     p.price as product_price , m.name as product_manufacturer ,  p.image as product_image , pd.description as product_description
FROM product p
INNER JOIN product_description pd ON pd.product_id = p.product_id
INNER JOIN product_to_category ptc ON ptc.product_id = p.product_id
INNER JOIN category_description cd ON cd.category_id = ptc.category_id
INNER JOIN manufacturer m ON m.manufacturer_id = p.manufacturer_id
WHERE pd.language_id = 2 AND cd.language_id = 2 AND p.status = 1 AND     p.product_id = p.product_id AND p.manufacturer_id = p.manufacturer_id
GROUP BY p.product_id
)
as foo
cross join (Select @var := '') as foo2

您可以在顶部选择中添加所需的其他字段,但这基本上可以消除第一个字段中产品的重复。