crossfuse Posted March 30, 2009 Share Posted March 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151846-for-while-what/ Share on other sites More sharing options...
crossfuse Posted March 30, 2009 Author Share Posted March 30, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/151846-for-while-what/#findComment-797381 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 not to be mean, but you shouldn't be asking here for help on your homework Quote Link to comment https://forums.phpfreaks.com/topic/151846-for-while-what/#findComment-797385 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151846-for-while-what/#findComment-797389 Share on other sites More sharing options...
Maq Posted March 31, 2009 Share Posted March 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151846-for-while-what/#findComment-797404 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.