[英]how to format the display of results after a sql query
我一直在嘗試格式化我的結果,該結果按姓氏的第一個字母排序,但是有問題
我需要它以以下格式回顯
<section>
<div id="slider">
<div class="slider-content">
<ul>
<li id="LETTER"><a name="LETTER" class="title">LETTER</a><ul>
<li><a href="#">SURNAME</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</section>
我已經嘗試將其拆分(請參見下面的代碼),但無法正確呈現
<html>
<head>
<title>MySQLi Read Records</title>
</head>
<body><section>
<div id="slider">
<div class="slider-content">
<ul>
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = " SELECT name,
surname,
mobile,
UPPER (LEFT(surname, 1)) AS letter
FROM contacts
ORDER BY surname";
//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
//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
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<li id="', $row['letter'], '"><a name="', $row['letter'],'" class="title">', $row['letter'],'</a><ul>';
$lastLetter = $row['letter'];
echo "bottom";
}
echo "<li><a href='#'>{$surname} - {$name}</a></li>";
}
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?> </ul>
</li>
</ul>
</div>
</div>
</section>
</body>
</html>
我需要找到在哪里以及如何回顯這些部分,所以就像這樣
:-更新-:
收到回復后,我就把它弄亂了,就像這樣
<body>
<section>
<div id="slider">
<div class="slider-content">
<ul>
<li id="E"><a name="E" class="title">E</a>
<ul>
bottom
<li><a href='#'>egg - smash</a></li>
<li id="S"><a name="S" class="title">S</a>
<ul>
bottom
<li><a href='#'>surname</a></li>
<li><a href='#'>surname</a></li>
<li><a href='#'>surname</a></li>
<li id="Z">
<a name="Z" class="title">Z</a>
<ul>
bottom
<li><a href='#'>zoo</a></li>
<!-- </ul> BAD UL ?-->
</li>
</ul>
</div>
</div>
</section>
</body>
</html>
什么時候應該
<section>
<div id="slider">
<div class="slider-content">
<ul>
<li id="s"><a name="s" class="title">s</a><ul>
<li><a href="#">surname</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</section>
始終縮進您的代碼,始終對變量運行測試,在執行循環之前不要采取跳過簡單測試的步驟
<body>
<section>
<div id="slider">
<div class="slider-content">
<ul>
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = " SELECT name,
surname,
mobile,
UPPER (LEFT(surname, 1)) AS letter
FROM contacts
ORDER BY letter ASC";
//execute the query
$result = $mysqli->query( $query );
//TEST 1st print_r($result); if not working there is an error on your sql
//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 records
//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
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<li id="', $row['letter'], '"><a name="', $row['letter'],'" class="title">', $row['letter'],'</a><ul>';
$lastLetter = $row['letter'];
echo "bottom";
}
echo "<li><a href='#'>{$surname} - {$name}</a></li>";
}
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
<!-- </ul> BAD UL ?-->
</li>
</ul>
</div>
</div>
</section>
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.