繁体   English   中英

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

[英]Display data from table in letter sub headings

我试图显示我的数据从表中的索引,并决定我想要例如子标题

A
apple
ant
ape

...

Z
zoo
zebra

有功能可以做到这一点吗?

这是我的代码,目前我还没有尝试任何操作,因为不确定我在寻找什么功能

<?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();

?>

此刻正在给我一个清单

您可以为while循环执行此操作:

    $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>";
}

暂无
暂无

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

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