【问题标题】:Alphabetical listing group by alphabet in php [closed]php中按字母顺序排列的列表组[关闭]
【发布时间】:2013-07-14 10:10:36
【问题描述】:

我有一个成员表,我在其中存储有关成员的所有详细信息,包括其姓名、电子邮件、电话等。我希望该名称将显示为按字母顺序排列的组。如示例所示。

A
  Alan
  Alex
  Amar
  Andy

B
  Bob
  Brad

C
  Calvin
  Clerk
D

E

我可以使用 ASC 的顺序按字母顺序对字段进行排序,但是如何将它们放在字母组中。

欢迎提出任何建议。我正在使用 php。

【问题讨论】:

  • 什么组?你想在你的数据库中,在一个数组中,在输出中对它们进行分组吗?你已经有代码了吗?
  • 数据显示问题通常最好在应用程序级别处理 - 例如有一个作用于有序列表的简单循环。
  • 是应用层还是表现层? @草莓
  • 但我想要一些方法来做到这一点
  • 我现在有按字母排序的数组,我如何在 php 中仅使用单个循环显示这样的数组

标签: php mysql


【解决方案1】:

如果你想这样做,在你的 SQL 中;

SELECT SUBSTRING(name, 1, 1) as alpha, name from 'user' GROUP BY SUBSTRING(name, 0, 2), name order by 'alpha', 'name'

在php中

 <?php

    $temp = array(); // would also generate a dynamic array
    $result = mysql_query("SELECT SUBSTRING(name, 1, 1) as alpha, name from 'user' GROUP BY SUBSTRING(name, 0, 2), name order by 'alpha', 'name'"
    while ($row = mysql_fetch_array($result)) {
        $temp[$row['alpha']][] = $row['name'];
    }

    /* this would create array such as;

    'A'
        --> 'Adam'
        --> 'Apple' 
    'B'
        --> 'Ba...'
        --> 'Be...' 
    */

?>

希望这会有所帮助。

【讨论】:

    【解决方案2】:
    try this
    /* Get the letter user clicked on and assign it a variable called $sort */
    $sort = $_REQUEST['letter'];
    /* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */
    if($sort == ""){
    $qry= "SELECT * FROM tbl_customers ORDER BY lastname ASC ";
    }else{
    /* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */
    $qry = "SELECT * FROM tbl_customers WHERE lastname LIKE '$sort%' ORDER BY lastname ASC";
    }
    /* Notice the use of '%' wilde card in the above query  "LIKE '$sort%'". */
    //next step is to execute the query.
    $execute = mysql_query($qry) or die(mysql_error());
    /* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */
    echo "<p>";
    for ($i = 65; $i < 91; $i++) {
    printf('<a href="%s?letter=%s">%s</a> | ',
    $PHP_SELF, chr($i), chr($i));
    }
    echo "</p>";
    /* now we are ready to display the results. Since out tbl_customers table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table.
    To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */
    if(mysql_num_rows($execute)>0){
    do{
    echo "<p>" .$result['id']. " " .$result['firstname']. " " .$result['lastname']. "</p>";
    }while($result = mysql_fetch_assoc($execute));
    }else{
    echo "<p>No customer found.</p>";
    }
    

    【讨论】:

      【解决方案3】:

      这个问题应该有答案了——How to display an Array under alphabetical letters using PHP?

      选择的答案:

      $previous = null;
      foreach($array as $value) {
          $firstLetter = substr($value, 0, 1);
          if($previous !== $firstLetter) echo "\n".$firstLetter."\n---\n\n";
          $previous = $firstLetter;
      
          echo $value."\n";
      }
      

      【讨论】:

        【解决方案4】:

        也许这是一个不好的方法,但它现在可以帮助你。

        <?php
        
            $aGroup = array(
                "A" => array(),
                "B" => array(),
                "C" => array(),
                "D" => array(),
                "E" => array(),
                // until "Z"
                );
        
            $result = mysql_query("SQL as you want");
        
            while ($row = mysql_fetch_array($result)) {
                $letter = strtoupper($row[0][0]); // I supose that the first column is the name
                $aGroup[$letter][] = $row;
            }
        
        ?>
        

        【讨论】:

        • 在 $letter = strtoupper($row[0][0]);我得到名字和第一个字母,然后我可以按字母顺序分组。希望对你有所帮助!
        • 我不是一个编码员,但就个人而言,我认为这是一个好方法!
        • 它是为了保持简单易读。有更多方法可以做得更好,但我认为这是一种更快的方法。
        【解决方案5】:

        你可以像这个例子一样使用你的选择查询来做到这一点:

        $query = "SELECT * FROM `user` ORDER BY `name` ASC ;";
        

        【讨论】:

        • 我已经这样做了
        • OP想要字母结果的组,ORDER ASC返回所有收集的数据而不是分组。
        • 你能 var_dump 你的结果并在这里发布,我可以给你一个脚本。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        相关资源
        最近更新 更多