Jump to content

if statements inside while loop need just one header..


turpentyne

Recommended Posts

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>

Link to comment
Share on other sites

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>';

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.