rwachowiak Posted November 19, 2010 Share Posted November 19, 2010 Lets say I have: $var=5; What would an output look like to get me: 1<br> 2<br> 3<br> 4<br> 5<br> Im at a complete loss! Thanks! Link to comment https://forums.phpfreaks.com/topic/219205-simple-question-i-think/ Share on other sites More sharing options...
tomfmason Posted November 19, 2010 Share Posted November 19, 2010 you are wanting the range function <?php $var = 5; foreach(range(1,$var) as $num) { echo "$num<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/219205-simple-question-i-think/#findComment-1136683 Share on other sites More sharing options...
salathe Posted November 19, 2010 Share Posted November 19, 2010 Alternatively, go back to basics with: for ($i = 1; $i <= 5; $i++) { echo "$i<br>\n"; } Link to comment https://forums.phpfreaks.com/topic/219205-simple-question-i-think/#findComment-1136703 Share on other sites More sharing options...
Chris92 Posted November 19, 2010 Share Posted November 19, 2010 Just to throw even more anoyingness to this thread: $var = 5; $i = 1; while( $i <= $var ) { echo $i . "<br />\n"; $i++; } Link to comment https://forums.phpfreaks.com/topic/219205-simple-question-i-think/#findComment-1136705 Share on other sites More sharing options...
rwachowiak Posted November 19, 2010 Author Share Posted November 19, 2010 Thanks for the input... what im really looking for is this: I have a query that pulls a number from a database... the number will change based on the WHERE clause... and with each different number, i need an output that adds a form input, 1 for each number... so if the variable = 5, there needs to be 5 inputs in the form, if the variable is 3, then 3 inputs in the form... and so on. so would i have something like: <form> <?php $var = $Training_Slots; foreach(range(1,$var) as $num) { echo "<input id=\"Slot$num\"><br />"; } my code is way off, but you get the idea? ?> Link to comment https://forums.phpfreaks.com/topic/219205-simple-question-i-think/#findComment-1136717 Share on other sites More sharing options...
Chris92 Posted November 19, 2010 Share Posted November 19, 2010 That should work.. Link to comment https://forums.phpfreaks.com/topic/219205-simple-question-i-think/#findComment-1136719 Share on other sites More sharing options...
rwachowiak Posted November 19, 2010 Author Share Posted November 19, 2010 yup got it! thanks guys! <form id="myForm" method="post"> <? $username="??"; $password="??"; $database="??"; $id = $_GET['id']; mysql_connect('hostname',$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM Players WHERE Mugshot='$id'"; $result=mysql_query($query); $var = $Training_Slots; foreach(range(1,$var) as $num) { echo "Training Slot $num: <select name=\"Slot$num\" id=\"Slot$num\"> <option value=\"\">Choose Training</option> <option value=\"5\">5 SKT</option> <option value=\"7\">7 SKT</option> <option value=\"9\">9 SKT</option> </select><br> "; } ?> <input type="Submit"> </form> edit: removed hostname from mysql_connect Link to comment https://forums.phpfreaks.com/topic/219205-simple-question-i-think/#findComment-1136729 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.