我想要在 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/