0

I have developed a code to fetch database results.

Now I need to filter the database results using java script live filtering method. But the filtering part was not successful.

Can anybody help me to figure it out?

Code:

    <div class="col-lg-8" >
  <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
  <center><h3>Wasthu</h3></center>
  <?php
    $sql = "SELECT * FROM services  where service='Architecture'";
    $result = $con->query($sql);
    if ($result->num_rows > 0) 
    {
    while ($row = $result->fetch_assoc()) 
      {
    ?>
  <div class="col-lg-3" style="border: outset;">
    <div class="header">
        <?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
    </div>
    <ul class="body" id="myUL">
    <li>
      <a href="www.google.com" target="_blank">
        <?php echo $row ['name']; ?>
      </a>
      <i class="fa fa-envelope-square"><?php echo $row ['email']; ?></i>
      <br>
      <i class="fa fa-address-book"><?php echo $row ['address']; ?></i>
      <br>
      <?php echo $row ['years']; ?>
      <br>
      <i class="fa fa-info"><?php echo $row ['details']; ?></i>
      <div style="background-color: #000000">
        <font color="#FFFFFF"><?php echo $row ['district']; ?></font>
        <font color="#FFFFFF"><?php echo $row ['city']; ?></font>
      </div>
    </li>
</ul>
</div>
  <?php
      }
    } 
    else 
    {
      echo "0 results";
    }
    $con->close();
  ?>

</div>

<script>
  function myFunction() 
  {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) 
    {
        a = li[i].getElementsByTagName("a")[0];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) 
        {
          li[i].style.display = "";
        } 
        else 
        {
          li[i].style.display = "none";
        }
    }
  }
</script>

The image given below shows database fetched results before filter enter image description here

This image shows how needed result disappear when search. Here I want to search sadeepa.when I search It, it disappears enter image description here

1 Answer 1

2

The #myUL element is not a UL but DIV - and its children are not LI elements but H4. And you have no A tags.

Either change the tag names in HTML - or modify your JavaScript function to look for the proper tags.

<div class="col-lg-8" >
  <input type="text" 
    id="myInput" 
    onkeyup="myFunction()" 
    placeholder="Search for names.." 
    title="Type in a name">
  <center><h3>Wasthu</h3></center>
  <?php
    $sql = "SELECT * FROM services  where service='Waasthu'";
    $result = $con->query($sql);
    if ($result->num_rows > 0) 
    {
  ?>
  <div class="col-lg-3" style="border: outset;">
  <div class="header">
    <?php echo '<img src="' . $row['image_path4']. '" width="100" height="100">'; ?>
  </div>
  <ul class="body" id="myUL">
  <?php
      while ($row = $result->fetch_assoc()) 
      {
  ?>
    <li>
      <a href="www.google.com" target="_blank">
        <?php echo $row ['name']; ?>
      </a>
      <i class="fa fa-envelope-square"><?php echo $row ['email']; ?></i>
      <br>
      <i class="fa fa-address-book"><?php echo $row ['address']; ?></i>
      <br>
      <?php echo $row ['years']; ?>
      <br>
      <i class="fa fa-info"><?php echo $row ['details']; ?></i>
      <div style="background-color: #000000">
        <font color="#FFFFFF"><?php echo $row ['district']; ?></font>
        <font color="#FFFFFF"><?php echo $row ['city']; ?></font>
      </div>
    </li>
  <?php
      }
    } 
    else 
    {
      echo "0 results";
    }
    $con->close();
  ?>
</div>

<script>
  function myFunction() 
  {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) 
    {
        a = li[i].getElementsByTagName("a")[0];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) 
        {
          li[i].style.display = "";
        } 
        else 
        {
          li[i].style.display = "none";
        }
    }
  }
</script>

UPDATE

The example works just fine - try it.

function myFunction() 
  {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) 
    {
        a = li[i].getElementsByTagName("a")[0];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) 
        {
          li[i].style.display = "";
        } 
        else 
        {
          li[i].style.display = "none";
        }
    }
  }
<div class="col-lg-8" >
  <input type="text" 
    id="myInput" 
    onkeyup="myFunction()" 
    placeholder="Search for names.." 
    title="Type in a name">
  <center><h3>Wasthu</h3></center>
  <div class="col-lg-3" style="border: outset;">
  <div class="header">
    <img src="http://randomavatar.com/avatar/50270" width="100" height="100">
  </div>
  <ul class="body" id="myUL">
    <li>
      <a href="www.google.com" target="_blank">
        Peter Goodyear
      </a>
      <i class="fa fa-envelope-square">[email protected]</i>
      <br>
      <i class="fa fa-address-book">London city</i>
      <br>
      35 yrs
      <br>
      <i class="fa fa-info">Some details</i>
      <div style="background-color: #000000">
        <font color="#FFFFFF">District 5</font>
        <font color="#FFFFFF">London</font>
      </div>
    </li>
    <li>
      <a href="www.google.com" target="_blank">
        Sara Bullock
      </a>
      <i class="fa fa-envelope-square">[email protected]</i>
      <br>
      <i class="fa fa-address-book">Alabama city</i>
      <br>
      35 yrs
      <br>
      <i class="fa fa-info">Some details</i>
      <div style="background-color: #000000">
        <font color="#FFFFFF">District 5</font>
        <font color="#FFFFFF">Alabama</font>
      </div>
    </li>
    <li>
      <a href="www.google.com" target="_blank">
        Mark Meyerson
      </a>
      <i class="fa fa-envelope-square">[email protected]</i>
      <br>
      <i class="fa fa-address-book">New York city</i>
      <br>
      35 yrs
      <br>
      <i class="fa fa-info">Some details</i>
      <div style="background-color: #000000">
        <font color="#FFFFFF">District 5</font>
        <font color="#FFFFFF">New York</font>
      </div>
    </li>
    <li>
      <a href="www.google.com" target="_blank">
        Robert Stone
      </a>
      <i class="fa fa-envelope-square">[email protected]</i>
      <br>
      <i class="fa fa-address-book">Dallas city</i>
      <br>
      35 yrs
      <br>
      <i class="fa fa-info">Some details</i>
      <div style="background-color: #000000">
        <font color="#FFFFFF">District 5</font>
        <font color="#FFFFFF">Dallas</font>
      </div>
    </li>
  
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

I updated my code after formatting based on your answer. But when I typed the name which I want to search it disappears and others are remaining. It is vice versa of my need. I need to remain what I search and remove other's from viewing.How can I do that? I have update my question
well, if it is the opposite of what you are looking for - then simply swap the places of display = "none" and display = ""
I swapped the values, but still my issue was not solved. when I typed John his name disappear and other person name are remaining
Do you have a JSfiddle or CodePen ?
No. I don't have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.