R
R
retrojdev2016-02-11 17:11:01
PHP
retrojdev, 2016-02-11 17:11:01

How to write an Opencart SQL query correctly?

Now I take all the information about the order from the oc_order table, these are: address, phone number, etc.

$result_for_sal = $this->db->query("SELECT * FROM oc_order_product WHERE order_id = (SELECT order_id FROM oc_order_product ORDER BY order_product_id DESC LIMIT 1)");

    $fileName =  $result_for_sal->row['order_id'].".sal";

    $id = $result_for_sal->row['order_id'];

    $result_for_base_sm = $this->db->query("SELECT * FROM oc_order WHERE order_id = '$id'");

    $firstname =  $result_for_base_sm->row["payment_firstname"].' '.$result_for_base_sm->row["payment_lastname"];
    
    $telephone = $result_for_base_sm->row["telephone"];
    $country  = $result_for_base_sm->row["payment_country"];
    $region  = $result_for_base_sm->row["payment_zone"];
    $city  = $result_for_base_sm->row["payment_city"];
    $address  = $result_for_base_sm->row["payment_address_1"];
    $email =  $result_for_base_sm->row["email"];
    $comment =  $result_for_base_sm->row["comment"];
    $points =  $result_for_sals->row["points"];
    $points =  $result_for_salss->row["price"];
    $sale_for = $result_for_base_sm->row["payment_method"];
    $sale_for = strip_tags($sale_for);

$str = <<<EOD
[Client] 
Name= $firstname
MPhone= $telephone
CPhone=
ZIP=
Country= $country 
Region= $region
City= $city
Address= $address
EMail= $email

[Options]
SaleType=1
Comment= $comment
OrderNumber= $id
DeliveryCondition= $sale_for
ReserveDate= 
EOD;
    for($i = 0; $i<count($result_for_sal->rows); $i++) {
      $points = $result_for_sal->rows[$i]['points'];
      $price = $result_for_sal->rows[$i]['price'];
      $counts = $result_for_sal->rows[$i]['quantity'];
$str .= <<<EOD
\n[$i]
GoodID=$points
Price=$price
Count=$counts
EOD;
    }
    $str = iconv("UTF-8", "Windows-1251", $str);
    file_put_contents('Orders/'.$fileName,$str);

Only here you need to display information from another table:
$points = $result_for_sal->rows[$i]['points'];
      $price = $result_for_sal->rows[$i]['price'];

points - points in OC
price - option price in OC
54a588f1b9034c8db2270fa65ad3f677.png
Or how to organize it correctly? Points - I chose this as a unique option ID, so as not to create an additional field.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eskrano, 2016-02-11
@eskrano

And what about opencart? When you make requests, the main thing is not to use anything, as the developers of this miracle did, but to think with your head.

K
Kirill Arutyunov, 2016-02-11
@arutyunov

Use nested queries or JOINs.
At first write "naked" requests. For example, in phpmyadmin. Then move those queries into code by replacing the data with variables.
If writing complex queries is not your forte, and this functionality will be rarely used, then just do 1-2 SELECT * for tables that contain points and price, and then combine 3 arrays in the code: your data, points and price.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question