【问题标题】:How can I get the first letter of first and last row name in every page?如何获取每页中第一行和最后一行名称的第一个字母?
【发布时间】: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>&nbsp;&nbsp;"; 
};
   echo "</h4></div>";

mysqli_close($conn);

echo "<hr>";
?>

【问题讨论】:

  • 你想从我们这里得到什么?

标签: php sql


【解决方案1】:

如果我理解正确,您想将每个页面的名字的起始字母添加到您的分页中。

我能想到的唯一方法是在分页循环中添加一个查询,它只选择页面的第一个条目,然后将结果格式化为只显示第一个字母,但这会影响性能。

我要做的是创建过滤器来显示以每个字母开头的所有条目(ALL A B C D...)

echo "<a href='alumni.php'>ALL </a>";
foreach (range('A', 'Z') as $char) {
    echo "<a href='alumni.php?name=" . $char . "'>" . $char . " </a>";
}

并将注释为“要显示的实际查询”的部分更改为类似

if( isset( $_GET['name'] ) ) {      
    $letter = substr( $_GET['name'], 0, 1 );    
    $sql = "select name, phone, details, photo from phone WHERE name LIKE '".$letter."%' order by name asc LIMIT $start_from, ".$results_per_page;
} else 
    $sql = "select name, phone, details, photo from phone order by name asc LIMIT $start_from, ".$results_per_page;
}

【讨论】:

  • 它是否对记录进行分组排序,结果以我选择的字母开头?我试过了(你的答案)但不知何故它不起作用,当我点击下面的任何一个时没有观察到任何变化。
  • 对不起,我写的第一个代码不好,复制+粘贴一遍。
  • 谢谢,这很有用,你拯救了这一天:)。
  • 很高兴为您提供帮助:)