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 8647805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:09:41+00:00 2026-06-12T13:09:41+00:00

I liked the function contributed here: PHP – PRE-select drop down option function generateSelect($name,

  • 0

I liked the function contributed here:
PHP – PRE-select drop down option

function generateSelect($name, $options, $optionToSelect) {
    $html = '<select name="'.$name.'">';
    foreach ($options as $option => $value) {
        if($value == $optionToSelect)
            $html .= '<option value="'.$value.'" selected="selected">'.$value.'</option>';
        else
            $html .= '<option value="'.$value.'">'.$value.'</option>';
    }
    $html .= '</select>';
    return $html;
}

/* And then call it like */
$html = generateSelect('company', $companies, 'Apple');

However, this doesn’t address a description that is needed sometimes with drop-down menu.

For example:

<select name="ranges">
<option value="0">All Ranges</option>
<option value="1">Under $10,000</option>
<option value="2">$10,000 - $25,000</option>
<option value="3">$25,000 - $50,000</option>
<option value="4">$50,000 - $75,000</option>
<option value="5">$75,000 - $100,000</option>
<option value="6">$100,000 - $200,000</option>
<option value="7">$200,000 or more</option>
</select>

What needs to be modified for the generateSelect function to allow it to assign, for example, along with a value of “4”, but a description of “$75,000 – $100,000” to accompany it? The way the generateSelect function is now, it would assign a value of “4” and place in the description (for lack of a better term) also a “4”.

Or is there a better way to do this in PHP? Thanks!

  • 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-06-12T13:09:44+00:00Added an answer on June 12, 2026 at 1:09 pm

    I am not sure what you mean by description (perhaps you could clarify; do you want an additional attribute that contains the option key? I am unregistered so I cannot ask except as an answer)

    If you want for example to show the “description” as part of the value attribute, you would simply append it to the value attribute like so:

    $html .= '<option value="'.$value.' ('.$option.')" selected="selected">'.$option.'</option>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
This code adds the text liked inside div.gdt-starrating : $(a.gdt-starrating).live(click, function() { $(div.gdt-starrating).text(liked); });
FB.Event.subscribe('edge.create', function(response) { alert ('liked!') } ); How do I check the reverse action?
I'm trying to use my own function (never liked $(document).ready() ) to dynamically load
I have a function call SwapOnscreenPhotos. Here is the source for that function: function
When we have code like: function a(){ var x =0; this.add=function(){ alert(x++); } }
I have code like function populate...() { var user = $.storage.get(particulars); if (user) {
if I have methods like function getVar1(){ return $this->var1; } function getVar2(){ return $this->var2;
I'd like to be able to create variables using a function, something like: function
I am using the REGEXP_LIKE function in Oracle 10g to find values in a

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.