简体   繁体   English

在PHP中显示具有相同id的行的值

[英]Display value of row with same id in PHP

I am trying to make a system which automatically prints out orders, and I am facing a problem right now. 我正在尝试建立一个自动打印订单的系统,我现在面临一个问题。 I am trying to make a string in a format which the printer is understanding, and right now, i need to gather the data from database before sending. 我试图以打印机理解的格式创建一个字符串,现在,我需要在发送之前从数据库中收集数据。 Now my problem is that in some cases the order_id is the same. 现在我的问题是在某些情况下order_id是相同的。 Here is an example: 这是一个例子:

+----------+----------------------+
| order_id |     product_name     |
+----------+----------------------+
|     1    |   Pizza with cheese  |
+----------+----------------------+
|     1    |    Coca-Cola Zero    |
+----------+----------------------+
|     2    |       Spaghetti      |
+----------+----------------------+
|     3    |        Lasagna       |
+----------+----------------------+

So what i am trying to is gather all the "product_name"'s in one string which have the same "order_id", so it display it as: "Pizza with cheese, Coca-Cola Zero" 所以我想要的是将所有“product_name”收集在一个具有相同“order_id”的字符串中,因此它显示为:“Pizza with cheese,Coca-Cola Zero”

Is there any way to do that? 有没有办法做到这一点?

Thanks in advance. 提前致谢。

EDIT: I don't know if I mentioned it above clearly, but I want to make a PHP script that are displaying it. 编辑:我不知道我是否清楚地提到它,但我想制作一个显示它的PHP脚本。 Sorry if I am confusing. 对不起,如果我很困惑。

You can select for this 你可以选择这个

select order_id, count(*), group_concat(product_name)
from orders
group by order_id
having count(*) > 1;

This gives you all order_id s which have multiple entries with the number of entries and the orders concatenated together. 这将为您提供所有order_id ,其中包含多个条目,其中条目数和订单连接在一起。 If you don't need the number leave out the count(*) in the column list. 如果您不需要该数字,请忽略列列表中的count(*)

If you want all orders leave out the having 如果您希望所有订单漏下having

select order_id, count(*), group_concat(product_name)
from orders
group by order_id

And using that from PHP 并使用PHP中的那个

$conn = new mysqli($host, $user, $pass, $db);
// use the appropriate select
$result = $conn->query('select order_id, group_concat(product_name) '
                       . 'from orders group by order_id');
while ($row = $result->fetch_array()) {
    // process the result row
}

This is of course just a brief outline. 这当然只是一个简短的概述。 You must add the error handling and fill in what you want to do with the result. 您必须添加错误处理并填写您想要对结果执行的操作。

You could use 你可以用

SELECT order_id, GROUP_CONCAT(product_name SEPARATOR ', ') AS product_names FROM orders GROUP BY order_id;

Example in PHP: PHP中的示例:

<?php
    $db_server = 'localhost';
    $db_user = 'user';
    $db_pass = 'pass';
    $db_name = 'database';

    $con = mysql_connect($db_server, $db_user, $db_pass)
        or die('Could not connect to the server!');

    mysql_select_db($db_name)
        or die('Could not select a database.');


    $sql = "SELECT order_id, GROUP_CONCAT(product_name SEPARATOR ', ')";
    $sql .= " AS product_names FROM orders GROUP BY order_id";

    $result = mysql_query($sql)
        or die('A error occured: ' . mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <title>test</title>
</head>

<body>
<h1>orders</h1>
<table>
   <tr><th>orders_id</th><th>product_names</th></tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
    printf("<tr><td>%d</td><td>%s</td></tr>\n", $row['order_id'],
                                                $row['product_names']);
}
?>
</table>
</body>
</html>

You should split up your data in two separate table, since there are relate 1:N where one order can have multiple products. 您应该将数据拆分为两个单独的表,因为存在关联1:N ,其中一个订单可以包含多个产品。

CREATE TABLE `order` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   .... any further information about the order,
   PRIMARY_KEY(`id`)
);

CREATE TABLE `order_product` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `order_id` int(11) DEFAULT NULL,
  `product_name` int(11) DEFAULT NULL,
   PRIMARY KEY (`id`),
   CONSTRAINT `order_product_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `order` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;  

that way you get no multiple orderIds for one order 这样你就不会得到一个订单的多个orderIds

Getting one order 获得一个订单

select * from order where id=1

Getting all products of an order 获取订单的所有产品

select op.* from order o inner join order_product op on op.orderId=o.id

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM