Jump to content

Recommended Posts

I was given this assignment in my PHP class.

 

"Write PHP scripts that: 1. Adds up the odd numbers beginning with 1 up to and including a positive odd number that the user inputs into an HTML form type text box. This Sum will be printed. 2. Makes and prints a table containing the above odd numbers in the first column and beside each number in the 2nd column, put the square of the number. There should be two files, an html form that allows the user to enter the max number in an text box. The second is a php file that receives the number and performs the above two tasks."

 

 

 

All i need to know is what to do in the html file, not the second part.

Link to comment
https://forums.phpfreaks.com/topic/151846-for-while-what/
Share on other sites

am i on the right track?

 

Html

<code>

<html>

<body>

<h2 align="center">Find the sum and square of a number</h1>

<form action="square.php" method="post">

<br>

 

Odd number: <input type="text" size="4" maxlength="7" name="num">

 

<br><input type="Submit" value="Submit">

<input type="Reset" value="Reset">

<br><br>

 

 

 

</form>

</body>

</html>

[/code]

 

PHP file

 

<html>
<body>
<?php

$num = $_POST["num"];


$number2 = (1) ;

$sum = ($num + $number2) ;



print("<br> The number plus 1 is $sum");



?>
<br><br>
<font size="4" color="blue"> Table of Square Values</font><br>
<table border= "1">
<th> Number</th><th>Sqr </th>
<?php
$end = $num
   
   while ($i <= $end)	{
	$sqr= $i * $i
	print ("<tr><td>$i</td><td>$sqr</td></tr>");
	$i = $i + 2;
}





?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/151846-for-while-what/#findComment-797381
Share on other sites

Eh, in my opinion, nothing is wrong with homework questions as long as the asker is trying, and as long as the OP says that it's a homework question.

 

Asking for help on your homework is perfectly fine in my book.  I agree with corbin, he's not going to learn anything and only be a detriment to the programming community, if he doesn't do things himself...

 

@crossfuse, you have your form correct and you grab the value correctly but you need to check if it's an odd number.  A good way to do this is with the modulus operator(%). 

 

Try to implement this piece of code into yours.  And if you don't know how it works just ask, and I'll give you a detailed explanation but basically it's called a Ternary Operator (essentially a 1 line if/else statement):

 

$total += ($x % 2) ? $x : 0;

 

$x is the number that you're looping and if it's odd then add it to the total.

 

Hope this helps.

 

Link to comment
https://forums.phpfreaks.com/topic/151846-for-while-what/#findComment-797404
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.