我写了一个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>";
?>
你想从我们这里得到什么? –