简体   繁体   中英

PHP filter a database table column into an A-Z listing

This is what I have so far and I dont know where to go at this point:

  // print A-Z Filter based on names of companies
  $qry = db_select('cloud_computing_capability_data', 'cd');
  $qry -> fields('cd', array(
    'company',
  ))
  -> orderBy('company', 'ASC');

  $names = array('company');
  $output = preg_grep('/^[a-z]/i', $names);
  print_r( $output);

Second Try

//    $letter = $_GET['company']
//  $regex = '/^'.$letter.'/i';
//  if ($letter == '#') $letter = '[0-9]';
//  $result = preg_grep($regex, $qry);

i have a table called cloud_computing_capability_data and the column is called company

What I need to do is I need to create a AZ listing... so:

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

and when you click on the letter it will bring up a table with only the words beginning with A's if I clicked A and only the words beginning with numbers if I clicked the # sign.

I was thinking of using a regular expression to accomplish this but I don't want to create 27 different pages. So is there a way to call the letter at the end of the url? like creating something that will do this

http://mywebsite.com/site/list?letter=A

well you need to organize your ideas first of all: the html code will have links each one calling the letter with the regular form

the first part of the php script will have to assign to a variable the value you will send through the link

 <?
 $a=$_GET["letter"];
 ?>

the second part of the script will have to connect to the database and read each row as an array using a loop, each time get the array place 'company' and if the first character of the string

 $result=array();

inside the loop of index i

 if ($str[0]==$a{
     $result[$i]=$query_result;
 }

is equal to $a then save it to a new array

the third part of the script is a loop to print out the last array

 foreach ($result as $value) {
       echo $value;}

Use a code similar to this:

$letter = $_GET['letter'];

$result = $db->prepare("
    SELECT * 
    FROM tableName 
    WHERE fieldName LIKE :letterToSearch 
    ORDER BY fieldName ASC
");

$result->bindValue(':letterToSearch', $letter . '%');
$result->execute();

I've used a basic PDO query to find results in the field fieldName (change it to something else) which START with the letter A or B, or whatever you pass to $letter

You don't need to use regex for this, to output all the letters, firstly do something like this:

foreach(range('A','Z') as $letter)
{
    echo '<a href="directory.php?letter=' . $letter . '">' . $letter . '</a>';
}

Then, on your directory.php page you need to grab the letter using $_GET .

$letter = isset($_GET['letter']) ? mysql_real_escape_string($_GET['letter']) : false;

Now you've got this letter, you just need to select the results that begin with it, to do that, you can use a WHERE statement (I'm not sure what DB class you're using).

WHERE `company` LIKE '{$letter}%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM