V
V
Vladimir Vedernikov2020-05-11 15:19:08
PHP
Vladimir Vedernikov, 2020-05-11 15:19:08

How to implement search function via index.php?

Good day to all, tell me how to implement a search on the site. Now there is a script like this

Search script
<?php
require_once('engine/db.php');
?>
<html>
<head>
<style>
body{width:615px;font-family:arial;letter-spacing:1px;line-height:20px;}
.tbl-qa{width: 100%;font-size:0.9em;background-color: #f5f5f5;}
.tbl-qa th.table-header {padding: 5px;text-align: left;padding:10px;}
.tbl-qa .table-row td {padding:10px;background-color: #FDFDFD;vertical-align:top;}
.button_link {color:#FFF;text-decoration:none; background-color:#428a8e;padding:10px;}
#keyword{border: #CCC 1px solid; border-radius: 4px; padding: 7px;background:url("demo-search-icon.png") no-repeat center right 7px;}
.btn-page{margin-right:10px;padding:5px 10px; border: #CCC 1px solid; background:#FFF; border-radius:4px;cursor:pointer;}
.btn-page:hover{background:#F0F0F0;}
.btn-page.current{background:#F0F0F0;}
</style>
</head>
<body>
<?php	



    define("ROW_PER_PAGE",2);
    $pdo_conn = new PDO(  DB_DSN, DB_USERNAME, DB_PASSWORD );
  $search_keyword = '';
  if(!empty($_POST['search']['keyword'])) {
    $search_keyword = $_POST['search']['keyword'];
  }
  $sql = 'SELECT * FROM news WHERE title LIKE :keyword OR fullnews LIKE :keyword OR shortnews LIKE :keyword ORDER BY id DESC ';
  
  /* 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 = $pdo_conn->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 = $pdo_conn->prepare($query);
  $pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
  $pdo_statement->execute();
  $result = $pdo_statement->fetchAll();
  
?>
<form name='frmSearch' action='' method='post'>

<table class='tbl-qa'>
  <thead>
  <tr>
    <th class='table-header' width='20%'>Title</th>
    <th class='table-header' width='40%'>Description</th>
    <th class='table-header' width='20%'>Date</th>
  </tr>
  </thead>
  <tbody id='table-body'>
  <?php
  if(!empty($result)) { 
    foreach($result as $row) {
  ?>
    <tr class='table-row'>
    <td><?php echo $row['title']; ?></td>
    <td><?php echo $row['id']; ?></td>
    </tr>
    <?php
    }
  }
  ?>
  </tbody>
</table>
<?php echo $per_page_html; ?>
</form>
</body>
</html>


But this option does not suit me, and there is not enough knowledge to do it differently.
In general, I created in index.php
case 'search':
    search();
    break;

function search() {
  

   $results['pageTitle'] = PAGE_TITLE ;
   require( TEMPLATE_PATH . "/main/search.php" );
}


And I created the file search.php in the class folder connected it to the file with access to the database

The question is how can I make the server part of the script located in the search.php class file connected to index.php and processed in the client part of the template

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-05-11
@dimonchik2013

little code,
well, at least you removed it into the spoiler
, decompose - first learn how to return from the code in the page, then from the file, then from the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question