[英]Display data from table in letter sub headings
我試圖顯示我的數據從表中的索引,並決定我想要例如子標題
A
apple
ant
ape
...
Z
zoo
zebra
有功能可以做到這一點嗎?
這是我的代碼,目前我還沒有嘗試任何操作,因為不確定我在尋找什么功能
<?php
//include database connection
include 'inc/dbcon.php';
//query all records from the database
$query = "select *
from db ORDER BY item ASC";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record
//start table
//creating our table heading
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<h2>{$item}</h2><br>";
echo "";
}
echo "";
}else{
//if database table is empty
echo "Shit out of luck there is No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
此刻正在給我一個清單
您可以為while循環執行此操作:
$letter = "";
while( $row = $result->fetch_assoc() )
{
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
if(substr(strtoupper($item),0,1) != $letter)
{
$letter = substr(strtoupper($item),0,1);
echo("<h2>$letter</h2><br>");
}
echo "<h2>{$item}</h2><br>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.