php - how to format the display of results after a sql query -
i have been trying format results being sorted first letter of surname having problems
i need echo in following format
<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>
i have tried split (see code bellow) not rendering correctly
<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 records database $query = " select name, surname, mobile, upper (left(surname, 1)) letter contacts order surname"; //execute query $result = $mysqli->query( $query ); //get number of rows returned $num_results = $result->num_rows; //this link our add.php create new record if( $num_results > 0){ //it means there's database record //creating our table heading //loop show each records while( $row = $result->fetch_assoc() ){ //extract row //this make $row['firstname'] //just $firstname 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 empty echo "no records found."; } //disconnect database $result->free(); $mysqli->close(); ?> </ul> </li> </ul> </div> </div> </section> </body> </html>
i need find , how echo these sections this
:- update -:
with reply got itested , goes this
<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>
when should be
<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>
always indent code, run tests on variables, not take steps skipping simple testing before looping
<body> <section> <div id="slider"> <div class="slider-content"> <ul> <?php //include database connection include 'db_connect.php'; //query records database $query = " select name, surname, mobile, upper (left(surname, 1)) letter contacts order letter asc"; //execute query $result = $mysqli->query( $query ); //test 1st print_r($result); if not working there error on sql //get number of rows returned $num_results = $result->num_rows; //this link our add.php create new record if( $num_results > 0){ //it means there's database records //creating our table heading //loop show each records while( $row = $result->fetch_assoc() ){ //extract row //this make $row['firstname'] //just $firstname 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 empty echo "no records found."; } //disconnect database $result->free(); $mysqli->close(); ?> <!-- </ul> bad ul ?--> </li> </ul> </div> </div> </section> </body>