Jump to content

help!!


puglover

Recommended Posts

im a first time poster so i apologize if i do it wrong!! I am trying to make a multiplication table with php and it won't work. any suggestions will be greatly appreciated!!

 

<?php

$rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink");

 

?>

<html>

<head>

<title>

</title>

</head>

<body>

Create a multiplication table<p>

<form method="get" action="<?php $_SERVER['php_self'];?> ">

Enter number of rows<br />

<input type="text" name="rownum" size="10" value="<?php echo $_GET['rownum'];?>"><p>

Enter number of columns<br />

<input type="text" name="colnum" size="10" value="<?php echo $_GET['colnum'];?>"><p>

Select a color<br />

<select name="color">

<?php

for ( $i = 0; $i < count( $rowcolors ); $i++ )

{

echo "<option value=\"" . $i . "\">" . $rowcolors[$i] . "</option>\n";

 

}

?>

<input type="hidden" name="do_php" value="true">

<p><input type="submit" value="Create Table">

</form>

<?php

 

if( isset( $_GET['do_php'] ) )

{

echo "<table width=\"50%\" border=\"3\">\n";

echo "<tr><td width='$cellwidth'>&nbsp</td>";

for ( $j = 1; $j <= $_GET['colnum']; $j++ )

    {

        echo "<th>" .$j . "</th>\n";

}

for ($i = 1; $i <= $_GET['rownum']; $i++ )

echo "<tr>";

echo "<td>".$i."</td>\n";

}

echo "</tr>";

    for ( $j = 1; $j <= $_GET['colnum']; $j++ )

    {

        echo "<td>".$i * $j."</td>\n";

    }

 

 

}

 

echo "</table>";

?>

 

 

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/176732-help/
Share on other sites

Here is something to put you in the right direction.

 

First thing you always want to do (especially when it's a display issue) is validate your HTML/CSS.

<?php
$rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink");
var_dump($_GET);
?>
<html>
    <head>
        <title>
        </title>
    </head>
    <body>
        Create a multiplication table<p>
        <form method="get" action="multiply.php" >
            Enter number of rows<br />
            <input type="text" name="rownum" size="10"  /><br/>
            Enter number of columns<br />
            <input type="text" name="colnum" size="10"  /><br/>
            Select a color<br />

            <select name="color">
                <?php
                for ( $i = 0; $i < count( $rowcolors ); $i++ ) {
                    echo "<option value=\"" . $i . "\">" . $rowcolors[$i] . "</option>\n";

                }
                ?>
            </select>

            <?php

            if(!empty($_GET)) {
                echo "<table border='1'>";

                $i=1;
                while($i<=$_GET['colnum']) {
                    echo "<tr>";
                    $c=1;
                    echo "<td>".$i."</td>";
                    while($c<=$_GET['rownum']) {
                        echo "<td>".$c * $i."</td>";
                        $c++;
                    }
                    $i++;
                    echo "</tr>";
                }


                echo "</table>";

            }

            ?>

            <input type="hidden" name="do_php" value="true">
            <input type="submit" value="Create Table">
        </form>


    </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/176732-help/#findComment-931868
Share on other sites

Not a display issue... a logic issue with the for loops.

 

I hope this isn't a homework assignment that I just did for you ;)

 

<?php
$rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink");
?>
<html>
<head>
<title>
</title>
</head>
<body>
Create a multiplication table<p>
<form method="get" action="">
Enter number of rows<br />
<input type="text" name="rownum" size="10" value="<?php echo $_GET['rownum'];?>"><p>
Enter number of columns<br />
<input type="text" name="colnum" size="10" value="<?php echo $_GET['colnum'];?>"><p>
Select a color<br />
<select name="color">
<?php
for ( $i = 0; $i < count( $rowcolors ); $i++ )
{
   echo "<option value=\"" . $i . "\">" . $rowcolors[$i] . "</option>\n";

}
?>
<input type="hidden" name="do_php" value="true" />
<p><input type="submit" value="Create Table" /></p>
</form>
<?php

if( isset( $_GET['do_php'] ) )
{
   //Create table
   echo "<table width=\"50%\" border=\"3\">\n";
   
   //start first row with a blank space
   echo "<tr><td width='$cellwidth'>&nbsp</td>";
   
   //finish first row
   for($i=1; $i<=$_GET['colnum']; $i++) {
   echo "<td>".$i."</td>\n";
   }
   
   //Close first row
   echo "</tr>\n";
   
   //Start second row
   for($j=1; $j<=$_GET['rownum']; $j++) {
   echo "<tr>";
   
   //Add 1st column values
   echo "<td>".$j."</td>\n";
   
   //Add multiplied values
   for($k=1; $k<=$_GET['colnum']; $k++) {
		echo "<td>".$j * $k."</td>\n";
   }
   
}

//Close the row
   echo "</tr>\n";
   
}

//Close the table
echo "</table>\n";

?>


</body>
</html>

 

I at least left the row color portion for you to figure out

Link to comment
https://forums.phpfreaks.com/topic/176732-help/#findComment-931910
Share on other sites

oh derr perhaps posting my current code would work..lol...ill just post the table portion.

 

<?php

if( isset( $_GET['do_php'] ) )
{
echo "<table width=\"50%\" border=\"3\">\n";
    echo "<tr><td>&nbsp</td></tr>";

for ($i = 1; $i <= $_GET['rownum']; $i++ ) 
{
	echo "<tr>";
	echo "<td>".$i."</td>";

	for ( $j = 1; $j <= $_GET['colnum']; $j++ ) 
	{
		echo "<td>".$i * $j."</td>";
	}

	echo "</tr>";
}

echo "</table>";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/176732-help/#findComment-933867
Share on other sites

How about this?

<style>
.nums
{
   width:5%;
   float:left;  
}
.head
{
   background-color:#CCC;  
}
</style>
<?php
   for ($i=0;$i<11;$i++)
      echo "<div class=\"nums head\">$i</div>";
   echo "<br />";
   for ($i=1;$i<11;$i++)
   {
      echo "<div class=\"nums head\">$i</div>";
      for ($j=1;$j<11;$j++)
         echo "<div class=\"nums\">" . $i*$j . "</div>";
      echo "<br />\n";
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/176732-help/#findComment-933868
Share on other sites

Here, I added comments. The <style> part is just to make the divs display in the right format; that isn't php, just HTML.

 

<style>
.nums
{
   width:5%;
   float:left;  
}
.head
{
   background-color:#CCC;  
}
</style>
<?php
   //Draw ten divs with numbers 0 through 11 with the header style for the top
   for ($i=0;$i<11;$i++)
      echo "<div class=\"nums head\">$i</div>";
      
   //make a new line
   echo "<br />";
   
   //for numbers 1 through 10...
   for ($i=1;$i<11;$i++)
   {
      //Echo those numbers for the left header column
      echo "<div class=\"nums head\">$i</div>";
      //For numbers 1 through 10...
      for ($j=1;$j<11;$j++)
         //Multiply the two numbers together and display them
         echo "<div class=\"nums\">" . $i*$j . "</div>";
      //new line
      echo "<br />\n";
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/176732-help/#findComment-933871
Share on other sites

How about if I do it without HTML formatting:

<?php
   //"\t" makes a tab and "\n" makes a new line
   //Draw the top row
   //This is the same as just echoing "1\t2\t3\t4\t5\t6\t7\t8\t9\t10";
   for ($i=0;$i<11;$i++)
      echo $i."\t";
      
   //Go to the next line
   echo "\n";
   
   //for numbers 1 through 10...
   for ($i=1;$i<11;$i++)
   {
      //Echo those numbers for the left header column
      echo $i."\t";
      //For numbers 1 through 10...
      for ($j=1;$j<11;$j++)
         //Multiply the two numbers together and display them
         echo $i*$j."\t";
      //new line
      echo "\n";
   }
?>

 

You can just view source to see it formatted as a table.

 

Here is the code without the comments if it is easier to read:

<?php
   for ($i=0;$i<11;$i++)
      echo $i."\t";
   echo "\n";

   for ($i=1;$i<11;$i++)
   {
      echo $i."\t";
      for ($j=1;$j<11;$j++)
         echo $i*$j."\t";
      echo "\n";
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/176732-help/#findComment-933878
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.