./search.php

<?php

$dir_path = './';

include($dir_path.'includes/common/common.php');

$layout->body = str_replace('%CONTENT', gettpl('main/box').'%REST', $layout->body);
$layout->body = str_replace('%CONTENT', gettpl('search/search'), $layout->body);

//construct a search query bassed on posted values
if(count($_GET)<1)
{
    
//not a single value, no search, show a blank search page and the newest 20 books just to fill the space
    $query = "SELECT BtName, cast(BtId AS UNSIGNED) as BtId FROM booktitle ORDER BY BtId DESC LIMIT 0, 20;";
    
$results = mysql_query($query) or report(mysql_error($connection), $query);

    
while($row = mysql_fetch_object($results))
    
{
        
$out .= gettpl('main/float');

        
if(file_exists('images/books/'.$row->BtId.'.jpg'))
        
{
            
$image = 'books/'.$row->BtId.'.jpg';
        
}
        
else
        
{
            
$image = 'books/none.jpg';
        
}

        
$out = str_replace('%CONTENT', '<a href="book.php?id='.$row->BtId.'"><img src="images/thumbnail.php?maxh=150&maxw=150&src='.$image.'"><br>'.ucwords($row->BtName).'</a>', $out);
    
}
    
$layout->body = str_replace('%REST', $out, $layout->body);
}
else
{
    
//have got some keywords but what kind?
    if(isset($_GET['q']))
    
{
        
//got a q value - so search is from the top right box, q could be author, name, publisher so search for all using OR
        $query = "SELECT booktitle.BtName, author.AuthorName, booktitle.BtId, publisher.PubName FROM booktitle LEFT JOIN authorship ON booktitle.BtId = authorship.BtId LEFT JOIN author ON authorship.AuthorId = author.AuthorId LEFT JOIN publisher ON publisher.PubId = booktitle.PubId WHERE booktitle.BtName LIKE '%%%s%%' OR author.AuthorName LIKE '%%%s%%' OR publisher.PubName LIKE '%%%s%%';";
        
$query = sprintf($query, $_GET['q'], $_GET['q'], $_GET['q']);
    
}
    
else
    
{
        
//got other values, test for existance and actual keywords, if present add to the query using AND op
        $query = "SELECT booktitle.BtName, author.AuthorName, booktitle.BtId, publisher.PubName FROM booktitle LEFT JOIN authorship ON booktitle.BtId = authorship.BtId LEFT JOIN author ON authorship.AuthorId = author.AuthorId LEFT JOIN publisher ON publisher.PubId = booktitle.PubId WHERE";

        
if(isset($_GET['title']) && $_GET['title']!="")
        
{
            
$d = TRUE;
            
$query .= " booktitle.BtName LIKE '%".$_GET['title']."%' AND";
        
}
        
if(isset($_GET['author']) && $_GET['author']!="")
        
{
            
$d = TRUE;
            
$query .= " author.AuthorName LIKE '%".$_GET['author']."%' AND";
        
}
        
if(isset($_GET['publisher']) && $_GET['publisher']!="")
        
{
            
$d = TRUE;
            
$query .= " publisher.PubName LIKE '%".$_GET['publisher']."%' AND";
        
}
        
//Remove redundent AND
        $query = str_replace('ANDAND', '', $query.'AND');
        
if($d!=TRUE)
        
{
            
//just in case some idiot trys to search for something with no keywords
            $query = str_replace('WHEREAND', '', $query);
        
}
        
$query .= ';';
    
}

    
$results = mysql_query($query) or report(mysql_error($connection), $query);
    
//now the results
    if(mysql_num_rows($results)>0)
    
{
        
//got at least 1 so lets show them
        $out = gettpl('search/results');
        
$out = explode('%ROW', $out);

        
while($row = mysql_fetch_object($results))
        
{
            
//only running one query here and multiple authors for same book produce
            // two rows. I want this though to show both names so, cunstruct an array
            // of each book, then author then add the row object as the value
            $books[$row->BtName][$row->AuthorName] = $row;
        
}
        
        
//loop through each book found, writting the contents
        foreach($books as $name => $data)
        
{
            
$rows .= $out[1];
            
$rows = str_replace('%TITLE', $name, $rows);
            
unset($auts);
            
$ats = explode('%AUTHORS', $rows);
            
            
//now loop through each author, writting content to the page
            foreach($data as $author => $row)
            
{
                
$auts .= $ats[1].', ';
                
$auts = str_replace('%AUTHOR', $author, $auts);
                
$rows = str_replace('%PUBLISHER', $row->PubName, $rows);
                
$rows = str_replace('%ID', $row->BtId, $rows);
            
}
            
$auts = str_replace(', ,', '', $auts.',');
            
$rows = explode('%AUTHORS', $rows);
            
$rows[1] = $auts;
            
$rows = implode('', $rows);
        
}
        
$layout->body = str_replace('%REST', $out[0].$rows.$out[2], $layout->body);
    
}
    
else
    
{
        
//got no results
        $layout->body = str_replace('%REST', gettpl('search/nothing'), $layout->body);
    
}
}



$layout->output();