javascript - php 按字母顺序分页

标签 javascript php jquery ajax

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

我有数据库测试

表名称:tbl_student

属性为: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>  

最佳答案

问题是您在 url 中使用了 charact 作为变量名,在 $_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>

关于javascript - php 按字母顺序分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202534/

相关文章:

php - Yii框架前后端分离

javascript - 选中单选按钮时尝试使用 javascript 添加唯一类

javascript - 自动缩放 DIV 元素并考虑页眉和页脚直到滚动

javascript - 在 jQuery 中,使用 $.extend(),这是多余的吗?

php - Laravel 表单验证,如果不为空则失败

php - 在 Laravel 中,如何在一秒钟内发出并发请求时避免重复记录

javascript - 如何检查动态数组是否为空?

javascript - 通过 JavaScript 获取输入的 CSS 转换值

jquery - data.find ('#myDiv' ) 不是 $.post 请求中的函数

javascript - 在 DOM 中更新图像的最有效方法是什么?