Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6093161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:31:43+00:00 2026-05-23T12:31:43+00:00

Mid afternoon mental block. Simple PHP script to generate an HTML snippet. Will appreciate

  • 0

Mid afternoon mental block. Simple PHP script to generate an HTML snippet. Will appreciate a second pair of eyes to spot what I’m doing wrong …

<?php $prevname = ''; ?>
<?php foreach($subcategories as $sub_category): ?>
    <?php $newname = $sub_category->getCategory()->getName(); ?>
    <?php $group_changed = strcmp($newname, $prevname); ?>
    <?php if ($group_changed): ?>
    <optgroup label="<?php echo $newname; ?>">
    <?php endif; ?>
        <option value="<?php echo $sub_category->getId(); ?>"><?php echo $sub_category; ?></option>
    <?php if ($group_changed): ?>
    </optgroup>
    <?php endif; ?>
<?php endforeach; ?>

[Edit]

Generated HTML looks like this:

<select>
    <optgroup label="Group1">
            <option value="1">Group1 Sub Item 1</option>
    </optgroup>
    <optgroup label="Group1">
            <option value="2">Group1 sub Item 2</option>
    </optgroup>
    <!-- and so on ...
</select>

instead of what I expected:

<select>
    <optgroup label="Group1">
            <option value="1">Group1 Sub Item 1</option>
            <option value="2">Group1 sub Item 2</option>
    </optgroup>
    <optgroup label="Group2">
            <option value="1">Group2 Sub Item 1</option>
            <option value="2">Group2 sub Item 2</option>
    </optgroup>
    <!-- and so on ...
</select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:31:43+00:00Added an answer on May 23, 2026 at 12:31 pm
    <?php $group_changed = (strcmp($newname, $prevname) != 0) ; ?>
    

    And you are not assigning $prevname to the $newname.

    Additionally, the optgroup should be closed not in the same iteration, but on next optgroup name change. The code should look something like this:

    <?php $prevname = ''; $group_opened = false; ?>
    <?php foreach($subcategories as $sub_category): ?>
        <?php $newname = $sub_category->getCategory()->getName(); ?>
        <?php $group_changed = (strcmp($newname, $prevname)!=0) ; ?>
        <?php if ($group_changed): $prevname = $newname;?>
               <?php if ($group_opened):?>
                 </optgroup>
               <?endif; ?>
        <optgroup label="<?php echo $newname; ?>">
         <?php $group_opened = true; ?>
        <?php endif; ?>
            <option value="<?php echo $sub_category->getId() ?>"><?php echo $sub_category; ?></option>
    <?php endforeach; ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application : I am working on one mid-large size application which will be used
I'm beginner - mid range when it comes to php knowledge and complete beginner
From here: http://heanet.dl.sourceforge.net/project/teamlab/TeamLabinstall_EN.pdf For a mid-size portal it will be sufficient to deploy it
I have a simple pub-sub setup on a mid-sized network, using ZMQ 2.1. Although
What is a good simple problem to throw at Jr. and Mid level developers
For a university mid-term project I have to design a configurable processor, to write
I work in a mid size company as a intermediate developer and work on
I recently completed development of a mid-traficked(?) website (peak 60k hits/hour), however, the site
When I hire developers for general mid-to-senior web app development positions, I generally expect
According to the documentation in VB6 the Mid() function returns a variant, but Mid$()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.