2016-10-23 7 views
0

Ich versuche, eine Suchfunktion mit Keyword und Dropdown in meiner Website, habe ich in Google suchen, aber ich kann nicht scheinen, den gleichen Code zu finden. Ich habe versucht, Code zu kombinieren, den ich gefunden habe, und ich komme dazu. Ja, es funktioniert nur, wenn Sie jeweils eine Option in 3 Dropdown auswählen. Und mein Problem ist, dass es nicht funktioniert, wenn ich nur eine Option in nur einer Dropdown-Liste auswähle und mit der Suche fortfahre.Suchfunktion mit Schlüsselwort und mehreren Dropdown-Menüs

<!DOCTYPE html> 
<html> 
<head> 
    <title>Activity</title> 
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen"> 


    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <link href="css/bootstrap-responsive.css" rel="stylesheet"> 
</head> 
<body> 
    <?php 
     include('dbcon.php'); 
    ?> 

    <div class="container"> 
     <div class="row-fluid"> 
      <div class="span12"> 
         <br /> 
         <br /> 
       <div class="row-fluid"> 
        <div class="span6"> 
         <div class="span6"> 
          <form method="POST" action="search.php" class="navbar-search pull-left"> 
           <input type="text" name="search" class="search-query" placeholder="Search"> 
<select name="author" class="input"> 
    <option value="all">Select Author</option> 
    <option value="bbb">bbb</option> 
    <option value="rrr">rrr</option> 
    <option value="ooo">ooo</option>              
    <option value="hhh">hhh</option> 
    <option value="KingKoy Max">KingKoy Max</option> 
</select> 
<select name="category" class="input"> 
    <option value="all">Select Category</option> 
    <option value="ccc">ccc</option> 
    <option value="ttt">ttt</option> 
    <option value="iii">iii</option> 
    <option value="ggg">ggg</option> 
    <option value="Romance">Romance</option>  
</select> 
<select name="year_publication" class="input"> 
    <option value="all">Select Year</option> 
    <option value="2014">2014</option> 
    <option value="2015">2015</option> 
    <option value="2016">2016</option> 
</select> 
     <input type="submit" value="Search Now"> 
          </form> 
         </div> 
         <br /> 
         <br /> 
         <br /> 
         <table class="table table-bordered table-hover table-striped" style="width:800px;"> 
          <thead> 
           <tr> 
            <th>Title</th> 
            <th>Author</th> 
            <th>Category</th> 
            <th>Year Publication</th> 
            <th>Place Publication</th> 
           </tr> 
          </thead> 
          <tbody> 
           <?php 
            $query=mysql_query("select * from book")or die(mysql_error()); 
            while($row=mysql_fetch_array($query)){ 
            ?> 

            <tr> 
             <td><?php echo $row['title']; ?></td> 
             <td><?php echo $row['author']; ?></td> 
             <td><?php echo $row['category']; ?></td> 
             <td><?php echo $row['year_publication']; ?></td> 
             <td><?php echo $row['place_publication']; ?></td> 
            </tr> 
            <?php 
            } 
           ?> 
          </tbody> 
         </table> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 

</body> 

hier ist der PHP-Code:

<?php 
     include('dbcon.php'); 
    ?> 
           <?php 



            $search=$_POST['search']; 
            $author=$_POST['author']; 
            $category=$_POST['category']; 
            $year_publication=$_POST['year_publication']; 
            $query=mysql_query("select * from book where 
            title like '%$search%' AND author like '%$author%' AND category like '%$category%' AND year_publication like '%$year_publication%' ")or die(mysql_error()); 


            while($row=mysql_fetch_array($query)){ 
            ?> 

            <tr> 
             <td><?php echo $row['title']; ?></td> 
             <td><?php echo $row['author']; ?></td> 
             <td><?php echo $row['category']; ?></td> 
             <td><?php echo $row['year_publication']; ?></td> 
             <td><?php echo $row['place_publication']; ?></td> 

            </tr> 
            <?php 
            } 
            ?> 

Antwort

0

Sie überprüfen können, ob Dropdown ausgewählt ist oder nicht und bei leeren Satz leerer Variable. Versuchen Sie

ersetzen
$search=$_POST['search']; 
$author=$_POST['author']; 
$category=$_POST['category']; 
$year_publication=$_POST['year_publication']; 

von

if(empty($_POST['search'])) 
    $search = ''; 
else 
    $search=$_POST['search']; 
if(empty($_POST['author'])) 
    $author = ''; 
else 
    $author=$_POST['author']; 
if(empty($_POST['category'])) 
    $category = ''; 
else 
    $category=$_POST['category']; 
if(empty($_POST['year_publication'])) 
    $year_publication = ''; 
else 
    $year_publication=$_POST['year_publication']; 
+0

ich es versucht, zu ersetzen, aber immer noch nicht .. Dank funktioniert sowieso –

Verwandte Themen