繁体   English   中英

SQL查询后如何格式化结果显示

[英]how to format the display of results after a sql query

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

我需要它以以下格式回显

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

始终缩进您的代码,始终对变量运行测试,在执行循环之前不要采取跳过简单测试的步骤

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM