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

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

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