Jump to content

I have code to search a database of members, I can search by lastname :


vet911

Recommended Posts

I have code to search a database of members, I can search by lastname but having a little problem. I want to be able to search by lastname starting with the first letter. (ie:  a or b or c or d and so on). As it is right now I can only search by first letter of last name if I add % to the search (ie: a% b% c%) will give me all members with the last names starting with the approiate letter designation. I'm not sure how to handle this, any help would be appreciated. Thanks.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

</head>
<body>
<center><table cellspacing="10" cellpadding="10" width="750" border="0">
<h4>Search</h4>

<form name="search" method="post" action="<?php $PHP_SELF?>">
Seach for: <input type="text" name="find" /> in <Select NAME="field"> 

<Option VALUE="lname">Last Name</option> 
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?php 

// check to see if anything is posted
if (isset($_POST['find'])) {$find = $_POST['find'];}
if (isset($_POST['searching'])) {$searching = $_POST['searching'];}
if (isset($_POST['field'])) {$field = $_POST['field'];}

//This is only displayed if they have submitted the form 
if (isset($searching) && $searching=="yes") {
echo "<h4>Results</h4><p>";
// If they did not enter a search term we give them an error
if (empty($find)) {
echo "<p>You forgot to enter a search term"; exit;
}

// Otherwise we connect to our Database 
mysql_connect("localhost", "root", "1910") or die(mysql_error());
mysql_select_db("cmc_member") or die(mysql_error());

// We preform a bit of filtering 
$find = strtoupper($find);
$find = strip_tags($find); $find = trim ($find); 

// Now we search for our search term, in the field the user specified 
$results = mysql_query("SELECT * FROM members WHERE upper($field) LIKE'$find'");

// And we display the results 
if($results && mysql_num_rows($results) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($results))
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product ALIGN='CENTER'

       if($fname != "" && $fname != null)
          echo "<td ALIGN='CENTER'><FONT COLOR='red'><b>$fname $lname</b></FONT><br> $address <br> $city $state $zip <br>$phone<br><a href=\"mailto: $email\">$email</a><br></td>";

       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns)
       {
           echo "</tr>";
           $i=0;
           }  // end if
   } // end while

} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";

}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches = mysql_num_rows($results);

if ($anymatches == 0) {
echo "Sorry, but we can not find an entry to match your query<br><br>"; }

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find;
}
?> 
</tr>
</table></center> 
</body>
</html>

Link to comment
Share on other sites

Thanks,  that worked fine. I tried using the wild card in the query before I posted but when I put it in ( $results = mysql_query("SELECT * FROM members WHERE upper($field) LIKE'%$find%'"); I had it in both places, front and back, didn't think to place it in the back only. When it was in both it found all cases of the search letter in all places which was what I didn't want.

 

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.