显示某个字母的数据

时间:2013-08-18 12:07:36

标签: php html mysql mysqli

好的,我的这个页面运行良好但是我需要做些什么才能显示某个字母的数据?

这是我目前得到的代码

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

3 个答案:

答案 0 :(得分: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

See more about the UPPER function here

答案 1 :(得分:-1)

我建议您执行以下步骤。

更新你的mysql查询语句

$query = "select * from contacts order by name_field";

Name_field或列名称,以便您获得按字典顺序排序的结果集。

保留$ previous以跟踪上次添加的字母。

在每一行使用此功能(请参阅startsWith() and endsWith() functions in PHP),以查找名称值是否以与$ previous不同的字母开头。如果有更改,则更新$ previous并根据需要添加带有字母的“”标签。

function startsWith($haystack, $needle)
{
    return $needle === "" || strpos($haystack, $needle) === 0;
}

答案 2 :(得分:-1)

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

SELECT * FROM contacts ORDER BY name ASC

这会将所有联系人从A排序到Z.现在,为了能够确定联系人姓名的初始字母,请使用substr函数...

$initial_letter = strtoupper(substr($name, 0, 1));