Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add A Search

Status
Not open for further replies.
Mar 23, 2015
37
US
Can anyone help me add a search box into my code where the user can filter the database better? A have page that list people from a donors table. By default when the page opens it returns everyone in alphabetical order by last name then first name. Above the table on the page are alphabet buttons to filter the list. I'd like to put the search either before the A, after the Z or under the alphabet buttons entirely.

Button Code
Code:
<script>
var btns = "";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var letterArray = letters.split("");
for(var i = 0; i < 26; i++){
    var letter = letterArray.shift();
    btns += '<button class="mybtns" onclick="alphabetSearch(\''+letter+'\');">'+letter+'</button>';
}
function alphabetSearch(let){
window.location = "donorList.php?letter="+let;
}
</script>

php and mysql code
Code:
<?php 
  
   $letter = "";
     if(isset($_GET['letter']) && strlen($_GET['letter']) == 1){
      $letter = preg_replace('#[^a-z]#i', '', $_GET['letter']);
	 }

  	$sql = "SELECT * FROM donors WHERE lastname LIKE '$letter%' ORDER BY lastname, firstname";
    $result = mysql_query($sql) or die(mysql_error());
    ?>
  
   <h4>Donor List</h4>
   <p style="margin-left:25px;"> Clicking on an A thru Z button will return names that start with that letter.</p>
   <p style="margin-left:25px;"> Clicking on a table column header will sort the entire list by that heading.</p>
   <p style="margin-left:25px;"><script> document.write(btns);</script>

  <table...

Thank you!
 
That's not a search, its a filter.
But do you really want to bring back a name simply because it contains the letter selected somewhere in it?

If I click say "P", I might xpect Petersen, Pauslon, Petrov, but not really Samper.

Other than that, what issue are you having exactly?





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks for the reply. Yes it's a filter and if someone clicks on the P button it returns Paulson, Peterson and Petrov. I'd like to add where the user can type in pe and return Peterson and Petrov or petr and return Petrov. I'm pretty sure I don't need to change the php/mysql but I don't know how or where to add in the user input box with the buttons.
 
Oh, yes never mind. I saw your query wrong.

If you want an immediate response where the the list gets filtered as they type, then you'll need to use Ajax.

As to adding your buttons, you can place them really anywhere you want. But placing them right now as you have built them, isn't really a PHP question, its a JS question. since you are using Js to build the html for them.

In JS You can either use the innerHTML property of a div somewhere in your page to output your buttons, or the appendChild function to add each button as you built it.

For JS questions: forum216

Or simply use PHP to build the buttons instead, and echo them out, which I think may be a little simpler.

In JS I'd do something like this:
Code:
function buildButtons()
{
var btns = "";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var letterArray = letters.split("");
for(var i = 0; i < 26; i++){
    var letter = letterArray.shift();
    btns += '<button class="mybtns" onclick="alphabetSearch(\''+letter+'\');">'+letter+'</button>';
}
$div_buttonlocation = document.getElementById('buttonlocation');
$div_buttonlocation.innerHTML = btns;
}


...


<div id="buttonlocation">
</div>
<script type="text/javascript">buildButtons();</script>












----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks again for your help. The alphabet buttons are working (I've been using those for months), I just need to insert the user input filter. It doesn't matter to me if I it filters as they type or on button click. I'll go post this over at the javascrip forum.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top