PHP MySQL query statement problems - php

I've been trying to fix this PHP MySQL query all afternoon and am losing my mind! the following query works!:
$sql = "SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, o.shipping_code, o.total, o.channel, o.currency_code, o.currency_value, o.date_added, o.date_modified, c.image,
cg.name AS cgroup, CONCAT(o.payment_address_1, '<br>', o.payment_city, ', ', o.payment_zone, ' ', o.payment_postcode) AS address, o.telephone AS phone, o.email AS email,
(SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') .
"') AS order_status FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "customer` c ON(o.customer_id=c.customer_id) LEFT JOIN `" . DB_PREFIX . "customer_group_description`
cg ON(c.customer_group_id=cg.customer_group_id)";
but when I try to add the following to it it breaks.
(SELECT action FROM " . DB_PREFIX . "donate_history h LEFT JOIN " . DB_prefix . "order od ON (h.customer_id = od.customer_id)),
I dont if Im writing it right. I've rewritten it, removed parts, tried to make as simple as possible, but I can't fix it. This is what I've been trying :
$sql = "SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, o.shipping_code, o.total, o.channel, o.currency_code, o.currency_value, o.date_added, o.date_modified, c.image,
cg.name AS cgroup, CONCAT(o.payment_address_1, '<br>', o.payment_city, ', ', o.payment_zone, ' ', o.payment_postcode) AS address, o.telephone AS phone, o.email AS email,
(SELECT action FROM " . DB_PREFIX . "donate_history h LEFT JOIN " . DB_prefix . "order od ON (h.customer_id = od.customer_id)),
(SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') .
"') AS order_status FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "customer` c ON(o.customer_id=c.customer_id) LEFT JOIN `" . DB_PREFIX . "customer_group_description`
cg ON(c.customer_group_id=cg.customer_group_id)";

Looks like your new subquery you tried adding didn't have an alias.
SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, o.shipping_code, o.total, o.channel, o.currency_code, o.currency_value,
o.date_added, o.date_modified, c.image, cg.name AS cgroup, CONCAT(o.payment_address_1, '<br>', o.payment_city, ', ', o.payment_zone, ' ', o.payment_postcode) AS address,
o.telephone AS phone, o.email AS email,
(SELECT action
FROM " . DB_PREFIX . "donate_history h
LEFT JOIN " . DB_prefix . "order od
ON (h.customer_id = od.customer_id)) AS donate_history,
(SELECT os.name
FROM " . DB_PREFIX . "order_status os
WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') .
"') AS order_status
FROM `" . DB_PREFIX . "order` o
LEFT JOIN `" . DB_PREFIX . "customer` c
ON(o.customer_id=c.customer_id)
LEFT JOIN `" . DB_PREFIX . "customer_group_description` cg
ON(c.customer_group_id=cg.customer_group_id)

Related

Opencart 3.x: Change manual invoice in adm let disabled products to be added

hello i nee dto customize my opencart manual invoice in admin.
id like to add disbaled products and enabled to my invoice. but only admin in administration can make it.
i tried to change this file:
system/library/cart/cart.php and in line 41
i replaced the line 41 query to
if (isset($this->session->data['api_id'])) {
$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_store p2s LEFT JOIN " . DB_PREFIX . "product p ON (p2s.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND p2s.product_id = '" . (int)$cart['product_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
} else {
$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_store p2s LEFT JOIN " . DB_PREFIX . "product p ON (p2s.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND p2s.product_id = '" . (int)$cart['product_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.date_available <= NOW() AND p.status = '1'");
}
but its keepeing blocking me to add disabled products on admin manual invoice cart.
To check if admin is logged in use condition
if (isset($this->session->data['user_id']) && !empty($this->session- >data['user_token'])) {
// admin sql
}else{
// stock sql
}

How to get another column values from MySQL Query in opencart3 admin panel?

The code below uses 2 tables "category_path" and "category_description" to get id=>name of all categories and sub-categories. Im bad at mySql, so I would appreciate if you help me.
In this function I need to also get values of 'cat_name' column FROM THE OTHER (third) table named 'category'
https://i.stack.imgur.com/1PC4A.jpg
public function getCategories($data = array()) {
$sql = "SELECT cp.category_id AS category_id,
GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' > ') AS name,
c1.parent_id, c1.sort_order
FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id)
LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";
You are already using that category table in the first join and you gave it an alias of c1 so
SELECT cp.category_id AS category_id, c1.cat_name,
. . .
. . .

how to get affiliated name in Admin sale order list?

I need affiliated name in sale order list for corresponding orders on opencart.
I check the query on admin/model/sale/order.php
$sql = "SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS order_status, o.shipping_code, o.total, o.currency_code, o.currency_value, o.date_added, o.date_modified FROM `" . DB_PREFIX . "order` o";
I don't known how to add query for affiliate help me dudes.
Try this:
`$sql = "SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, (SELECT CONCAT(a.firstname, ' ', a.lastname) FROM ". DB_PREFIX."affiliate a WHERE a.affiliate_id = o.affiliate_id ) as affiliate_name, (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS order_status, o.shipping_code, o.total, o.currency_code, o.currency_value, o.date_added, o.date_modified FROM `" . DB_PREFIX . "order` o";`

opencart did not execute correct mysql query

I add a new column in orders table (shopper_id) I am trying to get the data with the new column condition but result returns empty
the query is correct and when I execute it directly it returns correct results
open cart formula
SELECT o.order_id, o.firstname, o.lastname, os.name as status, o.date_added,o.shopper_id as shopper_id , o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.store_id = '" . (int) $store_id . "' AND o.order_status_id = '" . (int) $status . "' AND os.language_id = '" . (int) $this->config->get('config_language_id') . "' AND ( o.shopper_id = NULL OR o.shopper_id = '0' ) ORDER BY o.order_id DESC
Below is my mysql query-
SELECT o.order_id, o.firstname, o.lastname, os.name as status, o.date_added,o.shopper_id, o.total, o.currency_code, o.currency_value FROM `medrahoc_order` o LEFT JOIN medrahoc_order_status os ON (o.order_status_id = os.order_status_id) WHERE o.store_id = '0' AND o.order_status_id = '1' AND os.language_id = '1' AND ( o.shopper_id = NULL OR o.shopper_id = 0 ) ORDER BY o.order_id DESC
my model function
public function getOrdersByStore($store_id, $status = null) {
if (isset($status)) {
$sql = "SELECT o.order_id, o.firstname, o.lastname, os.name as status, o.date_added,o.shopper_id as shopper_id , o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.store_id = '" . (int) $store_id . "' AND o.order_status_id = '" . (int) $status . "' AND os.language_id = '" . (int) $this->config->get('config_language_id') . "' AND o.shopper_id = '0' ORDER BY o.order_id DESC";
} else {
$sql = "SELECT o.order_id, o.firstname, o.lastname, os.name as status, o.date_added, o.total, o.currency_code, o.currency_value FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_status os ON (o.order_status_id = os.order_status_id) WHERE o.store_id = '" . (int) $store_id . "' AND o.order_status_id > '0' AND os.language_id = '" . (int) $this->config->get('config_language_id') . "' ORDER BY o.order_id DESC";
}
$query = $this->db->query($sql);
return $query->rows;
}

Opencart: Product Name Print Several times, How to fix.?

i had added the following Opencart module for my order report list...
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=3597&filter_search=order%20list%20filter%20model&page=4
I have problems with the column "Products". If there are more than one option the products name prints several times. So if I got a product with three options the product name prints three times. Is there any way to fix this problem?
i want print product name and model number only once, any idea.?
i will attach the results what i got now...
this is my sql query...
public function getOrders($data = array()) {
$sql = "select o.order_id,o.email,o.telephone,CONCAT(o.shipping_address_1, ' ', o.shipping_address_2) AS address,CONCAT(o.firstname, ' ', o.lastname) AS customer,o.payment_zone AS state,o.payment_address_2 AS block, o.payment_address_1 AS address,o.payment_postcode AS postcode,(SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS status,o.payment_city AS city,GROUP_CONCAT(pd.name) AS pdtname,GROUP_CONCAT(op.model) AS model,o.date_added,sum(op.quantity) AS quantity,GROUP_CONCAT(opt.value ) AS options, GROUP_CONCAT(opt.order_product_id ) AS ordprdid,GROUP_CONCAT(op.order_product_id ) AS optprdid, GROUP_CONCAT(op.quantity) AS opquantity from `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_product op ON (op.order_id = o.order_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id = op.product_id and pd.language_id = '" . (int)$this->config->get('config_language_id') . "') LEFT JOIN " . DB_PREFIX . "order_option opt ON (opt.order_product_id = op.order_product_id) ";
Product Name = GROUP_CONCAT(pd.name) AS pdtname,
Add DISTINCT
GROUP_CONCAT(DISTINCT pd.name)

Categories