<html>
<head>
<title>Retrieve data from file</title>
</head>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>
<h2>Retrieve data from file and sort it by lname, fname</h2>

<table bgcolor=#dddddd border=5 outset width=700>
<tr bgcolor=tan>
    <th>Name</th><th>Address</th><th>Flavors</th><th>Toppings</th><th>Credit Card</th>
</tr>

<?php

    $file = '/home/sultans/web/data/cust_order.file';   #server file
//  $file = '../data/cust_order.file';                  #local PC file

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


//===============================================================================
    function display()
    {
        global $data;   
        $data_sorted = Array();                 //this will be a 2dim array

        foreach($data as $rec)
        {
            $fields = explode('||',$rec);       //turn $rec into an array

            $data_sorted[ ] = $fields;          //add array to next slot of $data_sorted
        }

        usort($data_sorted, 'by_lname_fname');  //sort array by lastname then firstname
            
        foreach($data_sorted as $row)
        {
            $first      = $row[0];
            $last       = $row[1];
            $address    = $row[2];
            $flavor     = $row[3];
            $topping    = $row[4];
            $creditCard = $row[5];
                
            $address2  = str_replace("__", "<br>", $address);   #replace __ with <br>
            $flavor2   = str_replace("," , "<br>", $flavor);
            $topping2  = str_replace("," , "<br>", $topping);

            print "<tr valign=top>";
            print "<td>$first $last </td> <td>$address2</td>
                   <td>$flavor2</td> <td>$topping2</td> <td>$creditCard</td>";
            print "</font></tr>";
        }    
    }

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

        $filename = $file;
 
        $input = fopen ($filename, 'r')  
                    or die ("Cannot open $input for read"); 

        $data = file($filename)                         #read entire file into array
                    or die ("Cannot read from file"); 
       
        fclose($input);
    }

//=======================================================================================
//Compare function: will be called by the PHP usort function
//   It receives 2 elements.  Since this is a 2 dimensional array, it receives 2 arrays.
//   The job is to tell usort whether to switch the elements by returning 1,
//   or to keep then as is by returning -1 or 0
//=======================================================================================
function by_lname_fname($a, $b)                 //receive 2 elements (arrays in this case)
{
    $fname1 = strtolower($a[0]);                //convert to lower case
    $fname2 = strtolower($b[0]);                //so the compare is not case sensitive
    $lname1 = strtolower($a[1]);                
    $lname2 = strtolower($b[1]);                

    if ($lname1  > $lname2) return  1;          //if lname1 > lname2 -> switch
    if ($lname1  > $lname2) return -1;          //if lname1 < lname2 -> no switch
    if ($lname1  > $lname2)                     //if lname1 = lname2
    {
        if ($fname1  > $fname2) return  1;      //if fname1 > fname2 -> switch          
        if ($fname1  > $fname2) return -1;      //if fname1 > fname2 -> no switch
        if ($fname1  > $fname2) return  0;      //no switch
    }
}

?>
</table>

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