【问题标题】:display data from a certain letter显示某个字母的数据
【发布时间】:2013-08-18 12:07:35
【问题描述】:

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

这是我目前得到的代码

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

【问题讨论】:

  • 您想在同一页上列出所有信件,还是要在每页请求或表单请求中列出所有信件?
  • 都一样,但必须在自己的标题下:)

标签: php html mysql mysqli


【解决方案1】:

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

正如你提到的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

【讨论】:

  • 感谢您的回答,请稍后再回来查看,我现在正在尝试使用此答案。我已将我的查询更新为 $query = "select * from contacts ORDER BY surname ASC";只是尝试更新其余部分以使其正常工作。
  • @thenashone 你需要像上面提到的那样使用*, LEFT(surname, 1) AS letter 来获取这封信。
  • 是的,见底部更新,您可以使用 UPPER 将所有字母变为大写。
  • @thenashone 将 echo "{$surname}"; 放在 IF 之外,IF 仅用于打印 Letter 而不是其余的。您可以将 if 放在函数 extract($row); 之后其余的保持不变。
  • echo '&lt;li id="', $row['letter'], '"&gt;&lt;a name="', $row['letter'],'" class="title"&gt;', $row['letter'],'&lt;/a&gt;&lt;ul&gt;'; 是的,你忘记正确使用单引号了。
【解决方案2】:

我建议您执行以下步骤。

更新你的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;
}

【讨论】:

  • 请你帮我简化一下,因为我是新手
  • @YourCommonSense 这就是我在查询中包含“按名称字段排序”的原因。怎么了?
  • @thenashone 很遗憾,这个网站不是您的个人辅导服务。您应该先阅读一些手册,然后再提出某个问题。不是一步一步的教程
  • @YourCommonSense 你建议在 php 或 mysql 中做什么?因为你之前的评论是相反的。
  • @humblelearner 是的,我可以显示只是想要的数据,就像我在上面发布的那样。
【解决方案3】:

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

SELECT * FROM contacts ORDER BY name ASC

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

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

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2020-04-10
    • 2017-10-28
    相关资源
    最近更新 更多