简体   繁体   English

Php Sql 使用关键字搜索多个表并显示搜索结果

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

Pretty much I want to be able to search with a keyword in multiple tables then display the matching results from each of the tables (products, services, blog posts).我非常希望能够在多个表中使用关键字进行搜索,然后显示每个表(产品、服务、博客文章)的匹配结果。 I have code that works to search in one table and have fiddled a little bit with UNION in the query but not sure where to go from here.我有可以在一个表中搜索的代码,并且在查询中对 UNION 进行了一些修改,但不确定从这里到 go 的位置。 I'm not very experienced in php.我在 php 方面不是很有经验。 If anyone could show me example of how its used with my code would really help如果有人可以向我展示它如何与我的代码一起使用的示例将真正有帮助

<?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>

For anyone who wants to know how the resulting query ended up:对于任何想知道结果查询如何结束的人:

$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

If you want to display all the results in the same "table" (for example HTML table, but doesn't matter what type of visualisation is used), you need to define a fixed number of column for each table.如果要在同一个“表”中显示所有结果(例如 HTML 表,但使用哪种类型的可视化并不重要),则需要为每个表定义固定数量的列。

For example, if you have product (id, name, price, description, category) and blog (id, title, body), you can have a union of both using 3 columns (id, type and result).例如,如果您有产品(id、名称、价格、描述、类别)和博客(id、标题、正文),则可以使用 3 列(id、类型和结果)将两者合并。

  • ID: Unique identifier for each type. ID:每种类型的唯一标识符。
  • TYPE: "blog", "product", etc, probably something that you can generate a link to the item.类型:“博客”、“产品”等,可能是您可以生成项目链接的内容。
  • RESULT: Text where is show why this row was selected.结果:显示选择该行的原因的文本。 You can concatenate multiple fields with ||您可以使用||连接多个字段. .

Example:例子:

$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