SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Location
    Brookfield, MO
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using optgroup with php

    I am wanting to create a dropdown menu that pulls info from a MySQL database. I want to separate the info into catagories using optgroup. Can someone please help me with the code to do this? I am new to php programming and need plenty of help. Thanks!
    jocoder

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Its difficult to say without seeing the database architecture you are working with.

    Depending on the circumstance I would most likely use two tables.

    One table would store the root level category sections.

    The other table would store each category that would belong to one category section.

    So the proposed tables would probably take on a similar structure to the following.

    category_sections
    -- name (primary key)

    categories
    -- name (primary key)
    -- section_name (foreign key to category_sections references name)

    Is that similar to what you have in regards to the architecture?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    This is a snippet of code used to build a select menu with a option group in one of my apps.

    The idea is basically same. Instead here there are sections (root) and topics. A Section has many topics and a topic belongs to one section.

    PHP Code:
                <?php $f->current('topic');if($f->name(false)) { ?>
                <li>
                    <label for="thread-topic"><?php echo $f->required()?'<span class="required">Forum:</span>':'Forum:'?></label>
                    <select name="<?php $f->name(); ?>" id="thread-topic" class="<?php echo $f->required()?'required ':'';echo $f->invalid()?'invalid':''?>"<?php echo $f->disabled()?' disabled="disabled"':''?>>
                    <?php foreach($sections as $section) { ?>
                        <optgroup label="<?php echo $section->name?>">
                        <?php
                            
    if($section->topics) {
                            
                                foreach(
    $section->topics as $topic) {
                                
                                    
    $value = (int) $f->value(false)?$f->value(false):'0'?>
                                    <option value="<?php echo $topic->id?>"<?php echo $value==$topic->id?' selected="selected"':''?>><?php echo ucwords($topic->name); ?></option>
                                <?php }
                            
                            }
                        
    ?>
                        </optgroup>
                    <?php ?>
                    </select>
                    <?php echo $f->feedback(false)?'<p>'.$f->feedback(false).'</p>':''?>
                </li>        
                <?php ?>
    It pretty much comes down to a nested loop on the display side of things.

    PHP Code:
                    <?php foreach($sections as $section) { ?>
                        <optgroup label="<?php echo $section->name?>">
                        <?php
                            
    if($section->topics) {
                            
                                foreach(
    $section->topics as $topic) {
                                
                                    
    $value = (int) $f->value(false)?$f->value(false):'0'?>
                                    <option value="<?php echo $topic->id?>"<?php echo $value==$topic->id?' selected="selected"':''?>><?php echo ucwords($topic->name); ?></option>
                                <?php }
                            
                            }
                        
    ?>
                        </optgroup>
                    <?php ?>

  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Location
    Brookfield, MO
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thanks for the help. Greatly appreciated!
    jocoder


Bookmarks

Posting Permissions

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