Jump to content

loops


bscotyb

Recommended Posts

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

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

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

  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... :P

Link to comment
https://forums.phpfreaks.com/topic/260998-loops/#findComment-1337667
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.