如何在sql查询后格式化结果的显示

时间:2013-08-18 15:16:34

标签: php html mysql

我一直在尝试格式化我的结果,这些结果按姓氏的第一个字母排序但有问题

我需要它以下列格式回显

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

1 个答案:

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