Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

John Shofstall
John Shofstall
14,308 Points

SQL WHERE clause is causing results to not show up

I have a table wp_wps_orders that contains all of the orders processed on the site, and I'm writing this to export the orders. However, I want it to only export from the dates given by an admin user and when I put the WHERE clause in, it shows no orders. The "order_time" column is given it's value with the time() function and I have to convert the users date input to the same format using strtotime() but even though if I echo out the converted date, and it has a timestamp that coincides with the order_time timestamp, it doesn't show as a result. Here is my code

<?php
    $table = "wp_wps_orders";
    require_once("../../../wp-config.php");
    global $wpdb;
    $start = strtotime($_POST['startdate']);
    $end = strtotime($_POST['enddate']);

    $rows = array();

    $today = time();

    // Get (as an array of objects) the information you want to export
    $orders = $wpdb->get_results('SELECT * FROM '.$table.'WHERE order_time BETWEEN $start AND $end ORDER BY order_time DESC',OBJECT_K);

    // Put the retrieved data in CSV format
    foreach ($orders as $order) {
        $date = $order->order_time;
        $oDate = date('F j Y G:i',($order->order_time + nws_adjust_to_timezone()));

        $columns = array(); // used to clear the array on each pass
        $columns['oid'] = str_replace(',' , '' , $order->oid);
        $columns['who'] = str_replace(',' , '' , $order->who);
        $columns['user_id'] = str_replace(',' , '' , $order->user_id);
        $columns['l_name'] = str_replace(',' , '' , $order->l_name);
        $columns['f_name'] = str_replace(',' , '' , $order->f_name);
        $columns['state'] = str_replace(',' , '' , $order->state);
        $columns['zip'] = str_replace(',' , '' , $order->zip);
        $columns['town'] = str_replace(',' , '' , $order->town);
        $columns['country'] = str_replace(',' , '' , $order->country);
        $columns['email'] = str_replace(',' , '' , $order->email);
        $columns['telephone'] = str_replace(',' , '' , $order->telephone);
        $columns['net'] = str_replace(',' , '' , $order->net);
        $columns['tax'] = str_replace(',' , '' , $order->tax);
        $columns['amount'] = str_replace(',' , '' , $order->amount);
        $columns['d_option'] = str_replace(',' , '' , $order->d_option);
        $columns['p_option'] = str_replace(',' , '' , $order->p_option);
        $columns['txn_id'] = str_replace(',' , '' , $order->txn_id);
        $columns['pending_r'] = str_replace(',' , '' , $order->pending_r);
        $columns['terms'] = str_replace(',' , '' , $order->terms);
        $columns['dlinks_sent'] = str_replace(',' , '' , $order->dlinks_sent);
        $columns['voucher'] = str_replace(',' , '' , $order->voucher);
        $columns['voucher_amount'] = str_replace(',' , '' , $order->voucher_amount);
        $columns['cart_discount'] = str_replace(',' , '' , $order->cart_discount);
        $columns['tracking_id'] = str_replace(',' , '' , $order->tracking_id);
        $columns['order_time'] = $oDate;
        $columns['level'] = str_replace(',' , '' , $order->level);
        $rows[] = join(',',$columns); // fields are doing with a comma between them.
    }
    $file = 'Order ID, Who, User ID, Last Name, First Name, State, Zip, City, Country, Email, Phone, Net, Tax, Total Amount, Delivery Option, Payment Option, Taxation ID, Pending, Terms, Dlinks Sent, Voucher, Voucher Amount, Cart Discount, Tracking ID, Order Time, Level' . "\n" . join("\n",$rows); // rows are joined with a line-break between them.

    $filename = "MWM_Sales_".date("Y-m-d_H-i",time());
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header( "Content-disposition: filename=".$filename.".csv");
    print $file;
    exit;
?>