SF23103 Posted September 23, 2015 Share Posted September 23, 2015 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> Quote Link to comment https://forums.phpfreaks.com/topic/298281-number-of-html-select-fields-based-on-mysql-response/ Share on other sites More sharing options...
benanamen Posted September 23, 2015 Share Posted September 23, 2015 $number_value=6; foreach(range('1', $number_value) as $number) { echo "<option value=\"$number\">$number</option>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/298281-number-of-html-select-fields-based-on-mysql-response/#findComment-1521373 Share on other sites More sharing options...
Solution mac_gyver Posted September 23, 2015 Solution Share Posted September 23, 2015 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"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/298281-number-of-html-select-fields-based-on-mysql-response/#findComment-1521374 Share on other sites More sharing options...
SF23103 Posted September 23, 2015 Author Share Posted September 23, 2015 Great responses, thank you. Still learning lots, and that helps tons! Quote Link to comment https://forums.phpfreaks.com/topic/298281-number-of-html-select-fields-based-on-mysql-response/#findComment-1521375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.