douxian5076 2014-02-09 14:01
浏览 12
已采纳

显示字母子标题中表格的数据

I am trying to display a index of my data from a table and decided i would like sub headings for example

A
apple
ant
ape

...

Z
zoo
zebra

is there a function that can do this?

this is my code at moment I haven't tried anything as not sure what the function i'm looking for

<?php
//include database connection
include 'inc/dbcon.php';

//query all records from the database
$query = "select *
                        from db ORDER BY item ASC";

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

    //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
            echo "<h2>{$item}</h2><br>";
echo "";

    }

    echo "";

}else{
    //if database table is empty
    echo "Shit out of luck there is No records found.";
}

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

?>

this at the moment is giving me a list

  • 写回答

1条回答 默认 最新

  • dongqia3502 2014-02-09 14:27
    关注

    You could do this for your while loop:

        $letter = "";
    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(substr(strtoupper($item),0,1) != $letter)
        {
            $letter = substr(strtoupper($item),0,1);
            echo("<h2>$letter</h2><br>");
        }
        echo "<h2>{$item}</h2><br>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端