<html>
<head>
<title>Retrieve data from database</title>
<style>
    a     {text-decoration:none; color:brown}
    table {font:10pt arial; border:solid 2px black; border-collapse:collapse; margin-left:70px; background-color:#eeeeee; } 
    th,td {border:solid 1px black; padding-left:5px; padding-right:5px; vertical-align:top;}
</style>
</head>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>

<?php
    error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);      //all but warnings & notices

    $host = 'localhost';                        #local (either PC or NYU server)
//  $host = 'oit.scps.nyu.edu';                 #NYU server (explicit)
//  $host = 'oit.scps.nyu.edu:3306';            #NYU server (explicit with a port)

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

//===============================================================================
    function read_data()
    {
        global $host, $data;   

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

        if (!$_GET[sort])                                       #if none provided 
             $_GET[sort] = 'lastname_asc';                      #use lastname asc

        $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 
                  ORDER BY lower($field) $seq";                 #not case sensitive

        $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
    }

//===============================================================================
    function display()
    {
        global $data;   

        $sort = $_GET[sort];
        $upd  = $_GET[upd];

        $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';

        if ($upd)                                                       
            print "<h2>Select an order to update</h2>";
        else
            print "<h2>Retrieve from database with sort & update</h2>";
        
        print "<table> 
               <tr bgcolor=tan>
               <th>Order#</th>
               <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><i>Action</i></th> \n";

        foreach($data as $row)
        {
            $first      = $row[firstname];
            $last       = $row[lastname];
            $address    = $row[address];
            $flavor     = $row[flavor];
            $topping    = $row[topping];
            $creditCard = $row[creditCard];
            $order_id   = $row[order_id];
                
            $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>";
            print "
                   <td>$order_id</td><td>$first $last</td><td>$address2</td>
                   <td>$flavor2</td><td>$topping2</td><td>$creditCard</td>
                   <th><a  href=/~sultans/php/demo/4http/app/formToDBUpd.php?row=$order_id>
                       <img src=/~sultans/php/demo/4http/app/update.gif border=0></a>
                       <a  href=/~sultans/php/demo/4http/app/formToDBUpd.php?row=$order_id>
                       <img src=/~sultans/php/demo/4http/app/delete.gif border=0></a></th> \n";            
        }    
        print "</table> <hr /> \n";
    }

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

?>

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

<center>
<a href=/~sultans/php/demo/4http/app/formToDBAdd.php  >add order</a>       | 
list all orders                                                        |
<a href=/~sultans/php/demo/4http/app/getFromDBSrch.php>search</a>          |
<a href=/~sultans/php/demo/4http/app/formToDBUpd.php  >update order</a>
</center>
</body>
</html>