×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Add A Search

Add A Search

Add A Search

(OP)
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!

RE: Add A Search

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

RE: Add A Search

(OP)
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.

RE: Add A Search

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: Javascript

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

RE: Add A Search

(OP)
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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close