0

I have a search function but is showing me the result only if i search with one word if i put 2 words is not showing me nothing. Is possible to make a search with more than one word? I want to be able to search like "house new york" and to show me data from my DB with that search words. My 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>

My search function:

<?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();
Snuk
  • 35
  • 1
  • 6
  • you could split the words and use OR in your query to chek for each words – Liora Haydont Jun 28 '18 at 16:46
  • I use OR but is not working. Can you explain how to split the words please? I am new to php and the search function i adapt for my need but is from a tutorial from youtube. Thanks. – Snuk Jun 28 '18 at 16:50
  • you should check this answer https://stackoverflow.com/a/9416147/8759952 for the ORs. To split the words you can use the php function explode http://php.net/manual/en/function.explode.php – Liora Haydont Jun 28 '18 at 17:00
  • Still i dont uderstand how to do that implode. Thanks anyway. – Snuk Jun 28 '18 at 18:34

0 Answers0