简体   繁体   English

PHP将数据库表列过滤到AZ列表中

[英]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 我有一个名为cloud_computing_capability_data的表,该列称为company

What I need to do is I need to create a AZ listing... so: 我需要做的是创建一个AZ列表...因此:

# 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. 当您单击字母时,如果我单击A,将仅显示一个以A开头的单词的表,如果单击#号,则仅显示以数字开头的单词的表。

I was thinking of using a regular expression to accomplish this but I don't want to create 27 different pages. 我当时正在考虑使用正则表达式来完成此操作,但我不想创建27个不同的页面。 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 好吧,您需要首先组织您的想法:html代码将具有链接,每个链接都以常规格式调用字母

the first part of the php script will have to assign to a variable the value you will send through the link php脚本的第一部分将必须为变量分配您将通过链接发送的值

 <?
 $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 脚本的第二部分将必须连接到数据库,并使用循环将每一行读取为数组,每次获取数组的位置为“ company”,并且如果字符串的第一个字符

 $result=array();

inside the loop of index i 在索引i的循环内

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

is equal to $a then save it to a new array 等于$ a然后将其保存到新数组

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 我使用了基本的PDO查询来在字段fieldName (将其更改为其他名称)中查找结果,该字段以字母A或B开头,或者传递给$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 . 然后,在您的directory.php页面上,您需要使用$_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语句(我不确定您使用的是哪种DB类)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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