PHP-在数据库上搜索多个单词

时间:2018-06-28 16:42:18

标签: php html database search

我具有搜索功能,但仅当我输入2个单词时用一个单词搜索时,才显示结果。可以使用多个单词进行搜索吗?我希望能够像“纽约房屋”一样进行搜索,并使用该搜索词向我显示来自我的数据库的数据。 我的HTML:

<form name='frmSearch' action='' method='post'>
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
<table class='tbl-qa'>
  <thead>
	<tr>
	  <th class='table-header' width='10%'>ID Anunt</th>
	  <th class='table-header' width='10%'>Titlu</th>
	  <th class='table-header' width='10%'>Camere</th>
	  <th class='table-header' width='10%'>Etaj</th>
	  <th class='table-header' width='10%'>Compartimentare</th>

	</tr>
  </thead>
  <tbody id='table-body'>
	<?php
	if(!empty($result)) { 
		foreach($result as $row) {
	?>
	  <tr class='table-row'>
		<td><?php echo $row['id_anunt']; ?></td>
		<td><?php echo $row['titlu']; ?></td>
		<td><?php echo $row['camere']; ?></td>
		<td><?php echo $row['etaj']; ?></td>
		<td><?php echo $row['compartimentare']; ?></td>

	  </tr>
    <?php
		}
	}
	?>
  </tbody>
</table>
<?php echo $per_page_html; ?>
</form>

我的搜索功能:

<?php   
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
    $search_keyword = $_POST['search']['keyword'];
}
$sql = 'SELECT * FROM apartament where titlu LIKE :keyword OR camere LIKE :keyword OR etaj LIKE :keyword OR compartimentare LIKE :keyword ORDER BY titlu ASC';




/* Pagination Code starts */
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
    $page = $_POST["page"];
    $start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $connect->prepare($sql);
$pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pagination_statement->execute();

$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
    $per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
    $page_count=ceil($row_count/ROW_PER_PAGE);
    if($page_count>1) {
        for($i=1;$i<=$page_count;$i++){
            if($i==$page){
                $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
            } else {
                $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
            }
        }
    }
    $per_page_html .= "</div>";
}

$query = $sql.$limit;
$pdo_statement = $connect->prepare($query);
$pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();

0 个答案:

没有答案