<head>
<title>Image Search</title>
</head>
<body bgcolor=lightyellow>
<h1><center>Image Search</center></h1>
<img id='e1' style='position:absolute; top:225; left:630;' >


<?php

    error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);              //turn off PHP warnings & notices only

//  require "dbIO.php";                                         //include the dbIO.php file 
    require "/home/sultans/web/php/demo/4http/dbIO.php";        //make it absolute so it works with PHP*Tester

    $msg;

    if ($_POST)                                                 //if data was collected in $_POST array
    {
        retrieve();
        searchDB();
    }
    display();

//=============================================================================
    function retrieve()
    {
        global $type, $search;

        $type    = $_POST['type'];                              //get HTML form entry fields
        $search  = $_POST['search'];

        if (strtolower($type)!='book' &&  strtolower($type)!='software') 
            $type  = 'book';

        if ($search == '' || $search == ' ') 
            $search = '%';
    }
    
//=============================================================================
    function searchDB()
    {
        global $type, $search, $msg;

        $sql = "SELECT * FROM image
                 WHERE  type = '$type'
                   AND (name    LIKE '%{$search}%'
                    OR  owner   LIKE '%{$search}%'
                    OR  keyword LIKE '%{$search}%')"; 
        
        $data = dbIO('localhost','demo2','demo2','demo2', $sql);        //call the dbIO with the SQL    
        
        if (is_string($data))                                           //if string -> must be an error
            $msg = $data;
            
        if (is_numeric($data))                                          //if num -> must have been a update statement 
            $msg = $data . ' rows affected';                            //not used in this script

        if (is_array($data))                                            //if array -> must be a SELECT statement 
        {
            $msg .= "<table border=1 width=600> \n";            
            $msg .= "<tr bgcolor=tan><th>Name<th>Author/Owner<th>Keywords<th>Image</tr> \n";

            for ($i=0; $i < count($data); $i++)                                 //loop thru all the rows
            {
                $row = $data[$i];                                               //each row

                $msg .= "<tr valign=top> \n";

                foreach ($row as $col_name => $col_value)                       //loop thru each row
                {                                   
                    if ($col_name=='name' || $col_name=='owner' || $col_name=='keyword')
                    {                                       
                        $col = preg_replace('/,/', '<br>', $col_value);         //replace , with <br>
                        $msg .= "<td> $col </td> \n";
                    }
                    if ($col_name == 'filename')
                    {                                       
                        $url  = "/~sultans/javascript/demo/img/$col_value";

                        $msg .= "<td><img src=$url height=100 width=70 
                                      onMouseOver=document.getElementById('e1').src='$url'></td>";                 
                    }
                }
                $msg .= "</tr> \n";
            }
            $msg .= "</table> \n";
        }            
    }

//=============================================================================
    function display()
    {
        global $type, $search, $msg;
        global $search_error;

        if ($type == 'book')     $book_sel = 'selected';
        if ($type == 'software') $soft_sel = 'selected';
        
        print "<form method=POST action=$_SERVER[PHP_SELF]> \n";        //itself
        print "<table bgcolor=tan border=3 outset> \n";
        print "<tr>";
        print "<td><b>Image category:";
        print "<td><select name=type> \n";
        print "    <option value='book'     $book_sel>Books   </option> \n";
        print "    <option value='software' $soft_sel>Software</option> \n";
        print "    </select> \n";
        print "<tr>";
        print "<td><b>Search:  <font color=red> *$search_error </font>";
        print "<td><input type=text name=search value='$search' size='30'> (any keyword or %) \n";
        print "<tr>";
        print "<td width=75>";
        print "<input type=submit value='      Search      '>";
        print "<td>";
        print "<input type=reset  value='       Reset       '>";
        print "</tr> \n";
        print "</table> \n";
        print "</form>";
        print "<hr> \n";
        print "$msg";
    }
?>

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