[英]display data from a certain letter
好的,我使此页面运行良好,但是要显示某个字母的数据我该怎么办?
这是我目前得到的代码
<html>
<head>
<title>MySQLi Read Records</title>
</head>
<body>
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from contacts";
//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
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results > 0){ //it means there's already a database record
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>Firstname</th>";
echo "<th>Lastname</th>";
echo "<th>Username</th>";
echo "<th>Action</th>";
echo "</tr>";
//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 "<tr>";
echo "<td>{$name}</td>";
echo "<td>{$surname}</td>";
echo "<td>{$mobile}</td>";
echo "<td>";
//just preparing the edit link to edit the record
echo "<a href='edit.php?id={$id}'>Edit</a>";
echo " / ";
//just preparing the delete link to delete the record
echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
</body>
</html>
我要放置多个类似以下内容的条目,以正确的字母显示
<h1>A</h1>
echo "<td>{$name}</td>";
echo "<td>{$surname}</td>";
echo "<td>{$mobile}</td>";
<h1>b</h1>
echo "<td>{$name}</td>";
echo "<td>{$surname}</td>";
echo "<td>{$mobile}</td>";
ECT ECT
我要达到的目的是不显示以a开头的所有姓氏
我在此页面上找到了这段代码http://www.sitepoint.com/forums/showthread.php?303895-Display-Data-Beginning-with-a-Specialty-Letter
但是我该如何为我工作?
我是新手(极端),所以任何帮助都很棒,我尝试阅读教程,动手后感觉更好:)
更新* ** * ** * ** * ** * ** * ** * ** * ** * **
这很好用,但现在只列出一行,这是我的代码
<html>
<head>
<title>MySQLi Read Records</title>
</head>
<body>
<?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
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results > 0){ //it means there's already a database record
echo "<table border='1'>";//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
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<h1>', $row['letter'], '</h1>';
$lastLetter = $row['letter'];
echo "{$surname}";
}
}
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
</body>
</html>
在学习之后,我将为您提供有关如何存档所需内容和可以使用的功能的想法。
如前所述, you want all records displayed on the same page with their own alphabet letter as the header
。
有几种方法可以执行所需的操作,最常见的方法是按所需的顺序返回所需字段的ORDERed BY
数据,在本例中为ASC
。
这将按字母顺序列出所有记录。
从那里可以使用function LEFT to extract the first letter as another field
。
现在,您将在这里使用一些PHP,从上面开始,您已经对记录进行了排序,并且每条记录的第一个字母。
您可以在while
内使用类似的方法:
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<h1>', $row['letter'], '</h1>';
$lastLetter = $row['letter'];
}
基本上以上内容将检查是否已设置/定义了不是$lastLetter
的第一条记录,因此它将输入if
,打印您的第一个标题并将$lastLetter
设置$lastLetter
第一个字母。
在第一条记录之后,它将$lastLetter
与$row['letter']
相匹配,一旦它们不相等,它将再次打印标题,并用$row['letter']
更新$lastLetter
,这是当前字母。
您的MySQL查询如下所示:
SELECT *,
LEFT(firstname, 1) AS letter
FROM contacts
ORDER BY firstname
但是,最好定义所有需要的字段,而不是捕获所有字段,以防万一您在该表上有更多字段:
SELECT firstname,
surname,
mobile,
LEFT(firstname, 1) AS letter
FROM contacts
ORDER BY firstname
注意如果您的姓名不规范,则可以使用UPPER函数将字母大写,如下所示:
SELECT firstname,
surname,
mobile,
UPPER(LEFT(firstname, 1)) AS letter
FROM contacts
ORDER BY firstname
我建议您执行以下步骤。
更新您的mysql查询语句
$query = "select * from contacts order by name_field";
Name_field或列名是什么,以便您获得按字典顺序排序的结果集。
保持$ previous记录上次添加的字母。
在每一行使用此函数(请参阅PHP中的startsWith()和endsWith()函数 )来查找名称值是否以与$ previous不同的字母开头。 如果有更改,请更新$ previous并根据需要添加带有字母的“”标签。
function startsWith($haystack, $needle)
{
return $needle === "" || strpos($haystack, $needle) === 0;
}
首先,您应该将SQL查询更改为:
SELECT * FROM contacts ORDER BY name ASC
这将从A到Z的所有联系人排序。现在,要确定联系人姓名的初始字母,请使用substr函数...
$initial_letter = strtoupper(substr($name, 0, 1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.