Php 如何设置sql查询后结果显示的格式
我一直在尝试格式化我的结果,结果是按姓氏的第一个字母排序的,但有问题 我需要它以下面的格式回音Php 如何设置sql查询后结果显示的格式,php,html,mysql,Php,Html,Mysql,我一直在尝试格式化我的结果,结果是按姓氏的第一个字母排序的,但有问题 我需要它以下面的格式回音 <section> <div id="slider"> <div class="slider-content"> <ul>
<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>
MySQLi读取记录
始终缩进代码,始终对变量运行测试,不要在循环之前跳过简单测试
<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>
您的原始HTML看起来有点混乱,很难判断哪些元素需要放在哪里。标签可能与您预期的方式不匹配。你能检查一下它,或者发布一些示例/期望的输出吗?为什么它应该显示S,我看到它做的是正确的事情?谢谢你的快速回复。我已经尝试了你的重播,但仍然呈现错误。我将更新我的问题,以反映呈现效果,但没有证明这是有效的
print\r($result)代码>当问题是变量时,你在循环中浪费时间,如果问题是变量,那么问题就是它的来源,也就是说,你可以在gui db管理系统中尝试这一点“选择名称、姓氏、手机、上方(左(姓氏,1))作为联系人的信函,按字母ASC排序“
,然后来php上试用
<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>