2016-09-09 4 views
1

Ich habe mehrere Felder in meinem Suchformular und meine Abfrage funktioniert für einzelne Felder. Was ich versuche zu erreichen istErstellen einer Suchfunktion

1- Abfrage sollte funktionieren, wenn Suche auf 1 Feld basiert 2- Abfrage sollte funktionieren, wenn Suche nach mehreren Feldern Eintrag

meine Form

<form class="sidebar-search jumbro-search container list-inline center-block" method="get" action="search.php"> 

<div class="form-group col-md-2"> 
    <input list="location" name="location" class="form-control" placeholder="Location"> 
    <datalist id="location"> 
     <?php 

      $loc="select * from locations"; 
      $results=mysqli_query($dbc,$loc); 

      while($row_loc=mysqli_fetch_array($results)){ 

       echo '<option value='.$row_loc['region'].'>'.$row_loc['region'].'</option>'; 
      } 
     ?> 
    </datalist> 
</div> 
<div class="form-group col-md-2"> 
    <select class="form-control" name="category"> 
     <option selected>Category</option> 
     <?php 

      $cat="select * from property_type order by type_name asc"; 
      $results=mysqli_query($dbc,$cat); 

      while($row_cat=mysqli_fetch_array($results)){ 

       echo '<option value='.$row_cat['type_name'].'>'.$row_cat['type_name'].'</option>'; 
      } 
     ?> 
    </select> 
</div> 

    <div class="form-group col-md-2"> 
    <select class="form-control" name="status"> 
     <option selected>Status</option> 
     <?php 

      $status="select * from property_status order by status_name asc"; 
      $results=mysqli_query($dbc,$status); 

      while($row_status=mysqli_fetch_array($results)){ 

       echo '<option value='.$row_status['status_name'].'>'.$row_status['status_name'].'</option>'; 
      } 
     ?> 
    </select> 
</div> 

<div class="form-group col-md-2"> 
    <input type="text" name="price-max" value="999999999999" class="form-control" placeholder="Max Price"> 
</div> 

<div class="form-group col-md-2"> 
    <button class="btn btn-primary form-control">Search</button> 

</div> 
basiert

und mein pHP-Skript sieht wie folgt aus

// getting user data from search form 
$location=$_GET['location']; 
$category=$_GET['category']; 
$status=$_GET['status']; 





//scripts 
if($location!="location" && $category!="category" && $status!="status"){ 
    $query="select * from properties where `property_type` like '%$category%' && `location` like '%$location%' && `status` like '%$status%' "; 
} 
$query="select * from properties where `property_type` like '%$category%' or `location` like '%$location%' or `status` like '%$status%'"; 

$result=mysqli_query($dbc,$query); 

if(mysqli_query($dbc,$query)) { 
    $num_rows=mysqli_num_rows($result); 
} else { 
    echo 'Query failed'; 
} 

$num_rows=mysqli_num_rows($result); 



if($num_rows!=0){ 
    echo '<h3 class="page-header text-center">'.$num_rows.' Match Found</h3>'; 
while ($row=mysqli_fetch_array($result)) { 


    <?php 
}//end while 
}else{ 
echo '<h3 class="page-header text-center">No Match Found, try adjusting your search criteria.</h3>'; 

include 'functions/latest-sc.php'; 
} 

Antwort

2

Nun, okay, ich habe mehrere Ideen, was Sie in Ihrem Code ändern sollten.

  1. Ich empfehle dringend, repräsentative Logik (HTML- und Echo-Variablen) von Funktionen wie Definieren von Variablen und Bearbeiten von Datenbankabfragen zu trennen. Es wird dir in Zukunft sehr helfen.

  2. Sie können den Code in prüft Standardoption in Ihrem wählt mit leeren Wert

    <option value="">Select none</option> 
    

verwenden Es wird vereinfacht:

Statt:

if($location!="location" && $category!="category" && $status!="status") 

verwenden können :

if($location && $category && $status) 
  1. Lesen Sie mehr über escaping

  2. Auf Ihrer Hauptfrage - Sie Abfrage durch Verkettung erstellen können. Ich gebe Ihnen Beispiel und Sie können es ersetzen mit ‚OR‘ oder ‚AND‘ für Ihre Bedürfnisse:

    $sql = 'SELECT * FROM properties WHERE '; 
    $scopes = []; 
    foreach([$location,$category,$status] as $column => $condition) { 
        if ($condition) { 
        $scopes[] = $column.' LIKE \'%.$condition.'%\''; 
        } 
    } 
    
    $scopes = implode(' AND ',$scopes); 
    $sql .= $scopes.';'; 
    
    // ...do what you need 
    
  3. Es gibt viel mehr Tipps für die Codierung ist aber vielleicht gerade Sie es wie tot einfaches Beispiel präsentieren, so Ich überspringe es.

0

sollte diese Arbeit:

$data = [ 
    'property_type' => 'category_value', //$_GET['location'] 
    'category' => 'location_value', //$_GET['category'] 
    'status' => 'status_value' //$_GET['status'] 
]; 

$select = ""; 
$params = 0; 

foreach($data as $k => $v){ 
    if($params > 0){ 
     $select .= " or "; 
    } 

    //add some better conditions 
    if(strlen($v) > 0){ 
     $select .= "`$k` LIKE %$v%"; 
     $params++; 
    } 
} 

$query = "select * from properties where " . $select; 

print_r($query); 
0

OK denke ich, was Sie fordern eine SELECT basierend auf mehreren Spalten in einer Tabelle ist. Unten ist ein Skript aus meiner Anwendung, das Datensätze aus einer Tabelle auswählt, die nach einem Heimteam und einem Auswärtsteam sucht: -

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "localdb"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT hometeam FROM stats WHERE hometeam = 'Man City' AND awayteam = 'Sunderland'"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 

     echo $row['hometeam']; 
    } 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
?>