bscotyb Posted April 15, 2012 Share Posted April 15, 2012 i am having issues with my class work. this needs to start at the $startNum and end with the $endNum and display results in the increments submitted by the html. I can not get it to print out correctly it is supposed to say: The square of $startNum is $square //until it hits the end number and is going up by the correct increments, It wont go up by any increments and only shows a list if i leave out the increment-----any suggestions??? please i have to keep it very simple since i am just learning-ty html </head> <body> <h1>Squares</h1> <p> <form action = "squares2Boles.php" method = "post" > <p>Start with: <input type = "text" size = "5" name = "startNum" /> </p><p>End with: <input type = "text" size = "5" name = "endNum" /> </p><p>Increment by: <input type = "text" size = "5" name = "increment" /> </p><p><input type = "submit" value = "Display the Squares" /></p> </form> </body> </html> my php: <html> <head> <title>Squares</title> <link rel ="stylesheet" type="text/css" href="sample.css" /> </head> <body> <?php $startNum = $_POST['startNum']; $endNum = $_POST['endNum']; $increment = $_POST['increment']; $square = $_POST['square']; print ("<h1>SQUARES</h1><hr />"); for ($startNum = $startNum; $startNum <= $endNum; $startNum = $startNum + $increment) { $square = $startNum * $startNum; print ("The square of $startNum is $square<br />"); } print ("<hr />"); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/260998-loops/ Share on other sites More sharing options...
marcus Posted April 15, 2012 Share Posted April 15, 2012 for($i = $startNum; $i <= $endNum; $i += $increment){ echo "The square of " . $i . " is " . ($i*$i) . "<br/>\n"; } // alternatively $range = range($startNum, $endNum, $increment); foreach($range AS $value){ echo "The square of " . $value . " is " . ($value*$value) . "<br/>\n"; } Link to comment https://forums.phpfreaks.com/topic/260998-loops/#findComment-1337653 Share on other sites More sharing options...
bscotyb Posted April 15, 2012 Author Share Posted April 15, 2012 that really helped me alot appreciate it, now i have one more question when i input in the html start with: 10 End with: 20 Increment by: 2 in the html it wont square 20 still give me square for 18 what did i do wrong now, <?php $startNum = $_POST['startNum']; $endNum = $_POST['endNum']; $increment = $_POST['increment']; $square = $_POST['square']; print ("<h1>SQUARES</h1><hr />"); for($startNum = $startNum; $startNum < $endNum; $startNum = $startNum += $increment) { $square = $startNum * $startNum; print("The square of " . $startNum . " is " . $square . "<br/>\n");} { print("The square of " . $startNum . " is " . $square . "<br/>\n");} print ("<hr />"); ?> Link to comment https://forums.phpfreaks.com/topic/260998-loops/#findComment-1337659 Share on other sites More sharing options...
kicken Posted April 15, 2012 Share Posted April 15, 2012 Quote ; $startNum < $endNum; When you get to the point that $startNum is 20, you have the condition 20 < 20, which is false so the loop exits. You need to use <= (less than or equal to) to include 20 in the loop. Link to comment https://forums.phpfreaks.com/topic/260998-loops/#findComment-1337665 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 Quote that really helped me alot appreciate it, now i have one more question when i input in the html start with: 10 End with: 20 Increment by: 2 in the html it wont square 20 still give me square for 18 what did i do wrong now, <?php $startNum = $_POST['startNum']; $endNum = $_POST['endNum']; $increment = $_POST['increment']; $square = $_POST['square']; print ("<h1>SQUARES</h1><hr />"); for($startNum = $startNum; $startNum < $endNum; $startNum = $startNum += $increment) { $square = $startNum * $startNum; print("The square of " . $startNum . " is " . $square . "<br/>\n");} { print("The square of " . $startNum . " is " . $square . "<br/>\n");} print ("<hr />"); ?> If you had some proper formatting on your code, it would have been pretty obvious for you, at least I guess so. <?php $startNum = $_POST['startNum']; $endNum = $_POST['endNum']; $increment = $_POST['increment']; $square = $_POST['square']; print ("<h1>SQUARES</h1><hr />"); for($startNum = $startNum; $startNum < $endNum; $startNum = $startNum += $increment) { $square = $startNum * $startNum; print("The square of " . $startNum . " is " . $square . "<br/>\n"); } { print("The square of " . $startNum . " is " . $square . "<br/>\n"); } print ("<hr />"); ?> I've done nothing but removed and added some blankspaces. $square = $startNum * $startNum; is last calculated in the for loop, but you do this print("The square of " . $startNum . " is " . $square . "<br/>\n"); after the loop. $startnum has increased by the $increment and is now too large for the for loop, but $square is still the same. When you now print it out, $startnum increment higher and $square the same. <?php if(isset($_POST['startNum']) && isset($_POST['endNum']) && isset($_POST['increment']) && isset($_POST['square'])){ $startNum = $_POST['startNum']; $endNum = $_POST['endNum']; $increment = $_POST['increment']; $square = $_POST['square']; echo '<h1>SQUARES</h1><hr />'; for($startNum = $startNum; $startNum <= $endNum; $startNum += $increment){ $square = $startNum * $startNum; echo 'The square of ' . $startNum . ' is ' . $square . "<br/>\n"; } } ?> I couldn't let your code look like it did... Link to comment https://forums.phpfreaks.com/topic/260998-loops/#findComment-1337667 Share on other sites More sharing options...
bscotyb Posted April 15, 2012 Author Share Posted April 15, 2012 i suck at this thanks Link to comment https://forums.phpfreaks.com/topic/260998-loops/#findComment-1337668 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.