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

しかし、どうすればこれを機能させることができますか?

私は初心者です(非常に)ので、どんな助けでも素晴らしいです私はチュートリアルを読んでみました私はそれを実際に使った方が良いと思います:)

更新* ** * ** * ** * ** * ** * ** * ** * ** * **

これはうまく機能していますが、今では1行しかリストされていません。これは私のコードです

<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 に答える