helpmehelpmehelpme Posted December 19, 2011 Share Posted December 19, 2011 I have this script that asks the user their name and email address and age and stuff and it checks to see what their age is and outputs a message depending on what age bracket they belong to. I am trying to make a for loop that uses the value the user submitted for their age and output the range of numbers from the users age to 1000 by 3s, i know how the basic for loop works, but i don't know how to do one where you are using a variable for i. if that makes sense. Heres the code that i have. test1.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Feedback Form</title> </head> <body> <div><p>Please complete this form to submit your feedback:</p> <form action="test1.php" method="post"> <p>Name: <select name="title"> <option value="Mr.">Mr.</option> <option value="Mrs.">Mrs.</option> <option value="Ms.">Ms.</option> </select> <input type="text" name="name" size="20" /></p> <p>Email Address: <input type="text" name="email" size="20" /></p> <p>Age: <input type="text" name="age" size="20" /></p> <p>Response: This is... <input type="radio" name="response" value="excellent" /> excellent <input type="radio" name="response" value="okay" /> okay <input type="radio" name="response" value="boring" /> boring</p> <p>Comments: <textarea name="comments" rows="3" cols="30"></textarea></p> <input type="submit" name="submit" value="Send My Feedback" /> </form> </div> </body> </html> test1.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Your Feedback</title> </head> <body> <?php include('test1.html'); $name = $_POST['name']; $age = $_POST['age']; $okay = TRUE; if(empty($name)){ echo '<p class="error"> Please enter your name.</p>'; $okay = FALSE; } if (empty($age)){ echo '<p class="error"> Please enter you age.</p>'; $okay = FALSE; } if ($age < 65){ echo '<p>no retirement for you</p>'; } if ($age == 65){ echo '<p> Yay, you can retire</p>'; } if ($age > 65 || $age < 100){ echo '<p>Florida is for me!</p>'; } if ($age > 100){ echo '<p>Good Genes</p>'; } //print users age to 1000 by 3s for($i=$age; $<1000 i+3){ echo '$age'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/ Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 Exactly the way you did it, plus a semicolon: //print users age to 1000 by 3s for($i=$age; $<1000; i+3){ echo '$age'; } Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299160 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 right, but i keep getting error messages $age Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\xampp\PHP\test1.php on line 38 $age Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\xampp\PHP\test1.php on line 38 $age Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\xampp\PHP\test1.php on line 38 $age Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\xampp\PHP\test1.php on line 38 $age Notice: Use of undefined constant i - assumed 'i' in C:\xampp\htdocs\xampp\PHP\test1.php on line 38 $age Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299171 Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 Oh, I see now you missed a few other things.... //print users age to 1000 by 3s for($i=$age; $i<1000; $i+3){ echo '$age'; } Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299175 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 okay, now it prints out $age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age$age infinitely Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299178 Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 echo $age; Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299180 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 now it prints out the users age, but it won't increment by 3s Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299184 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 heres the updated code test1.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Feedback Form</title> </head> <body> <div><p>Please complete this form to submit your feedback:</p> <form action="test1.php" method="post"> <p>Name: <select name="title"> <option value="Mr.">Mr.</option> <option value="Mrs.">Mrs.</option> <option value="Ms.">Ms.</option> </select> <input type="text" name="name" size="20" /></p> <p>Email Address: <input type="text" name="email" size="20" /></p> <p>Age: <input type="text" name="age" size="20" /></p> <p>Response: This is... <input type="radio" name="response" value="excellent" /> excellent <input type="radio" name="response" value="okay" /> okay <input type="radio" name="response" value="boring" /> boring</p> <p>Comments: <textarea name="comments" rows="3" cols="30"></textarea></p> <input type="submit" name="submit" value="Send My Feedback" /> </form> </div> </body> </html> test1.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Your Feedback</title> </head> <body> <?php include('test1.html'); $name = $_POST['name']; $age = $_POST['age']; $okay = TRUE; $i = $_POST['age']; if(empty($name)){ echo '<p class="error"> Please enter your name.</p>'; $okay = FALSE; } if (empty($age)){ echo '<p class="error"> Please enter you age.</p>'; $okay = FALSE; } if ($age < 65){ echo '<p>no retirement for you</p>'; } if ($age == 65){ echo '<p> Yay, you can retire</p>'; } if ($age > 65 && $age < 100){ echo '<p>Florida is for me!</p>'; } if ($age > 100){ echo '<p>Good Genes</p>'; } //print users age to 1000 by 3s for($i=$age; $i<1000;$i+3){ echo $age; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299186 Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 Damn, I need coffee or something. Here, this should do it: //print users age to 1000 by 3s for($i=$age; $i<1000; $i = $i + 3){ echo $i . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299189 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 awesome...thanks Quote Link to comment https://forums.phpfreaks.com/topic/253455-for-loop/#findComment-1299194 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.