0

好的,我让这个页面运行良好,但是我必须怎么做才能显示来自某个字母的数据?

这是我目前得到的代码

<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 然后 b 开头的姓氏

我在这个页面上找到了这段代码http://www.sitepoint.com/forums/showthread.php?303895-Display-Data-Beginning-with-a-Particular-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>
4

3 回答 3

0

既然你正在学习,我会告诉你如何归档你想要的东西以及你可以使用的功能。

正如你所提到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$row['letter'],一旦它们不相等,它会再次打印标题并更新$lastLetter当前$row['letter']字母。

您的 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

在此处查看有关该UPPER功能的更多信息

于 2013-08-18T12:39:26.067 回答
-1

我建议您执行以下步骤。

更新你的 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;
}
于 2013-08-18T12:15:53.463 回答
-1

首先,您应该将 SQL 查询更改为:

SELECT * FROM contacts ORDER BY name ASC

这将对所有联系人从 A 到 Z 进行排序。现在,要确定联系人姓名的初始字母表,请使用 substr 函数...

$initial_letter = strtoupper(substr($name, 0, 1));
于 2013-08-18T12:20:11.097 回答