User Tools

Site Tools


This is an old revision of the document!

AJAXify your Daily stats

Last week, we created a daily stats page. If you're like us, then you compulsively click refresh on this page way too often! So lets AJAX-ify this page so that it updates automatically every time someone makes an order.

Get jQuery

We're going to use jQuery for this page. jQuery isn't necessary for AJAX, you could just use plain javascript as well, but jQuery makes things much easier in general.

You can either download jQuery and host it on your own server, or use a CDN like Google or Microsoft. We use Google's CDN.

If you're hosting it on your own server, then add into the <head> section of daily_stats.php

<script src="https://you.path/to.jquery.js"></script>

or if you're using a CDN (this one is Google) then use

<script type="text/javascript" src=""></script>
<script src=""></script>

you have to replace YOUR_API_KEY with the API key you've obtained from Google.

Server Side PHP

At the top of daily_stats.php, add this code AFTER the require('includes/application_top.php');

$action = $_POST['action'];

if ($action != '')
    if ($action == 'latest')
       	$selectql = $db->Execute("select orders_id from orders ORDER BY date_purchased DESC LIMIT 1");
	echo json_encode($selectql->fields);
    elseif($action == 'stats')
        $selectql = $db->Execute('SELECT                                                                                                                                                                                                                                      
COUNT(orders.orders_id) as num,                                                                                                                                                                                                                                               
SUM(orders_total.value) as val,                                                                                                                                                                                                                                               
SUM(orders_total.value)/COUNT(orders.orders_id) as avg                                                                                                                                                                                                                        
FROM orders, orders_total                                                                                                                                                                                                                                                     
WHERE orders_total.title="Sub-Total:" AND orders.orders_id = orders_total.orders_id AND DATEDIFF(CURDATE(), orders.date_purchased) < 1');
        echo json_encode($selectql->fields);

This will make daily_stats.php echo out 2 different JSON files. 'latest' will be used for polling (we can poll the server every 5, 10, 15 seconds or so) and 'stats' will be triggered by new information in the polling event.

It eed to be after

Client Side Javascript

/home/ladyada/public_html/wiki/data/attic/tutorials/zencartmods/daily_ajax.html.1308756627.txt.gz · Last modified: 2016/01/28 18:05 (external edit)