簡體   English   中英

Php Sql 使用關鍵字搜索多個表並顯示搜索結果

[英]Php Sql Search multiple tables with keyword and display search results

我非常希望能夠在多個表中使用關鍵字進行搜索,然后顯示每個表(產品、服務、博客文章)的匹配結果。 我有可以在一個表中搜索的代碼,並且在查詢中對 UNION 進行了一些修改,但不確定從這里到 go 的位置。 我在 php 方面不是很有經驗。 如果有人可以向我展示它如何與我的代碼一起使用的示例將真正有幫助

<?php
include "config.php";
include 'Header.php';

// declares two arrays
$results = array();
$errors = array();

// get search textbox
$searchTerms = mysqli_real_escape_string($link, $_GET['search']);

// check if the length is not less than 2 chars
if (strlen($searchTerms) < 3) {
   $errors[] = "Your search term must be longer than 2 characters";
}  

// if there is no error, let perform the search
if (count($errors) < 1) {

  $query = "(SELECT * FROM tbl_products WHERE product_name LIKE '%{$searchTerms}%' OR product_description LIKE '%{$searchTerms}%')
   UNION
   (SELECT * FROM tbl_blog WHERE blog_title LIKE '%{$searchTerms}%' OR blog_content LIKE '%{$searchTerms}%') 
   UNION
   (SELECT * FROM service_categories WHERE serviceCat_name LIKE '%{$searchTerms}%')";          
   $result = mysqli_query($link, $query);

   //checks if the search yields any result
   if (mysqli_num_rows($result) < 1) {
     $errors[] = "Your search term yielded no results!<hr>";
   }
   else {
     //loop and store through all the results 
     while ($row=mysqli_fetch_array($result)) {
       extract($row);
       $results[] = '<a class="searchPageBox" href="viewProperty.php?propertyID='.$product_ID.'"><div style="width: 100%;"><div class="row"><div class="col-md-4">'.'<img style="width:100%; height=100%;" src="product-images/'.$product_image.'"  /></div><div class="col-md-8"><h3 class="searchPageTitle">'.$product_name.'</h3><div class="searchPageDesc">'.$product_description.'</div></div></div></div></a><hr>';
     } // end of while

   } // end of else

} // end of if

?>
<!DOCTYPE html>
<html>
<head>
<title>Search Site</title>
</head>

<body class="homepage">
<div id="page">
    <div id="two-column1" class="container">
        <div id="colB">
            <h3 class="searchPropertiesHeading">SEARCH PROPERTIES</h3>
        </div>
        <div id="colA">
            <!-- PHP here -->
            <h2 class="searchResults">Search Results for
            <span style="font-weight: bold"><?php echo $searchTerms ?></span>:</h2>
            <div>
                <?php echo count($results) ?>results</div>
            <hr><?php
          // display the search results here
          if (count($results) >0) {
              echo "".implode("", $results);
          }

          // display the error here
          if (count($errors) >0) {
              echo "".implode("<br>", $errors);
          }

         ?></div>
    </div>
</div>
<?php 
include 'Footer.php'; 
?>
<script src="js/jquery-1.10.1.min.js" type="text/javascript"></script>
<script src="js/bootstrap.js"></script>
<script src="js/script.js" type="text/javascript"></script>

</body>

</html>

對於任何想知道結果查詢如何結束的人:

$query = "(SELECT 'product' AS type, 'product' AS link, product_ID AS ID, product_name AS name, product_description AS description, product_image AS image FROM tbl_products WHERE product_name LIKE '%{$searchTerms}%' OR product_description LIKE '%{$searchTerms}%')
   UNION
   (SELECT 'blog' AS type, 'blog' AS link, blog_ID AS ID, blog_title AS name, blog_content AS description, blog_image AS image FROM tbl_blog WHERE blog_title LIKE '%{$searchTerms}%' OR blog_content LIKE '%{$searchTerms}%') 
   UNION
   (SELECT 'service' AS type, 'serviceCat' AS link, serviceCat_ID AS ID, serviceCat_name AS name, serviceCat_description AS description, serviceCat_image AS image FROM service_categories WHERE serviceCat_name LIKE '%{$searchTerms}%' OR serviceCat_description LIKE '%{$searchTerms}%') ORDER BY name ASC";     
   $result = mysqli_query($link, $query);


   if (mysqli_num_rows($result) < 1) {
     $errors[] = "Your search term yielded no results!<hr>";
   }
   else {
     //loop and store through all the results 
     while ($row=mysqli_fetch_array($result)) {
       extract($row);
       $results[] = '<a class="searchPageBox" href="view'.$type.'.php?'.$link.'ID='.$ID.'"><div style="width: 100%;"><div class="row"><div class="col-md-4">'.'<img style="width:100%; height=100%;" src="product-images/'.$image.'"  /></div><div class="col-md-8"><h3 class="searchPageTitle">'.$name.'</h3><div class="searchPageDesc">'.$description.'</div></div></div></div></a><hr>';
} 


     // end of while

   } // end of else

} // end of if

如果要在同一個“表”中顯示所有結果(例如 HTML 表,但使用哪種類型的可視化並不重要),則需要為每個表定義固定數量的列。

例如,如果您有產品(id、名稱、價格、描述、類別)和博客(id、標題、正文),則可以使用 3 列(id、類型和結果)將兩者合並。

  • ID:每種類型的唯一標識符。
  • 類型:“博客”、“產品”等,可能是您可以生成項目鏈接的內容。
  • 結果:顯示選擇該行的原因的文本。 您可以使用||連接多個字段 .

例子:

$query = "(SELECT id, 'product', product_name || ' ' || product_description FROM tbl_products WHERE product_name LIKE '%{$searchTerms}%' OR product_description LIKE '%{$searchTerms}%')
   UNION
   (SELECT id, 'blog', blog_title || ' ' || blog_content  FROM tbl_blog WHERE blog_title LIKE '%{$searchTerms}%' OR blog_content LIKE '%{$searchTerms}%') 
   UNION
   (SELECT id, 'services', serviceCat_name  FROM service_categories WHERE serviceCat_name LIKE '%{$searchTerms}%')";     

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM