Set amount of dropdown options, via DB - how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Set amount of dropdown options, via DB - how?

Post by simonmlewis »

I want to have a dropdown <option> of between 1 and 100.

In a database, there will be the number set .... such as "35".

Then I want there to be:

Code: Select all

<select name='list'>
<option value='1'>1<option>
...
<option value='35'>35<option>
Or if it is 50, then it goes only to '50'.

How do I do that? Is it best with a "FOR"? I'm a bit weak when it comes to FOR LOOPS. Any help would be appreciated.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Set amount of dropdown options, via DB - how?

Post by Celauran »

for loop would be the way I'd go about it. Assuming you've stored the value from the DB in $count, then

Code: Select all

for ($i = 1; $i <= $count; $i++)
{
    echo "<option value=\"{$i}\">{$i}</option>";
}
Post Reply