在PHP中按字母顺序分页

时间:2016-10-23 11:46:01

标签: javascript php jquery ajax

我想在php中按字母顺序分页。我有一个代码,但有一些问题。当我单击任何字母时,在地址栏中选择的字母显示,但从所选字母开始的数据不显示。

我有数据库测试

表名: tbl_student

atrributes是: student_id,student_name,student_phone

我的代码是:

 <?php  
$connect = mysqli_connect('localhost', 'root', '', 'testing');  
$char = '';  
if(isset($_GET["char"]))  
 {  
  $char = $_GET["char"];  
  $char = preg_replace('#[^a-z]#i', '', $char);  
  $query = "SELECT * FROM tbl_student WHERE student_name LIKE '$char%'";  
  }  
  else  
  {  
  $query = "SELECT * FROM tbl_student ORDER BY student_id";  
  }  
  $result = mysqli_query($connect, $query);  
 ?>  
<!DOCTYPE html>  
 <html>  
  <head>  
       <title> Alphabetic Pagination</title>  
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  </head>  
  <body>  
       <br /><br />  
       <div class="container" style="width:1100px;">  

            <div class="table-responsive">  
                 <div align="center">  
                 <?php  
                      $character = range('A', 'Z');  
                      echo '<ul class="pagination">';  
                      foreach($character as $alphabet)  
                      {  
                           echo '<li><a href="index.php?character='.$alphabet.'">'.$alphabet.'</a></li>';  
                      }  
                      echo '</ul>';  
                 ?>  
                 </div>  
                 <table class="table table-bordered">  
                      <tr>  
                           <th width="20%">ID</th>  
                           <th width="50%">Student Name</th>  
                           <th width="30%">Student Phone</th>  
                      </tr>  
                      <?php  
                      if(mysqli_num_rows($result) > 0)  
                      {  
                           while($row = mysqli_fetch_array($result))  
                           {  
                      ?>  
                      <tr>  
                           <td><?php echo $row["student_id"]; ?></td>  
                           <td><?php echo $row["student_name"]; ?></td>  
                           <td><?php echo $row["student_phone"]; ?></td>  
                      </tr>  
                      <?php  
                           }  
                      }  
                      else  
                      {  
                      ?>  
                      <tr>  
                           <td colspan="3" align="center">Data not Found</td>  
                      </tr>  
                      <?php  
                      }  
                      ?>  
                 </table>  
            </div>  
       </div>  
  </body>  

1 个答案:

答案 0 :(得分:0)

问题是您在url中使用character作为变量名,在$ _GET []数组中使用char

无论哪种 将$_GET["char"]更改为$ _GET [“character”]`

或者 改变

<li><a href="index.php?character='.$alphabet.'">'.$alphabet.'</a></li>

<li><a href="index.php?char='.$alphabet.'">'.$alphabet.'</a></li>