Jump to content


Photo

php code help (Multiplication Table)


  • Please log in to reply
1 reply to this topic

#1 isuckat_php

isuckat_php

    Member

  • Members
  • PipPip
  • 11 posts

Posted 23 April 2010 - 07:22 PM

Help is greatly appreciated.

I am generating a multiplication table that runs numbers 2-15 depending on which number the user chooses to input.

The problem is when i input a number it infinetly multiplies the number by 1.

Here is the code:

<?php
// time_table.php - a demo of using functions and include file script
// Get the functions we need for this time table
// Using extension "php" instead of "inc" will prevent others see the code

include_once("tt_functions.php");

# MAIN-PROGRAM
$title = "The Times-Tables";
html_begin ($title, $title);

#Validation of input and then produce the Times-Tables
#
if ($submit)
{
if ($n == ''){
?>
<span style="color:red;"><b>Oops, your forgot to supply info required.
Please correct the problem and try again. </b></span><br/>
<?
}
else if ((!ereg("[0-9]", $n))){
?>
<span style="color:red;">
<b>Please restrict your input to only numbers.</b></span><br/>
<?
}
else if (($n >= 15 ) || ($n < 2)) {
echo "Number Range Error.";
}
else{

//ready to produce the time tables
//function timetable( ) was defined in the included file function_timetable.php

timetable($n);
}
}

// Forms to collect user input
?>
<form method="post" action="time_table.php">
<p><b>Please enter an interger between 2 and 15:</b></P>
<input type='text' name='n' />
<p><input type="submit" name="submit" value="Time Tables" /></P>
</form>

<? html_end () ?>

Here is the tt_functions:

<?php

# A collection of functions to make our time table engineering
# easy to understand. It will be included in time_table.php

function html_begin ($title, $header)
{
print ("<HTML>\n");
print ("<HEAD>\n");
if ($title)
print ("<TITLE>$title</TITLE>\n");
print ("</HEAD>\n");
print ("<BODY bgcolor=\"#FFFF9C\">\n");
if ($header)
print ("<H2>$header</H2>\n");
}

# put out final HTML tags for page.

function html_end ()
{
print ("</BODY></HTML>\n");
}

function timetable ($n) {
// Go through each table
for ($table=1; $table<=$n; $table++)
{
  print "<p><b>The ".$table ."Times Table</b>\n";
//Produce $n lines for each table
  for($counter=1; $counter=$n; $counter++)
{
  $answer = $table * $counter;

//Is this an even-number counter?
  if ($counter % 2 == 0)
//Yes, so print this line in bold
  print "<br><b>$counter x $table = ".
          "$answer</b>";

  else
//No, so print this in normal face
    print "<br>$counter x $table = $answer";
  }
}
}?>

Again help is much appreciated. Thank you for taking the time to read this.


#2 andrewgauger

andrewgauger

    Advanced Member

  • Members
  • PipPipPip
  • 603 posts
  • LocationOregon

Posted 23 April 2010 - 09:21 PM

  for($counter=1; $counter=$n; $counter++)

why are you evaluating if $counter=$n?  will it ever =$n when you are assigning the value to it?  oh, it always will thats right.  If you want less than or equal to you should change it to:

   for($counter=1; $counter<=$n; $counter++)

but I'm usually wrong. :shy:
PHP 5.3.2 | Apache 2.2.14 | MySQL 5.1.41 | Ubuntu 10.04
Developers of the modern age get no respect.  We are always the blunt of jokes, the first to blame, and people hardly talk to us.  I wouldn't trade it for the world.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com