简体   繁体   中英

how to take multiple input and print output in php

    <body>
    <div class="input-div">
    <form action="" method="get">
        <input type="number" name="number[]" placeholder="Enter number" id="num">
        <input type="number"name="number[]" placeholder="Enter table length" id="num2">
        <input type="submit" id="button" value="Submit">
    </form>
    </div>

    <div class="output-div">
    <table>
    <?php
if (isset($_GET['number[]'])):
    $num = count($_GET['number[]']);
    for ($i = 0; $i <= $num; $i++):
        $var = $_GET[0];
        $max = $_GET[1];
    endfor;
    getTable($var, $max);
endif;

function getTable($var, $max)
{
    for ($i = 1; $i <= $max; $i++) {
        if (($i % 2) == 0):
            return isEven($var, $i);
        else:
            return isODD($var, $i);
        endif;
    }
}
function isEven($var, $i)
{
    echo "<tr style= \" background-color: grey;\">";
    echo "<td>" . $var . "</td>";
    echo "<td> * </td>";
    echo "<td>" . $i . "</td>";
    echo "<td> = </td>";
    echo "<td>" . $var * $i . "</td>";
    echo "</tr>";

}
function isOdd($var, $i)
{
    echo "<tr>";
    echo "<td>" . $var . "</td>";
    echo "<td> * </td>";
    echo "<td>" . $i . "</td>";
    echo "<td> = </td>";
    echo "<td>" . $var * $i . "</td>";
    echo "</tr>";
}

?>
    </table>
    </div>
</body>

please help how to print output. task is to take 2 numbers. 1st number will be whose table need to output. and 2nd number is for how long will be the table. i am trying to print output and not able to find the issue. what i am doing wrong? I just started php so do not know much. All help and hints are welcomed, thank you.

Simply you can get rid of two functions isEven and isOdd and just play with $style if even set to "background-color: grey;" , Otherwise leave it blank

<form action="" method="get">
        <input type="number" name="number[]" placeholder="Enter number" id="num">
        <input type="number"name="number[]" placeholder="Enter table length" id="num2">
        <input type="submit" id="button" value="Submit">
    </form>
if (isset($_GET['number'])):
        $var = $_GET['number'][0];
        $max = $_GET['number'][1];
        getTable($var, $max);
endif;
function getTable($var, $max)
{
    for ($i = 1; $i <= $max; $i++) {
        if (($i % 2) === 0)://Even
            $style = "background-color: grey;";
        else:
            $style = "";
        endif;
        echo "
            <tr style='$style'>
                <td>$var</td>
                <td>*</td>
                <td>$i</td>
                <td>=</td>
                <td>".$var*$i."</td>
            </tr>";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM