搜索栏能够用逗号搜索 - mySQL / php

时间:2015-02-10 20:00:27

标签: php mysql

我正在尝试编辑搜索栏,以便用户可以搜索以逗号分隔的值。它调用mySQL db。有人可以帮忙吗?我相信这一定是显而易见的,但我是新人。谢谢!

搜索页面:

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>

<?php include("../includes/header-home.php"); ?>
<div class="container">
  <div class="col=md-12">
    <p><strong>Search:</strong></p>
   <form name="form1" method="post" action="search_results.php">
    <p><input name="search" type="text" size="40" maxlength="50"/></p>
    <p>
     <input type="checkbox" name="type[]" value="1"> Medalist</p>

<p><strong>Medal:</strong>
<select name="medal">
<option disabled selected></option>
<option value="1">Medal 1</option>
<option value="2">Medal 2</option>
<option value="3">Medal 3</option>

</select>
</p>
<p><strong>State:</strong>
<select name="state">
<option disabled selected></option>
<option value="1">AL</option>
<option value="2">AK</option>
<option value="3">AZ</option>
<option value="4">AR</option>

</select>
</p>
<p><strong>Year:</strong>
<select name="year">
<option disabled selected></option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</optio>
</select>
</p>
<p><strong>Topic:</strong>
<select name="field">
<option disabled selected></option>
<option value="1">Field 1</option>
<option value="2">Field 2</option>
<option value="3">Field 3</option>

</select>
</p>

                  

搜索结果页:

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>

<?php
if (!isset ($_POST['search'])) {
    header("Location:admin.php");
}

 $search = $_POST['search'];
        $type    = (isset($_POST['type'])   ? "AND medalist = '1'" : "");
        $medal  = (isset($_POST['medal']) ? "AND medal = '{$_POST['medal']}'" : "");
        $state  = (isset($_POST['state']) ? "AND state = '{$_POST['state']}'" : "");
        $year  = (isset($_POST['year']) ? "AND year = '{$_POST['year']}'" : "");
        $field  = (isset($_POST['field']) ? "AND field = '{$_POST['field']}'" : "");


 $search_sqli = "
            SELECT * FROM profiles WHERE (
                    first_name      LIKE '%{$search}%' 
                OR  last_name       LIKE '%{$search}%' 
                OR  first_name2     LIKE '%{$search}%' 
                OR  last_name2      LIKE '%{$search}%' 
                OR  last_name2      LIKE '%{$search}%' 
                OR  city            LIKE '%{$search}%' 
                OR  agency          LIKE '%{$search}%' 
                OR  subcomponent    LIKE '%{$search}%' 
                OR  team_name       LIKE '%{$search}%' 
                OR  achievement     LIKE '%{$search}%' 
                OR  profile         LIKE '%{$search}%'
                OR  year            LIKE '%{$search}%'
            )
            {$type}
            {$medal}
            {$state}
            {$year}
            {$field}
        ";

$search_query=mysqli_query($connection, $search_sqli);
if (mysqli_num_rows($search_query) !=0)  {
$search_rs=mysqli_fetch_assoc($search_query);
}

?>

<?php include("../includes/header-home.php"); ?>
<div class="container">
  <div class="col=md-12">
     <p>Search:</p>
    <form name="form1" method="post" action="search_results.php">
    <input name="search" type="text" size="40" maxlength="50"/>
    <input type="submit" name="submit" value="Search" />

    </form>
   <br />
    <p><strong>Search Results:</strong></p>
  <?php if (mysqli_num_rows($search_query) !=0) {
     do  {
         ?>
    <p><ul>
    <li><a href="view_profile.php?profile=<?php echo urlencode($search_rs["id"]); ?>"><?php echo $search_rs['first_name']; ?> <?php echo $search_rs['last_name']; ?> <?php echo $search_rs['first_name2']; ?> <?php echo $search_rs['last_name2']; ?></a></li></ul></p>     

<?php } while ($search_rs=mysqli_fetch_assoc($search_query));

  } else {
      echo "No results found";
  }
  ?>
  <br />    
  <p> <a class="btn btn-default" href="search.php" role="button">Back to search</a></p>
    </div></div>

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?>

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

$searchArray = explode(',', $_POST['search']);

然后通过该数组运行每个项目的搜索查询,追加每个查询的结果,然后显示完整列表。

答案 1 :(得分:0)

你可以爆炸搜索然后使用这些值吗?

$search = $_POST['search'];
$myNewSearch = explode(",",$search);
$searchLen = count($myNewSearch);
for($i = 0; $i < $searchLen; $i++){
 Select * from profiles WHERE (
 first_name      LIKE '%{$myNewSearch[$i]}%'
 ... 
}

它有点长,但如果没有逗号,它会为你输入所有内容。