2017-08-12 78 views
0

我写了一个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>"; 
?> 
+2

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

回答

0

内如果我的理解正确地说,您想要将每个页面的名字的首字母添加到您的分页中。

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

什么,我会做的是创建过滤器,以显示与每个字母开头的所有条目(ALL ABC 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; 
} 
+0

它是否按组中的记录进行排序,结果是以我选择的字母开头的? 我试了一下(你的回答),但不知何故它不工作,当我点击下面的任何一个时,没有观察到变化。 –

+0

对不起,我写的第一个代码糟糕,复制+粘贴一遍。 – gravi

+0

谢谢你,那很有用 你保存了一天:)。 –

相关问题