Jump to content

PHP search function only searches one column


Adamhumbug

Recommended Posts

Hi all i have a few questions about a function i have to search my tables.

The first questions is, how do i open this up to search any of the columns rather than just the first one.

The second question is how do i open this function up to be used to search any of my tables.

The code i have at the minute that works to search the first column is:

function searchTable() {


  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById('customerTableSearch');
  filter = input.value.toUpperCase();
  table = document.getElementById('customerTable');
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    //column to search set below
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

I have tried quite a few things to open this up to more columns but i dont know how to get it to look at more than one column at a time.

The second question about making this a more generic search function uses the below code:

function searchTable(x, y) {

var searchbox = x;
var tablename = y;

  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById(searchbox);
  filter = input.value.toUpperCase();
  table = document.getElementById(tablename);
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

and then on the search box i have

onkeyup='searchTable(customerTableSearch, customerTable)'

this does not work and i am not sure where i am going wrong

Link to comment
Share on other sites

The offending line that is only allowing you to search the first column is this one:

 td = tr[i].getElementsByTagName("td")[0];

The `[0]` portion of that statement means to literally get the first column only. An easy fix is to add a nested loop, that does essentially the same as the for loop for the `tr` element, but do it for `td`, provided `td` is assigned:

 td = tr[i].getElementsByTagName("td");

Simply move the if block inside that inner loop, and it should work (some variable names inside that second for loop will need to be updated)

As for making this work with any table, I feel like the call to the function needs to pass a string. Without the quotes around customerTable, the code is going to assume that's a variable name, so maybe try updating to:

onkeyup='searchTable("customerTableSearch", "customerTable")'

These are all suggestions that require a minimal amount of refactoring of the current code, this doesn't mean I endorse that way it's written ;), there's lots of improvements that could be made for this to be much more maintainable in the future, but we'll leave that for another discussion

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.