any ideas how to get orderTotal and orderId inside the tpl_checkout_success_default for the conversions tracking purposes ?
So far it looks like order id can be accessed by using this variable $zv_orders_id but how to get order total ?
will this code work:
$orders_query = "SELECT * FROM zen_orders WHERE orders_id = " . $zv_orders_id ." LIMIT 1";
$orders = $db->Execute($orders_query);
$order_total = $orders->fields['order_total'];
many thanks,
cheers
look in /includes/modules/pages/checkout_success/header_php.php
in there you will see the queries already being run by zencart to do with your order, and id say its already pulling out the info you want.
so you just need to set said data you need to a variable that you can then use in your tpl_checkout_success_default.php file.
eg, something like $customer_has_gv_balance, you will see where it is set in the hearder file and then used in the template file
heres something i found in order.php that would almost do it as is:
$order_total_query = "select text, value
from " . TABLE_ORDERS_TOTAL . "
where orders_id = '" . (int)$order_id . "'
and class = 'ot_total'";
$order_total = $db->Execute($order_total_query);
For a simple tracking code like one used for a shopping comparison site, I've used the following for the order ID and order amount. Use these in the tpl_checkout_success.php page
Order ID:
echo $zv_orders_id;
Use this select statement:
$to_send_sql = 'select REPLACE (text,"$","") text from orders_total where orders_id = '.$zv_orders_id.' and class = "ot_subtotal"';
$to_send= $db->Execute($to_send_sql);
Order amount:
echo $to_send->fields['text'];
Hope this helps someone!
Related
I'm trying to run a query like below:
SELECT (SUM(amount) - SUM(refundAmount)) as amount FROM orders WHERE $invoiceFilter AND $websiteFilter
It's getting the sum of the amount column correctly, but it's not subtracting out the refund amount.
What is the correct syntax for a query like this?
try this:
SELECT SUM(amount - refundAmount) AS `amount`
FROM orders
WHERE
invoice = $invoiceFilter
AND website = $websiteFilter;
You can use subqueries in the FROM clause.
SQL:
SELECT amount - refund
FROM (SELECT SUM(amount) as amount
FROM orders
WHERE invoice = 'invoiceFilter' AND website = 'websiteFilter') as a,
(SELECT SUM(refundAmount) as refund
FROM orders
WHERE invoice = 'invoiceFilter' AND website = 'websiteFilter') as b;
PHP:
mysql_query("SELECT amount - refund
FROM (SELECT SUM(amount) as amount
FROM orders
WHERE invoice = '" . mysql_real_escape_string($invoiceFilter). "'
AND website = '" . mysql_real_escape_string($websiteFilter). "') as a,
(SELECT SUM(refundAmount) as refund
FROM orders
WHERE invoice = '" . mysql_real_escape_string($invoiceFilter). "'
AND website = '" . mysql_real_escape_string($websiteFilter). "') as b; ");
You maybe missing something like a reference column to filter the data you need to process. Something like:
SELECT (SUM(amount) - SUM(refundAmount)) AS amount
FROM orders
WHERE
<a_column> = $invoiceFilter
AND <another_column> = $websiteFilter;
I have the follwing structure:
id,name,product,price
Now, I want to know how I can get the lowest value from price - and - get the name that belongs to the price. Here's a example:
0,seller1,cake,5
1,seller2,cake,2.50
Obviously seller2 has the lowest price. But I need to get that price - and the name that belongs to that price - and display it in PHP.
Something like this:
echo $seller . " sells " . $product . " for " . $price . ".";
I hope I have been clear enough.
Kind regards,
Hillebrand
The SQL to select what you need would be:
SELECT name, product, price FROM `table` ORDER BY price LIMIT 1
(Note that you didn't provide the table name so you'll need to replace table with the correct name.)
You can then use mysqli_stmt_fetch to fetch the results:
$stmt = $mysqli->prepare("SELECT name, product, price FROM `table` ORDER BY price LIMIT 1");
$stmt->execute();
$stmt->bind_result($seller, $product, $price);
$stmt->fetch();
echo $seller . ' sells ' . $product . ' for ' . $price . '.';
Keep in mind that this will only select the first product with the lowest price. You may need to consider how you'd like this to behave if you have two or more items which are equal in having the lowest price (e.g. 0). Should it display them all? Should there be some other field to signify precedence?
maybe
$table = mysql_query("SELECT name, price FROM table ORDER BY price ASC LIMIT 1");
while($y = mysql_fetch_object($table))
{
echo $y->name;
}
I'm having a little trouble with my rand() function. I have the following query:
$listTrainers = mysqli_query($conn, "SELECT emp_id FROM employees;");
while($fetchTrainers = mysqli_fetch_row($listTrainers))
{
echo 'ID: ' . $fetchTrainers['0']. '<br>';
}
This query returns me the id of all employees in the database, is there a way I can randomly select one of these id's and store it in a variable?
I am trying to use the following function:
echo(rand(begin, end));
where begin is the first element from the query and end is the last element
add this to your query:
ORDER BY RAND() LIMIT 1
You can do so within your query easily
SELECT emp_id FROM employees ORDER BY RAND() LIMIT 1
The easiest way is to do this directly in the database, to avoid getting all IDs if you only need one:
SELECT
emp_id
FROM
employees
ORDER BY
RAND()
LIMIT
1
If you do need all IDs, but additionally want to pick a random one, use this to avoid querying the database twice:
$listTrainers = mysqli_query($conn, "SELECT emp_id FROM employees;");
while($fetchTrainers = mysqli_fetch_row($listTrainers))
{
$id = $fetchTrainers[0];
echo 'ID: ' . $id . '<br>';
$ids[] = $id;
}
$randomId = $ids[array_rand($ids)];
I'm trying to select all of the products that a user has purchased from my site, based on an order ID.
$orderid = mysql_query("SELECT MAX(orders_id) FROM orders") or die(mysql_error());
$orderid = mysql_fetch_row($orderid);
$productinfo = mysql_query("SELECT products_model, products_name, products_price, products_quantity FROM orders_products WHERE orders_id=" . $orderid[0]);
$productinfo = mysql_fetch_row($productinfo);
echo $productinfo[0] . " | " . $productinfo[1] . " | $" . $productinfo[2] . " | " . $productinfo[3] . "<br><br>";
This will pull one product from the database, but if the customer ordered 8 items, I would need this to loop until all of the products with an order ID of $orderid[0] have been selected. What would be the best way to go about this? Any help is appreciated!
A few things.
First, everyone here will tell you to look into using mysqli or pdo as an alternative to mysql_query, as they are much more secure, and actually easier to maintain and use.
See this article:
http://www.pontikis.net/blog/how-to-use-php-improved-mysqli-extension-and-why-you-should
Second, you could easily cut down your code by writing more efficient queries.
In this case you would want to use a SQL JOIN. (more about joins here http://www.sitepoint.com/understanding-sql-joins-mysql-database/)
SELECT products_model, products_name, products_price, products_quantity
FROM orders_products as products
JOIN orders as orders on products.order_id = order.order_id
WHERE products.order_id = {whatever order id you are trying to find}
This will give you all products that have been ordered for each order id.
And use a while loop to loop through all of your results that come out of your query
<?
$tablae = mysql_query("SELECT * FROM order_history where (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "' GROUP BY user_id");
while ($order = mysql_fetch_array($tablae)) {
?>
<tr>
<?
$tablaes = mysql_query("SELECT * FROM members where id='$order[user_id]'");
$user = mysql_fetch_array($tablaes);
$idsd=$user['id'];
$rPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'");
$hdPaid = mysql_fetch_array($rPaid);
$sPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE user_id='$idsd' AND (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'");
while ($hPaid = mysql_fetch_array($sPaid)) {
?>
<td><?=$user['username']?></td>
<td><?=$hPaid['total']?></td>
<?
}
?>
</tr>
<? } ?>
This gets me this result http://dl.dropbox.com/u/14384295/test.jpeg
I want to order the price totals by DESC.
I would need
$sPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE user_id='$idsd' AND (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'");
the total on that to be ordered by DESC.
Be really carefull with GROUP BY instructions in your SQL query. All columns which are in the result and which are not aggregate expressions (expressions would be the count, SUM, max, etc working on the group and not on the rows) should be in your group by expression;
Here you use a select *, you should try to list the real columns instead, and get this list in your group by, or use only SELECT user_id.
Most database would prevent you of running such not-very-well-formted group by query, but MySQl is not bailing you, tthat does not mean he won't gives you completly wrong results if you do not rexpect this rule (all columns which are not aggregates must be in the group by).
Then you should be able to order by an agregate expression by reusing this expression and not his alias in the order clause.
You could either use client side sorting with javascript, there are some nice jQuery addons that can do that.
Or you have to totaly rewrite your code to have a single sql using joins and group by.
But I cannot realy follow the logic with $rPaid, $hPaid and $sPaid so I cannot help you there.