简体   繁体   中英

How to replace a different div tag with the searched text?

I got the search box in the navigation bar at the top.

<li class="nav-item">
    <form action="model/action-search.php" class="search-form" method="POST">
        <div class="form-group has-feedback">
            <label for="search" class="sr-only fa fa-search">Search</label>
            <input type="text" class="form-control" name="search" placeholder="search">
            <input type="submit" name="submit">
        </div>
    </form>
</li>

The action file.

if(isset($_POST['submit']))
{
    $search = stripcslashes($_POST['search']);
    $search = mysqli_real_escape_string($con,$search);

    $query = $con->query("SELECT * FROM agencyemployee MATCH (agcName , nmName , agcemail) AGAINST ('".$search."')");
    header("Location:../sampsearch.php");
}

I already have a row of nursemaid displayed here. What I want is that when I search something I want to replace the displayed row with the searched text here.

<div class="row">
<?php
     $query = $con->query("SELECT * FROM agencyemployee");
     while($row = mysqli_fetch_array($query))
     {
?>
      <div class="col-md-4 col-sm-6 portfolio-item">
        <div class="portfolio-caption">
          <h4><?php echo $row['nmName']; ?></h4>
        </div>
      </div>
<?php}?>

Firstly I would suggest using GET for you form method as that way your search string would be reflected in the URL and you can refer to it.

If I understand well and what you want to do is highlight the searched text in each row then I would do something like this:

<div class="row">
<?php
  $query = $con->query("SELECT * FROM agencyemployee");
  while($row = mysqli_fetch_array($query)) : ?>
  <div class="col-md-4 col-sm-6 portfolio-item">
    <div class="portfolio-caption">
      <h4>
        <?php
          $name = $row['nmName'];
          if (isset($_GET['search'])) {
            $name =  str_replace($_GET['search'], "<span class='search-highligh'>" . $_GET['search'] . "</span>", $name);
          }
        ?>
        <?php echo $name; ?>
      </h4>
    </div>
  </div>
  <?php endwhile;?>

If i am able to understand your problem then you should follow below instruction: You should just update the $query variable as your requirement like you want to fetch searched information or all information. In that case just slightly change your code:

<div class="row">
<?php

  $query = "SELECT * FROM agencyemployee";
  if(isset($_POST['submit']))
  {
    $search = stripcslashes($_POST['search']);
    $search = mysqli_real_escape_string($con,$search);

    $query = "SELECT * FROM agencyemployee MATCH (agcName , nmName , 
    agcemail) AGAINST ('".$search."')";

 }
  $result = $con->query($query);
  while($row = mysqli_fetch_array($result))
  {
?>
      <div class="col-md-4 col-sm-6 portfolio-item">
        <div class="portfolio-caption">
          <h4><?php echo $row['nmName']; ?></h4>
        </div>
      </div>
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM