Jump to content

php code help (Multiplication Table)


isuckat_php

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/199557-php-code-help-multiplication-table/
Share on other sites

  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:

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.