Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts

    Need header before if/looped results

    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 Code:
    <?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($resultMYSQL_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> &nbsp;
    <!-- 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> &nbsp;"?>
     
     
     <br />
    </div> 
     </div> 

      
     <?php
    }
    // and so on, through four or five different categories 

    // then, close while statement...
    }

    ?>

    <!-- html code code code -->
    </body>

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    So you can do this one of two ways:

    1) Create a multidimensional array, and loop through the MySQL results to put them into that array. Have the first dimension be the category, and the second dimension be the data. Then loop through that multidimensional array to actually display the data.

    2) Create an array of true/false values. Have the key of that array be the category, and if it exists and is true, you've already displayed the category header, don't show it again.

    Personally, I like the first method more then the second.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Ok... a little closer now, but still lost. I'm new to multidimensional arrays, and not sure in this how/where to generate my headers for each array? Or is this still just creating one long array? Hopefully I'm getting closer, anyway. Here's where I've gotten to:

    PHP Code:
    <?php 
    include("../builder-test-code/dbc.php");

    $query_categories "SELECT ID, category_name FROM tbl_categories";
    $result mysql_query($query_options);
    while(
    $row mysql_fetch_assoc($result)){
    // this first query is to pull the category titles from another table - 
    // not sure what to do with it just yet, but I was thinking I'd include this 
    // in my loop some how to generate the headers names . for example if the main query category id = an id in this query result, to print that title at the start of each grouping..
        
    }
     
    $query_options  "SELECT component_name, image_filepath, component_category FROM tbl_components";
    $result mysql_query($query_options);

    while(
    $row mysql_fetch_assoc($result)){

    $categorized_items[$row['component_category']][] = array($row['component_name'], $row['image_filepath']);

     
    }


    function 
    PrintArray($categorized_items)
    {
        if(
    is_array($categorized_items))
        {
            
            foreach(
    $categorized_items as $key=>$value)
            {
                
                if(
    is_array($value))
                {
                
    PrintArray($value);

                }
                else
                {
                echo 
    "$key: $value<br>";
                }
            }
        }
    }  

    ?>


    <?php
    echo "and now...<br>";
    PrintArray($categorized_items);

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •