How to search by using alphabetic character in php

i have try to do so many time this code for alphabetic search but i m not getting how to search this is code

<?php
for ($i = 65 ; $i < 91 ; $i++)
{
printf ('<a href = “%s?result.php?$i=%s”>%s</a> ', $PHP_SELF, chr($i), chr($i));

	}?&gt;

by using this code it show alphabetic A to Z cappital Letter
but i want to seach in php if i select A then show A related data from data base
how is that possible please guide me or assist me

Thank’s in Advace
Rohit Tripathi

Please post a more detailed question.

I would do something like this…


$letters = array();
for ($i = 65; $i < 91; $i++)
{
	printf ('<a href = "%s?search=%s">%s</a> ', $_SERVER['SCRIPT_NAME'], chr($i), chr($i));
	$letters[] = chr($i); // Store all allowed letters in array
}

if (isset($_GET['search']) && in_array($_GET['search'], $letters))
{
	$search = $_GET['search'];
	$sql = "SELECT * FROM my_table WHERE some_field LIKE $search%"
	// Run ur query and process results...
}

$letters = array();
for ($i = 65; $i < 91; $i++)
{
printf ('<a href = “%s?search=%s”>%s</a> ', $_SERVER[‘SCRIPT_NAME’], chr($i), chr($i));
$letters = chr($i); // Store all allowed letters in array
}

if (isset($_GET[‘search’]) && in_array($_GET[‘search’], $letters))
{
$search = $_GET[‘search’];
$sql = “SELECT * FROM my_table WHERE some_field LIKE $search%”
// Run ur query and process results…
}

okk thank’s for replay but i m not geting what put on and here

$_SERVER[‘SCRIPT_NAME’],

if i use here my path name is this right like sh_result.php
or something else

please help me

Maybe this will help. :slight_smile:


<?php
function searchByChar($char){
  $res = mysql_query(sprintf(
    "SELECT foo, bar FROM table WHERE foo LIKE '%s%%';",
    mysql_real_escape_string($char)
  ));
  $results = array();
  while($result = mysql_fetch_assoc($res)){
    array_push($results, $result);
  }
  return $results;
}

$chars = range('A', 'Z');

$results = array();

if(isset($_GET['char']) && in_array($_GET['char'], $chars)){
  $results = searchByChar($_GET['char']);
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <div id="search">
      <?php foreach($chars as $char): ?>
        <a href="search.php?char=<?php echo $char; ?>">
          <?php echo $char; ?>
        </a>
      <?php endforeach; ?>
    </div>
    <div id="results">
      <?php if(0 === count($results)): ?>
        Sorry, no results found.
      <?php else: ?>
        <?php foreach($results as $result): ?>
          <p> We found <?php echo $result['bar']; ?></p>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </body>
</html>

Needs more ORDER BY, Anthony! :wink:

Dammit!

Actually, no it doesn’t. Does it?

Rudy will come along and say that you should ALWAYS order by. And in practical terms; presumably if you’re searching by alpha, you want it in alphabetical order (ORDER BY foo). Without the order by, you should get the results in order of the index (or if there is no index, unpredictable order!)