SF23103 Posted October 11, 2011 Share Posted October 11, 2011 I am trying to display entries from a database. I would like to categorize the entries into specific headings when they print to the page. For instance: ID 1 Question: What is your favorite color Answer: Blue Category: Favorites ID 2 Question: What is two plus two? Answer: Four Category: Math I would like to display all "Favorites" under the heading "Favorites", and all "Math" under heading "Math". There will be multiple entries for each. I figure I need to do a loop for each category? How do I do this without having to connect to the database multiple times? <?php $username="username"; $password="pass"; $database="database"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM ft_form_17 WHERE Include='Include'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"question"); //Question $f2=mysql_result($result,$i,"answer"); //Answer $f3=mysql_result($result,$i,"submission_id"); //submission $f4=mysql_result($result,$i,"category"); //category ?> <strong><a name="<?php echo $f3; ?>"><?php echo $f1; ?></a></strong> <?php echo $f2; ?> <?php $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/248868-multiple-loops-under-several-headings/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.