Hello! I'm a newbie. I am writing a PHP store. I did everything, but I can't get orders to view.

The code is attached. I am writing on a primitive procedural php, even without functions, because I still cannot figure out how to output everything correctly.

There are 2 tables:

  1. Orders table orders with fields
    • id_order
    • name_order (customer name)
    • e-mail (customer's email)
    • phone
    • address
    • customer (order number from session)
    • id_good (product id)
    • quantity (quantity of goods)
    • datetime (order date)
  2. Product table good , which contains id_good , name_good and other fields.

I want to withdraw orders in this way:

 Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: 18qskgmvp0i4133sbqfct1c0j3 Имя Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: Коля E-mail: kolia@mail.ru Π’Π΅Π»Π΅Ρ„ΠΎΠ½: 99999999999999 АдрСс: Π³. Π’ΠΎΡ€ΠΊΡƒΡ‚Π°, ΡƒΠ». МСдвСдСй, 77 Π”Π°Ρ‚Π° Π·Π°ΠΊΠ°Π·Π°: 31-08-2016 19:21:37 НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π°: Π’ΠΎΠ²Π°Ρ€ 4 ΠšΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡ 1 ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ: 4 Π¦Π΅Π½Π°: 5 Ρ€ΡƒΠ±. Π‘ΡƒΠΌΠΌΠ°: 20 Ρ€ΡƒΠ±. НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π°: Π’ΠΎΠ²Π°Ρ€ 3 ΠšΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡ 1 ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ: 1 Π¦Π΅Π½Π°: 4 Ρ€ΡƒΠ±. Π‘ΡƒΠΌΠΌΠ°: 4 Ρ€ΡƒΠ±. 

And I can only get this way:

 Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: 18qskgmvp0i4133sbqfct1c0j3 Имя Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: Коля E-mail: kolia@mail.ru Π’Π΅Π»Π΅Ρ„ΠΎΠ½: 99999999999999 АдрСс: Π³. Π’ΠΎΡ€ΠΊΡƒΡ‚Π°, ΡƒΠ». МСдвСдСй, 77 Π”Π°Ρ‚Π° Π·Π°ΠΊΠ°Π·Π°: 31-08-2016 19:21:37 НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π°: Π’ΠΎΠ²Π°Ρ€ 4 ΠšΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡ 1 ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ: 4 Π¦Π΅Π½Π°: 5 Ρ€ΡƒΠ±. Π‘ΡƒΠΌΠΌΠ°: 20 Ρ€ΡƒΠ±. Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: 18qskgmvp0i4133sbqfct1c0j3 Имя Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: Коля E-mail: kolia@mail.ru Π’Π΅Π»Π΅Ρ„ΠΎΠ½: 99999999999999 АдрСс: Π³. Π’ΠΎΡ€ΠΊΡƒΡ‚Π°, ΡƒΠ». МСдвСдСй, 77 Π”Π°Ρ‚Π° Π·Π°ΠΊΠ°Π·Π°: 31-08-2016 19:21:37 НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π°: Π’ΠΎΠ²Π°Ρ€ 3 ΠšΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡ 1 ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ: 1 Π¦Π΅Π½Π°: 4 Ρ€ΡƒΠ±. Π‘ΡƒΠΌΠΌΠ°: 4 Ρ€ΡƒΠ±. 

How to withdraw orders that customer details were repeated once, and under them all ordered goods were displayed? Code below:

 global $link; $sql_order = "SELECT * FROM orders"; // Π²Ρ‹Π²ΠΎΠ΄ Π΄Π°Π½Π½Ρ‹Ρ… Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ° $query_order = mysqli_query($link, $sql_order) or die(mysqli_error($link)); $num_order = mysqli_num_rows($query_order); for($i = 0; $i <= $num_order; $i++){ $row_order = mysqli_fetch_assoc($query_order); echo '<h4>Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: ' .$row_order['customer'].'<br />Имя Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: '.$row_order['name_order'].'<br />E-mail: '.$row_order['email'].'<br />Π’Π΅Π»Π΅Ρ„ΠΎΠ½: ' .$row_order['phone'].'<br />АдрСс: '.$row_order['address'].'<br />Π”Π°Ρ‚Π° Π·Π°ΠΊΠ°Π·Π°: '.date("dmY H:i:s", $row_order['datetime']). '</h4>'; echo '<br />'; $id_good = $row_order['id_good']; $sql_good = "SELECT * FROM goods WHERE id_good = '$id_good'"; // Π²Ρ‹Π²ΠΎΠ΄ Ρ‚ΠΎΠ²Π°Ρ€ΠΎΠ² $query_good = mysqli_query($link, $sql_good) or die(mysqli_error($link)); while($row_good = mysqli_fetch_assoc($query_good)){ echo 'НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π°: '.$row_good['name_good']; echo '<br />'; echo 'ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ: '.$row_order['quantity']; echo '<br />'; echo 'Π¦Π΅Π½Π°: '.$row_good['price_good'].' Ρ€ΡƒΠ±.'; echo '<br />'; $sum = $row_good['price_good']*$row_order['quantity']; echo $sum.' Ρ€ΡƒΠ±.'; echo '<br />'; } echo '<hr />'; } 
  • I will give advice - start your career correctly: learn patterns, OOP helps, collectors use, etc. Do not start the programmer’s way down. Such code, except in the 90s, was normal and it’s not a novice. - user207618
  • To learn the difficult, you need to understand the simple. I have not yet understood the procedural style, and the PLO is a dark forest for me. - Anastasia
  • It would be nice if you had some of the most table structures and at least some test data thrown at some sqlfiddle.com -
  • On the very test data everything is working fine phpfiddle.org/main/code/03hr-m90n ....... maybe the fact is that you need not $i <= $num_order; write, and $i < $num_order; ...... but for accuracy, of course, we need demo data tables ........... something like this sqlfiddle.com/#!9/db6c7 - Alexey Shimansky
  • I never wrote such a code, but oh well. I just mean that many people are satisfied with this, there is a similar code even in popular and popular products (hello, WP). I would not want a newbie to consider this the norm. - user207618

2 answers 2

In the current view, you can only do this:

To get non-duplicate customers from orders , then select all orders from the orders table by orders product data. Then in a cycle run through all customers and inside already under all orders. If the customer ID matches, then we display information. And if this is the first line - then we display at the beginning of the customer information. It looks like a crutch and horrible zhuuuuuuuuuuko, but I personally with the current structure of the database could not think of anything better.

 global $link; $sql = 'SELECT DISTINCT customer FROM orders'; $customers = []; if ($result = mysqli_query($link, $sql)) { while ($row = mysqli_fetch_assoc($result)) { $customers[] = $row['customer']; } mysqli_free_result($result); /* echo '<pre>'; print_r($customers); echo '</pre>'; */ } $sql = 'SELECT * FROM orders LEFT JOIN goods USING(id_good)'; $orders = []; if ($result = mysqli_query($link, $sql)) { while ($row = mysqli_fetch_assoc($result)) { $orders[] = $row; } mysqli_free_result($result); /* echo '<pre>'; print_r($orders); echo '</pre>'; */ } foreach ($customers as $customer) { $k = 0; foreach ($orders as $order) { if ($customer == $order['customer']) { $info = ''; if ($k == 0) { $info .= '<h4>Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: '.$order['customer'] .'<br />Имя Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: '.$order['name_order'] .'<br />E-mail: '.$order['email'] .'<br />Π’Π΅Π»Π΅Ρ„ΠΎΠ½: '.$order['phone'] .'<br />АдрСс: '.$order['address'] .'<br />Π”Π°Ρ‚Π° Π·Π°ΠΊΠ°Π·Π°: '.date("dmY H:i:s", $order['datetime']) .'</h4><br />'; } $info .= 'НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π°: '.$order['name_good'] .'<br />ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ: '.$order['quantity'] .'<br />Π¦Π΅Π½Π°: '.$order['price_good'].' Ρ€ΡƒΠ±.' .'<br />'.($order['price_good'] * $order['quantity']).' Ρ€ΡƒΠ±.<br /><br />'; echo $info; $k++; } } echo '<hr />'; } 

You can touch it here: http://phpfiddle.org/main/code/p9dg-mqbi Clicking on Run (I added more orders from user 123)

In general, the structure of the database should be changed. For example, in the orders field customer data is superfluous. They should not be there. Must be a customer ID only. And his data should already be in the customer table for example. In general, with various improvements and restructuring of the database, it is possible to fit everything in one request and it is easier to work.

UPD (from @Mike): With the current database structure in one order physically there can not be more than one product. Since the product ID and its quantity is directly in the order table. I suggest the following changes:

  1. Remove the id_good and quantity columns from the orders table
  2. Create table "Products in order":

     CREATE TABLE IF NOT EXISTS `order_goods` ( `id_order` int NOT NULL, `id_good` int NOT NULL, `quantity` int NOT NULL, PRIMARY KEY (`id_order`,`id_good`), FOREIGN KEY (`id_order`) REFERENCES orders(`id_order`), FOREIGN KEY (`id_good`) REFERENCES goods(`id_good`) ) 

Then the request for receiving at the same time orders and goods in them will be:

 select O.*, T.quantity, G.price_good, G.name_good from orders O left join order_goods T on T.id_order=O.id_order left join goods G on G.id_good=T.id_good 

At the output, he will give as many lines with the order as the number of goods in the order (or 1 if there are no goods).

  • Alexey, you are a genius! Once again, thank you for editing the code. If I can thank you somehow, say, to throw you a payment on a phone or e-wallet, then let me know. - Anastasia

In this case, you repeat the whole cycle as many times as there are goods, and you need to make a cycle in the if construction. From the category: if there is an order we print out (we check the sql with the query you already have):

 `$row_order = mysqli_fetch_assoc($query_order); echo '<h4>Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: ' .$row_order['customer'].'<br />Имя Π·Π°ΠΊΠ°Π·Ρ‡ΠΈΠΊΠ°: '.$row_order['name_order'].'<br />E-mail: '.$row_order['email'].'<br />Π’Π΅Π»Π΅Ρ„ΠΎΠ½: ' .$row_order['phone'].'<br />АдрСс: '.$row_order['address'].'<br />Π”Π°Ρ‚Π° Π·Π°ΠΊΠ°Π·Π°: '.date("dmY H:i:s", $row_order['datetime']). '</h4>'; echo '<br />'; $id_good = $row_order['id_good'];` 

And then in the body of this same if we set your for cycle (with the same conditions as it is), it will already go through only commodity items, and the header will be one.