如何从mysql中按字母顺序列出wiki

时间:2011-02-02 01:07:22

标签: php mysql

如何在维基页面列表等标题下按字母顺序列出mysql的结果。

示例:

一个
苹果


香蕉

等等

我的sql如下:

$sql = "SELECT id, title FROM myTable ORDER BY title ASC";

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

<?php
while ( $row = mysql_fetch_assoc($result) ){

    $first_letter = mb_substr($row['title'],0,1);
    if($tmp!=$first_letter){
        $tmp = $first_letter;
        echo '<h3 >'.$tmp.'</h3>';
    }
    echo $row['title'];

} //end while
?>

答案 1 :(得分:1)

试试这个

SELECT UPPER( SUBSTRING( title, 1, 1 ) ) AS 
FIRST , id, title
FROM myTable
ORDER BY FIRST ASC 
LIMIT 0 , 30

答案 2 :(得分:0)

<?php

$arrayAlphabet = array('a','b','c','d',....................,'y','z'); //DONT FORGET FILLING DOTS. :D

foreach($arrayAlphabet AS $apha){
    $sql = "SELECT id, title FROM myTable WHERE title LIKE '".$alpha."%' ORDER BY title ASC";
    $result = mysql_query($sql);
    if(mysql_num_rows($result)){
        while($rows = mysql_fetch_assoc($result)){
            $arrayOfTitles[$alpa][] = $rows;
        }
    }
}

foreach($arrayAlphabet AS $apha){
    ?>
    <div><span class="header"><?php echo strtoupper($alpha);?></span>
      <div class="titles">
        <?php
        foreach($arrayOfTitles[$alpa] AS $arrayKey $arrayValue){
          ?><div class="title">$arrayOfTitles[$alpa][$arrayKey]['title']</div><?php
        }
        ?>
      </div>
    </div>
}

?>

答案 3 :(得分:-2)

   **sorting the record alphetical order A B C D** ....Z when i **click A Letter** then its **show all name start with A letter** ,and click C Letter then its show all name start with C letter

     <?php
 $host = "localhost";  
 $user = "root";
 $pw = "";
 $database = "test";

 $con = mysql_connect($host,$user,$pw)
   or die("Cannot connect to mySQL.");

 mysql_select_db($database,$con)
   or die("Cannot connect to database.");




$errormsg= "No Record Found...!";


$alpha="%";
if (isset($_REQUEST['alpha'])) {
$alpha = $_REQUEST['alpha']."%";
}
$q1 = mysql_query("select * from registration where firstname like '$alpha%' " );





?>

<div >
       <form action="" method="post" >
<table  >
                         <td><div  style="float:left;">
  <CENTER>
     <FONT COLOR=Green>Sort by Alphabet:</FONT>  
                                <A HREF="<?php echo $_SERVER['PHP_SELF']; ?>">All</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=A">A</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=B">B</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=C">C</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=D">D</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=E">E</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=F">F</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=G">G</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=H">H</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=I">I</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=J">J</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=K">K</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=L">L</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=M">M</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=N">N</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=O">O</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=P">P</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Q">Q</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=R">R</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=S">S</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=T">T</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=U">U</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=V">V</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=W">W</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=X">X</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Y">Y</A>
     <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Z">Z</A>
   </CENTER>

         </div>   


   </td></tr>

           <tbody>
    <?php



    $i=1;
    $num_rows=mysql_num_rows($q1);
    if($num_rows==0)
    {
    ?>
   <div align="center">
    <div class="alert alert-danger alert-dismissable">

      <?php echo $errormsg; ?></div>
    </div>
           <?php
    }
    else
    {
    while($roww=mysql_fetch_array($q1))
    {                
      ?>
           <tr>
    <td><div style="height:100px;float:left;">
        <input name="" type="checkbox" value="">
      </div>
       <div><a href="#">  <b><?php echo $roww["firstname"]; ?></b></a><br />
       :&nbsp;<?php echo $roww["firstname"]; ?><br />
        <?php echo $roww["lastname"]; ?><br />
        <?php echo $roww["password"]; ?><br />
        <?php echo $roww["gender"]; ?><br />
      </div></td>
   </tr>
           <?php }
          $i++;
          }

          ?>
           </tbody>

         </table>
       </form>
     </div>
   </body></html>
  <SCRIPT LANGUAGE=JavaScript>
       <!--
         document.write(ALPHABET())
       //-->
     </SCRIPT>


CREATE TABLE IF NOT EXISTS `registration` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(20) NOT NULL,
`lastname` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,

`gender` varchar(20) NOT NULL,



PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;