【发布时间】:2025-11-29 00:20:05
【问题描述】:
我编写了一个 PHP 代码来按名称 ASC 显示数据库中的数据,然后将数据分成页面,每个页面最多包含 20 条记录。
我希望添加更多内容,例如在每个页码之后,一些信息将显示显示名字的第一个字母(第一个记录) - 姓氏的第一个字母(最后一个记录)。
喜欢:
1(A-H), 2(H-P), 3(P-U), 4(U-Z)
在这里,第一页包含记录,其中第一条记录(姓名的第一个字母)和最后一条记录(姓氏的第一个字母)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tssolutions";
$results_per_page = 20;
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
//echo "connected successfully";
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $results_per_page;
//actual query to show
$sql = "select name, phone, details, photo from phone order by name asc LIMIT $start_from, ".$results_per_page;
$result = $conn->query($sql);
echo "<div class='w3-row-padding w3-grayscale'>";
echo "<style>
img.chip
{
float:left;
height:25%;
width:25%;
border-radius: 50%;
}
</style>";
if ($result->num_rows > 0)
{
//output data of each row
while($row = $result->fetch_assoc())
{
echo "<div class='w3-col 3 m3 w3-margin-bottom w3-border w3-btn w3-padding w3-white' style='text-color:black;min-width:300px;'>";
echo "<div class='w3-half'>";
if(base64_encode( $row['photo'] )== !NULL )
{
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['photo'] ).'" class="chip" style="height:120px;width:120px;margin-top:20px;">';
}
else
{
echo "<img src='./images/noimage.jpg' alt='Jane' class='chip' style='height:120px;width:120px;margin-top:20px;'>";
}
echo "</div><div class='w3-half'>";
echo "</h3>" .$row['name']. "</h3>";
echo "<p class='w3-opacity'>" .$row['phone']. "</p>";
echo "<p style='text-align:justify'>" .$row['details']. "</p>";
echo "<p><button class='w3-button w3-light-grey w3-block'>Contact</button></p>";
echo "</div>";
echo "</div>";
}
echo "</div>";
}
else {
echo "0 results";
}
$anothersql = "SELECT COUNT(name) AS total FROM phone";
$anotherresult = $conn->query($anothersql);
$anotherrow = $anotherresult->fetch_assoc();
$total_pages = ceil($anotherrow["total"] / $results_per_page); // calculate total pages with results
echo"
<style>
a
{
text-decoration:none;
}
.curPage
{
text-decoration:underline;
font-size:30px;
}
</style>
";
echo"<div style='text-align:center;margin-bottom:3px;';><h4>";
for ($i=1; $i<=$total_pages; $i++) { // print links for all pages
echo "<a href='alumni.php?page=".$i."'";
if ($i==$page) echo "class='curPage'";
echo ">".$i."</a> ";
};
echo "</h4></div>";
mysqli_close($conn);
echo "<hr>";
?>
【问题讨论】:
-
你想从我们这里得到什么?