0

I am trying to write code to be able to expand and contract rows and columns from a multiplication table in PHP. This is the code that I currently have that I would think would be able to expand and contract the rows and columns:

<?php
$row=4;
$col=4;
if(isset($_GET["row"]))
{
    $row=$_GET["row"];
}
if(isset($_GET["col"]))
{
    $col=$_GET["col"];
}
?>
<html style="background-color: #90A7E8;">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <html lang="en">
<head>
<body>
<ul>
<?php
echo "<table>";
for($r=1;$r<$row+1;$r++){
    echo "<tr>";
    for($c=1;$c<$col+1;$c++){
        $p=$c*$r;
        //~ echo "<td>";
        echo "<td>$p</td>";
        //~ echo "</td>";
        }
    echo "</tr>";
}
echo "</table><br>";
$row++;
echo "</ul><a href='math.php?row=$row?col=$col'>more rows</a><br>";
$row-=1;
echo "<a href='math.php?row=$row?col=$col'>less rows</a><br>";
$col++;
echo "<a href='math.php?row=$row?col=$col'>more columns</a><br>";
$col-=1;
echo "<a href='math.php?row=$row?col=$col'>less columns</a><br>";
?>
</body>
</html>

When I run the code, I keep getting this error as well as the inability to expand and contract the columns. I get a non well formed numeric value error on this line of code: for($r=1;$r<$row+1;$r++)

When I attempt to expand and contract the columns, it only contracts the rows.

To summarize, I have a multiplication table I would like to expand and contract the rows and columns using only PHP. When I run the code specified above I get an error saying "A non well formed numeric value encountered" on the line specified above, as well as the inability to expand or contract the columns. Can anyone help me out here?

1 Answer 1

1

Starting with your non well formed numeric value: values in $_GET are all strings. You need to convert these strings to integers.

$row=(int)$_GET["row"];

In your links the query strings are not formed properly. The query string starts with a question mark, with key/value pairs separated by ampersand (&). So you get

echo "</ul><a href='math.php?row=$row&col=$col'>more rows</a><br>";

Lastly, your code that calculates the new row and column counts will only work for increasing those values because your calculation for decreasing them simply reverses the increase applied for the previous line.

Starting with $row set to 4:

$row++;    // $row = 5
echo ...   // More rows
$row -=1   // $row is now 4
echo ...   // Fewer rows.

There are many ways to deal with this. One way is to use a new variable for the new row and column counts.

Starting with $row set to 4:

$newRow = $row+1;    // $newRow = 5
echo ...             // More rows
$newRow = $row-1     // $newRow is now 3
echo ...             // Fewer rows.

Putting it all together gives:

<?php

$row=4;
$col=4;
if(isset($_GET["row"]))
{
    $row=(int)$_GET["row"];
}
if(isset($_GET["col"]))
{
    $col=(int)$_GET["col"];
}
?>
<html style="background-color: #90A7E8;">
<meta name="viewport" content="width=device-width, initial-scale=1">
<html lang="en">
<head>
<body>
<ul>
    <?php
    echo "<table>";
    for($r=1;$r<$row+1;$r++){
        echo "<tr>";
        for($c=1;$c<$col+1;$c++){
            $p=$c*$r;
            //~ echo "<td>";
            echo "<td>$p</td>";
            //~ echo "</td>";
        }
        echo "</tr>";
    }
    echo "</table><br>";
    $newRow = $row+1;
    echo "</ul><a href='math.php?row=$newRow&col=$col'>more rows</a><br>";
    $newRow = $row-1;
    echo "<a href='math.php?row=$newRow&col=$col'>less rows</a><br>";
    $newCol = $col+1;
    echo "<a href='math.php?row=$row&col=$newCol'>more columns</a><br>";
    $newCol = $col-1;
    echo "<a href='math.php?row=$row&col=$newCol'>less columns</a><br>";
    ?>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.