0

姓の最初の文字でソートされている結果をフォーマットしようとしていますが、問題があります

次の形式でエコーする必要があります

<section>                       
                      <div id="slider">
                        <div class="slider-content">
                          <ul>
                            <li id="LETTER"><a name="LETTER" class="title">LETTER</a><ul>
                                <li><a href="#">SURNAME</a></li>
                              </ul>
                            </li>

                              </ul>
                            </li>
                          </ul>
                        </div>
                      </div>         
                    </section>

私はそれを分割しようとしましたが(以下のコードを参照)、正しくレンダリングされません

<html>
<head>
<title>MySQLi Read Records</title>
</head>
<body><section>                       
                      <div id="slider">
                        <div class="slider-content">
                          <ul>
<?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


if( $num_results > 0){ //it means there's already a database record


    //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 '<li id="', $row['letter'], '"><a name="', $row['letter'],'" class="title">', $row['letter'],'</a><ul>';
    $lastLetter = $row['letter'];
  echo "bottom";  
}
echo "<li><a href='#'>{$surname} - {$name}</a></li>";


    }


}else{
    //if database table is empty
    echo "No records found.";
}

//disconnect from database
$result->free();
$mysqli->close();

?> </ul>
                            </li>
                          </ul>
                        </div>
                      </div>         
                    </section>

</body>
</html>

これらのセクションをエコーする場所と方法を見つける必要があるため、次のようになります

:- アップデート -:

返信で私はそれをテストしました、そしてそれはこのようなものになります

<body>
<section>
<div id="slider">
<div class="slider-content">
<ul>
<li id="E"><a name="E" class="title">E</a>
  <ul>
  bottom
  <li><a href='#'>egg - smash</a></li>
  <li id="S"><a name="S" class="title">S</a>
    <ul>
    bottom
    <li><a href='#'>surname</a></li>
    <li><a href='#'>surname</a></li>
    <li><a href='#'>surname</a></li>
    <li id="Z">
    <a name="Z" class="title">Z</a>
    <ul>
      bottom
      <li><a href='#'>zoo</a></li>
      <!-- </ul> BAD UL ?-->
      </li>
    </ul>
    </div>
    </div>
    </section>
</body>
</html>

いつあるべきか

<section>                       
                      <div id="slider">
                        <div class="slider-content">
                          <ul>
                            <li id="s"><a name="s" class="title">s</a><ul>
                                <li><a href="#">surname</a></li>
                              </ul>
                            </li>

                              </ul>
                            </li>
                          </ul>
                        </div>
                      </div>         
                    </section> 
4

1 に答える 1

0

常にコードをインデントし、常に変数に対してテストを実行し、ループのように前に単純なテストをスキップする手順を実行しない

<body>
    <section>                       
        <div id="slider">
            <div class="slider-content">
                <ul>
                <?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 letter ASC";
                    //execute the query
                    $result = $mysqli->query( $query );


        //TEST 1st print_r($result); if not working there is an error on your sql


                    //get number of rows returned
                    $num_results = $result->num_rows;
                    //this will link us to our add.php to create new record
                    if( $num_results > 0){ //it means there's already a database records
                    //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 '<li id="', $row['letter'], '"><a name="', $row['letter'],'" class="title">', $row['letter'],'</a><ul>';
                    $lastLetter = $row['letter'];
                    echo "bottom";  
                    }
                    echo "<li><a href='#'>{$surname} - {$name}</a></li>";
                    }
                    }else{
                    //if database table is empty
                    echo "No records found.";
                    }
                    //disconnect from database
                    $result->free();
                    $mysqli->close();
                ?> 
        <!-- </ul> BAD UL ?--> 
                </li>
                </ul>
            </div>
        </div>         
    </section>
</body>
于 2013-08-18T15:26:46.607 に答える