如何在页面上添加按字母顺序排列的分页

时间:2015-07-15 05:13:37

标签: php mysql

我是php&的新手开发一个页面,我想从db&获取数据分页应按字母顺序排列。即A | B | C | ....单击A时,它应显示仅以A开头的名称。目前我只是使用LIMIT功能。 请帮忙 !

<?php
     $host="localhost"; // Host name 
     $username="root"; // Mysql username 
     $password=""; // Mysql password 
     $db_name="testmra"; // Database name  
     // Connect to server and select databse.
     $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
     mysqli_select_db($conn,$db_name);
     $sql = "SELECT * FROM newuser WHERE role='User' ORDER BY name asc LIMIT 15";
     $query = mysqli_query($conn,$sql);

     echo "<table border='1' id='mytable' width='100%'>
         <tr><th colspan='9' align='center'><h2>User Details</h2></th></tr>
         <tr bgcolor='grey'>
             <th width='25%'>Full Name</th>
             <th width='25%'>Department</th>
             <th width='25%'>EmailId</th>
             <th width='25%'>Username</th>
         </tr>";
        while($row = mysqli_fetch_array($query))
        {
          echo "<tr>";
          echo "<td align='center'>" . $row['name']. "</td>";
          echo "<td align='center'>" . $row['department']. "</td>";
          echo "<td align='center'>" . $row['emailid'] . "</td>";
          echo "<td align='center'>" . $row['username'] . "</td>";
          echo "</tr>";
        }
     echo "</table>";
     mysqli_close($conn);   
?>

2 个答案:

答案 0 :(得分:1)

    <?php
     $host="localhost"; // Host name 
     $username="root"; // Mysql username 
     $password=""; // Mysql password 
     $db_name="testmra"; // Database name  
     // Connect to server and select databse.

     $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
     mysqli_select_db($conn,$db_name);
     if(isset($_GET['get'])){
       $get = mysqli_real_escape_string($_GET['get']);
     }else{
      $get = '';
     }
     $sql = "SELECT * FROM newuser WHERE role='User' AND name LIKE '$get%' ORDER BY name asc LIMIT 15";
     $query = mysqli_query($conn,$sql);

     echo "<table border='1' id='mytable' width='100%'>
         <tr><th colspan='9' align='center'><h2>User Details</h2></th></tr>
         <tr bgcolor='grey'>
             <th width='25%'>Full Name</th>
             <th width='25%'>Department</th>
             <th width='25%'>EmailId</th>
             <th width='25%'>Username</th>
         </tr>";
        while($row = mysqli_fetch_array($query))
        {
          echo "<tr>";
          echo "<td align='center'>" . $row['name']. "</td>";
          echo "<td align='center'>" . $row['department']. "</td>";
          echo "<td align='center'>" . $row['emailid'] . "</td>";
          echo "<td align='center'>" . $row['username'] . "</td>";
          echo "</tr>";
        }
     echo "</table>";
     mysqli_close($conn);   
?>
<?php
$alphas = range('A', 'Z');
 foreach ($alphas as $key) {?>
<a href="answer.php?get=<?php  echo $key  ?>"><?php echo $key ?></a>
<?php }?>

使用此功能,您将获得每个字母表,因此使用ajax调用它,您将使用get方法获取字母表。为此代码创建新页面answer.php。

答案 1 :(得分:1)

我想将此作为对已接受答案的评论添加,但我还没有足够的声誉,对不起!

答案很好,但我强烈建议不要在SQL语句中放置变量,尽管在插入之前转义字符串,它仍然是一个潜在的风险。

StackOverflow: Preventing SQL injection

以上链接详细说明了如何正确处理此事。任何级别的SQL参与者都非常值得一读。