<!--
#=====================================================================================
# Get data from a database
# Display to an html form
# Allow filtering through the data
#=====================================================================================
-->
<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>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>

<?php
    error_reporting(0);

    $host = 'localhost';                        #local (either PC or NYU server)

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


//===================================================================================
//read_data: read data from a database
//===================================================================================
function read_data()
{ 
        global $host, $data;
        global $ice_vanil,$ice_choco,$ice_straw,$ice_pecan,$ice_rocky,$ice_frnch,$ice_pstch;
        global $top_fudge,$top_sprkl,$top_nuts,$top_cream;

        $host_port = $host;
        $DBname    = 'demo2';
        $DBuser    = 'demo2';
        $DBpswd    = 'demo2';
 
        $connect = mysqli_connect($host_port,$DBuser,$DBpswd,$DBname);   #connect to db server
 
        if (! $connect) 
            die('Could not connect: ' . mysqli_connect_error());

        $ice_vanil = $_GET['ice_vanil'];                              #get the search criterea
		$ice_choco = $_GET['ice_choco'];
		$ice_straw = $_GET['ice_straw'];
		$ice_pecan = $_GET['ice_pecan'];
		$ice_rocky = $_GET['ice_rocky'];
		$ice_frnch = $_GET['ice_frnch'];
		$ice_pstch = $_GET['ice_pstch'];
		
		$top_fudge = $_GET['top_fudge'];
		$top_sprkl = $_GET['top_sprkl'];
		$top_nuts  = $_GET['top_nuts'];
		$top_cream = $_GET['top_cream'];
     
        $sql = "SELECT firstname,lastname,address,flavor,topping,creditCard,order_id  
                  FROM   cust_order 
                 WHERE  1=1 
                   AND ( ";
        if ($ice_vanil) $sql .= "flavor  like  '$ice_vanil%' AND ";                         
        if ($ice_choco) $sql .= "flavor  like '%$ice_choco%' AND ";                         
        if ($ice_straw) $sql .= "flavor  like '%$ice_straw%' AND ";                         
        if ($ice_pecan) $sql .= "flavor  like '%$ice_pecan%' AND ";                         
        if ($ice_rocky) $sql .= "flavor  like '%$ice_rocky%' AND ";                        
        if ($ice_frnch) $sql .= "flavor  like '%$ice_frnch%' AND ";                         
        if ($ice_pstch) $sql .= "flavor  like '%$ice_pstch%' AND ";                         
        if ($top_fudge) $sql .= "topping like '%$top_fudge%' AND ";                         
        if ($top_sprkl) $sql .= "topping like '%$top_sprkl%' AND ";                         
        if ($top_nuts)  $sql .= "topping like '%$top_nuts%'  AND ";                         
        if ($top_cream) $sql .= "topping like '%$top_cream%' AND ";                         
        $sql .= "1=1) ";
        $sql .= "ORDER BY order_id";
        
//      print $sql;                                             #debugging
         
        $cursor = mysqli_query($connect,$sql);                  #execute the query                      

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

        mysqli_free_result($cursor);                            #free result buffer

        mysqli_close($connect);                                 #close connection
}

//===================================================================================
//display: display data on the html page
//===================================================================================
function display()
{
        global $data;
        global $ice_vanil,$ice_choco,$ice_straw,$ice_pecan,$ice_rocky,$ice_frnch,$ice_pstch;
        global $top_fudge,$top_sprkl,$top_nuts,$top_cream;
    
        print "<form action=$_SERVER[PHP_SELF] method=GET> \n";
        print "Flavor(s)......";
        print "<input type=checkbox name=ice_vanil value='vanilla'"; 
        if ($ice_vanil)  print 'checked'; 
        print ">Vanilla \n";
        print "<input type=checkbox name=ice_choco value='chocolate'";
        if ($ice_choco)  print 'checked'; 
        print ">Chocolate \n";
        print "<input type=checkbox name=ice_straw value='strawberry'";
        if ($ice_straw)  print 'checked'; 
        print ">Strawberry \n";
        print "<input type=checkbox name=ice_pecan value='butter-pecan'";
        if ($ice_pecan)  print 'checked'; 
        print ">Butter Pecan \n";
        print "<input type=checkbox name=ice_rocky value='rocky-road'";
        if ($ice_rocky)  print 'checked'; 
        print ">Rocky Road \n";
        print "<input type=checkbox name=ice_frnch value='french-vanilla'";
        if ($ice_frnch)  print 'checked'; 
        print ">French Vanilla \n";
        print "<input type=checkbox name=ice_pstch value='pistachio'";
        if ($ice_pstch)  print 'checked'; 
        print ">Pistachio \n";
        print '<br>Topping(s)...';
        print "<input type=checkbox name=top_fudge value='hotFudge'";
        if ($top_fudge)  print 'checked'; 
        print ">Hot Fudge \n";
        print "<input type=checkbox name=top_sprkl value='sprinkles'";
        if ($top_sprkl)  print 'checked'; 
        print ">Sprinkles \n";
        print "<input type=checkbox name=top_nuts  value='nuts'";
        if ($top_nuts)   print 'checked'; 
        print ">Nuts \n";
        print "<input type=checkbox name=top_cream value='whippedCream'";
        if ($top_cream)  print 'checked'; 
        print ">Whipped Cream \n";
        print "
            <br><br>
            <input type=submit value='   Filter   '> 
            </form>
            <table bgcolor=lightyellow border=1>
            <tr bgcolor=tan>
        "; 
                                   
        print "<th>Order#<th>Name<th>Address<th>Flavors<th>Toppings<th>Credit Card ";

        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);
            $topping2  = str_replace("," , "<br>", $topping);

            print "<tr>";
            print "<th>$order_id</th> <td>$first $last </td> <td>$address2  </td> 
                   <td>$flavor2 </td> <td>$topping2    </td> <td>$creditCard</td> \n";          
        }    
        print "</table> \n";
}
//===============================================================================

?>

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