爆炸结果然后内爆php

爆炸结果然后内爆php

本文介绍了爆炸结果然后内爆php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取一列的内容,该列的内容存储为 3,2,然后我使用爆炸函数将它们分开.然后使用分隔的值来查询另一个表以获取名称.接下来我想内爆像汉堡、薯条这样的结果.但是结果不是用逗号分隔而是像汉堡薯条...这是我所做的:

$sql = "从订单中选择商品";$result = mysqli_query($connection, $sql);$array = mysqli_fetch_assoc($result);$exploded = expand(",", $array['items']);foreach($爆炸为$row){$query = "SELECT food_name FROM foodlist WHERE food_id = $row";$res = mysqli_query($connection, $query);$arr = mysqli_fetch_assoc($res);$imploded = implode(", ", $arr);回声$内爆;}
解决方案

因为每个 $arr 只包含一种成分,所以你实际上是 implode()ing一个元素,呼应它,然后呼应其他没有被任何东西分开的成分,所以它们一起显示.

您可以将它们添加到数组中,然后内爆它们:

$sql = "从订单中选择商品";$result = mysqli_query($connection, $sql);$array = mysqli_fetch_assoc($result);$exploded = expand(",", $array['items']);$ingredients = [];foreach($爆炸为$row){$query = "SELECT food_name FROM foodlist WHERE food_id = $row";$res = mysqli_query($connection, $query);$arr = mysqli_fetch_assoc($res);$ingredients[] = $arr["food_name"];}回声内爆(", ", $ingredients);

专业提示,在数据库中用逗号分隔字段是个坏主意.您应该

和查询类似

从订单中选择成分o JOIN order_ingredient oi ON oi.order_id = o.id JOIN成分i ON oi.ingredient_id = i.id

只需一个查询即可返回您的所有成分.

I'm getting the contents of a column which has contents stored as 3,2 then I'm using the explode function to separate them. The values separated are then used to query another table to bring names. Next I want to implode the results like burger, fries. But the results are not separated by the comma but are like burgerfries... This is what I have done:

$sql = "SELECT items FROM orders";
$result = mysqli_query($connection, $sql);
$array = mysqli_fetch_assoc($result);

$exploded = explode(",", $array['items']);

foreach ($exploded as $row){
    $query = "SELECT food_name FROM foodlist WHERE food_id = $row";
    $res = mysqli_query($connection, $query);
    $arr = mysqli_fetch_assoc($res);

    $imploded = implode(", ", $arr);

    echo $imploded;

}
解决方案

Because every $arr only contains one ingredient, so you're actually implode()ing just one element, echoing it, and then echoing other ingredients not separated by anything, so they show together.

You can add them to an array and then implode them:

$sql = "SELECT items FROM orders";
$result = mysqli_query($connection, $sql);
$array = mysqli_fetch_assoc($result);

$exploded = explode(",", $array['items']);

$ingredients = [];
foreach ($exploded as $row){
    $query = "SELECT food_name FROM foodlist WHERE food_id = $row";
    $res = mysqli_query($connection, $query);
    $arr = mysqli_fetch_assoc($res);

    $ingredients[] = $arr["food_name"];
}

echo implode(", ", $ingredients);


PRO TIP, it's a bad idea to have your fields separated by a comma like that in the database. You should normalize that field, have another table called ingredients and a pivot table called order_ingredient containing every ingredient a specific order has, then you can simply JOIN the ingredients by ID and get them all at once, instead of doing N+1 queries which will kill your performance fast.

The structure would look like this:

And the query something like

SELECT ingredient FROM orders o JOIN order_ingredient oi ON oi.order_id = o.id JOIN ingredients i ON oi.ingredient_id = i.id

which will return all your ingredients with just one query.

这篇关于爆炸结果然后内爆php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:19