turpentyne Posted July 27, 2012 Share Posted July 27, 2012 I can't think of how I'd do this... I have a database query that pulls results. Then, within that while statement I want to generate a subheader: Options 1, and show the results that match that category, then generate a 2nd subheader, Options 2, and so on.. Here's what I've got so far, and it's obviously looping the headers for each result right now... <?php include("dbc.php"); $query_options = "SELECT component_name, image_filepath, component_category FROM tbl_components"; $result = mysql_query($query_options); ?> <!-- html code code code --> <?php while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if ($row[component_category]=="1") { ?> <!-- start what shows only once for this if statement - this is what I don't know how to set up --> <div id="accessory-rail-mounts"> <a class="select-toggler" href="javascript:showHide('accessory-rail-expander');"> <img src="images/structural/red-plus.gif" style="position:relative;top:-2px;"/> ACCESSORY RAIL MOUNTS</a><!-- end --> <!-- what I want looping within this category --> <div style='float:left;padding-right:25px;' width='90'><?php echo $row['component_name']; ?> <br> <img src="<?php echo $row['image_filepath']; ?>" width='90'></div> <!-- end what I want looping within this category --> <!-- start what shows only once for this if statement --> </div><!-- end --> <?php } // 2nd category, like the first... if ($row[component_category]=="11") { ?> <div id="calibers"> <a class="select-toggler" href="javascript:showHide('caliber-expander');"> <img src="images/structural/red-plus.gif" style="position:relative;top:-2px;"/> CALIBERS</a> <div id="caliber-expander" style="display:none;"><?php echo $row['component_name']; ?><br /> <?php echo "<img src=\"{$row['image_filepath']}\" width='90'></div> "; ?> <br /> </div> </div> <?php } // and so on, through four or five different categories // then, close while statement... } ?> <!-- html code code code --> </body> Quote Link to comment https://forums.phpfreaks.com/topic/266357-if-statements-inside-while-loop-need-just-one-header/ Share on other sites More sharing options...
DavidAM Posted July 27, 2012 Share Posted July 27, 2012 You have to ORDER the SELECT by the category (or whatever) and keep track of the category that is being processed. The logic is something like this: $lastCategory = null; while ($row = mysql_fetch_assoc( ... ) ) { // Did the category change? if ( $row['Category'] != $lastCategory) { // Close the previous category if (!empty($lastCategory)) echo '</DIV>'; $lastCategory = $row['Category']; // Start new category echo '<DIV> ...'; } // Output the details echo $row['Whatever']; } // Make sure we close the last category if (! empty($lastCategory)) echo '</DIV>'; Quote Link to comment https://forums.phpfreaks.com/topic/266357-if-statements-inside-while-loop-need-just-one-header/#findComment-1364967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.