Jump to content

number of html select fields based on MySQL response


SF23103
Go to solution Solved by mac_gyver,

Recommended Posts

Ok, here's my latest dilemma.  If anyone has any suggestions, I would love to hear them.  I am not looking for someone to code this for me, just a point in the right direction.

 

I have a value in my MySQL database that is a number between 1 and 50 ($number_value).  When the page loads, I want to use that value to create a dropdown list from 1 to $number_value.

 

I could create a script that checks for $number_value for each possible situation, but that would be a pretty long and probably not the most efficient.  Any other ideas?

 

So you know what I am talking about, this is just a text representation:

If $number_value is 5, echo:
<option value=“1">1</option>
<option value=“2">2</option>
<option value=“3">3</option>
<option value=“4">4</option>
<option value=“5">5</option>

If $number_value is 3, echo:
<option value=“1">1</option>
<option value=“2">2</option>
<option value=“3”>3</option>

If $number_value is 10, echo:
<option value=“1">1</option>
<option value=“2">2</option>
<option value=“3”>3</option>
<option value=“4">4</option>
<option value=“5">5</option>
<option value=“6”>6</option>
<option value=“7">7</option>
<option value=“8">8</option>
<option value=“9”>9</option>
<option value=“10”>10</option>
Link to comment
Share on other sites

  • Solution

this is the whole point of variables and control structures in programming. you can set a variable to any value, from wherever you want, and use control structures, such as a loop, to test/use the value that's in the variable to determine what to do.

foreach(range(1,$number_value) as $value){
    echo "<option value='$value'>$value</option>\n";
}
  • Like 1
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.