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