<?php
//===============================================================================
// List all orders from the database
//===============================================================================
    error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);      //all error but warnigns & notices

    session_start();                            //get a handle to session

    if (! $_SESSION['cust_id'])                 //if there is no session for customer                           
        header("Location: shop.php");           //redirect to login page
?>
<html>
<head>
<title>Retrieve data from database</title>
<style>
a     {text-decoration:none; color:brown}
table {font-family:arial; font-size:11pt; background-color:f9f9f9; border:2px solid brown}
td    {border:1px solid gray} 
</style>
</head>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>

<?php

    $data = array();                            #define an array
    
    read_data();
    display();

//===============================================================================
// Read all orders and build a 2 dimensional array
//===============================================================================
    function read_data()
    {
        global $data;   

        $host      = 'localhost';
        $DBname    = 'demo2';
        $DBuser    = 'demo2';
        $DBpswd    = 'demo2';

        if (!$_GET['sort'])                                      #if none provided 
             $_GET['sort'] = 'order_id-desc';                    #sort by order_id descending

        $sort  = explode('-', $_GET['sort']);                    #split on '-'
        $field = $sort[0]; 
        $seq   = $sort[1]; 
 
        $connect = mysqli_connect($host,$DBuser,$DBpswd,$DBname);   #connect to db server 
        if (! $connect) 
            die('Could not connect: ' . mysqli_connect_error());

        $query = "SELECT firstname,lastname,address,flavor,topping,creditCard,order_id 
                  FROM cust_order 
                  WHERE cust_id = $_SESSION[cust_id]            
                  ORDER BY $field $seq";                        

        $cursor = mysqli_query($connect,$query);                #execute the query
        if (! $cursor) 
            die('Could not execute query: ' . mysqli_error($connect));
       
        $i=0;
        while ($row = mysqli_fetch_array($cursor))              #get each row as an array
            $data[$i++] = $row;                                 #store row in 2 dim array    

        mysqli_free_result($cursor);                            #free result buffer
        mysqli_close($connect);                                 #close connection
    }

//===============================================================================
// Display all orders in an HTML table
// allow sorting of columns 
//===============================================================================
    function display()
    {
        global $data;   

        $sort = $_GET['sort'];

        $name_seq = ($sort =='lastname-asc')  ? 'lastname-desc'   : 'lastname-asc';
        $addr_seq = ($sort =='address-asc')   ? 'address-desc'    : 'address-asc';
        $flav_seq = ($sort =='flavor-asc')    ? 'flavor-desc'     : 'flavor-asc';
        $topg_seq = ($sort =='topping-asc')   ? 'topping-desc'    : 'topping-asc';
        $card_seq = ($sort =='creditCard-asc')? 'creditCard-desc' : 'creditCard-asc';
        $ordr_seq = ($sort =='order_id-asc')  ? 'order_id-desc'   : 'order_id-asc';

       print "<h3>$_SESSION[cust_fname], your orders are below...</h3>";

        print "<table width=800> 
               <tr bgcolor=tan>
               <th><a href=$_SERVER[PHP_SELF]?sort=$name_seq>Name</a>
               <th><a href=$_SERVER[PHP_SELF]?sort=$addr_seq>Address</a>     
               <th><a href=$_SERVER[PHP_SELF]?sort=$flav_seq>Flavors</a>     
               <th><a href=$_SERVER[PHP_SELF]?sort=$topg_seq>Toppings</a>    
               <th><a href=$_SERVER[PHP_SELF]?sort=$card_seq>Credit Card</a> 
               <th><a href=$_SERVER[PHP_SELF]?sort=$ordr_seq>Order Num</a> 
               <th width=110>Action</th> \n";

        foreach($data as $row)
        {
            $first      = $row[0];
            $last       = $row[1];
            $address    = $row[2];
            $flavor     = $row[3];
            $topping    = $row[4];
            $creditCard = $row[5];
            $order_id   = $row[6];
                
            $address2  = nl2br($address);                       #change all \n to <br>
            $flavor2   = str_replace("," , "<br>", $flavor);    #put multiple values
            $topping2  = str_replace("," , "<br>", $topping);   #on separate lines

            print "<tr valign=top>";
            print "
                   <td>$first $last</td><td>$address2</td><td>$flavor2</td>
                   <td>$topping2</td><td>$creditCard</td><td>$order_id</td>
                   <td><a href=shopDBUpd.php?order=$order_id><img src=update.gif title=update border=0></a>
                       <a href=shopDBUpd.php?order=$order_id><img src=delete.gif title=delete border=0></a></td> \n";              
        }    
        print "</table> \n";
    }

//===============================================================================

?>

<?php include "../../include.php"; ?>              <!-- hyperlink to see the code -->

<hr/>
<center>
<base href=/~sultans/php/demo/5session/shop/ >
<a href=shopCart.php>   shop          </a> |
<a href=shopDBAdd.php>  checkout      </a> |
                        list orders        |
<a href=shopDBSrch.php> search        </a> |
<a href=shopProf.php>   profile       </a> |
<a href=shop.php?out=y> logout        </a>
</center>
</body>
</html>