Home > OS >  PHP Times Table creation
PHP Times Table creation

Time:02-04

Hi I'm trying to figure out some code in PHP, I've been given the task to make a basic times table input, I have that part figured out. I have also to reverse the order the table is being output. So instead of the example running from 1 x 10=10 down I have to reverse that order from 10 x 10=100 down instead. This part I can't figure out.output

if (isset($_POST['calculate'])) {

$start = $_POST['startNumber'];
$end = $_POST['endNumber'];
$times = $_POST['times'];

for($start; $start <= $end; $start  ) {

    $answer = $start * $times;
    echo $start, " x ", $times, " = ", $answer, "<BR>";
    }
}

CodePudding user response:

You could tweak your original code, but rather than output it straight away add it to the current output at the start. The output the result at the end

if (isset($_POST['calculate'])) {

    $start = $_POST['startNumber'];
    $end = $_POST['endNumber'];
    $times = $_POST['times'];

    $output = '';
    for($start; $start <= $end; $start  ) {
        $answer = $start * $times;
        $output =  $start . " x " . $times ." = " . $answer . "<BR>" . $output;
    }
    
    echo $output;
}

CodePudding user response:

The easiest way I can think of is to create an array with range starting with the end value. With that, you can simply iterate over the numbers and do your calculations.

Demo, http://sandbox.onlinephpfunctions.com/code/727cb873fa8d5567157bfcd03f4bde5f4a574a4d

<?php

$start = 1;
$end = 10;
$multiplier = 10;

$reversed = range($end, $start);

foreach ($reversed as $input) {
    echo sprintf('%s x %s = %s %s', $input, $multiplier, $input * $multiplier, "\n");
}

CodePudding user response:

You can just push to an array instead, reverse the array after the loop, join the array into a string, then echo the output.

Also be careful with user submitting input. In this case I believe you're expecting ints so I typecasted them for you.

if (isset($_POST['calculate'])) {

    $start = (int)$_POST['startNumber'];
    $end = (int)$_POST['endNumber'];
    $times = (int)$_POST['times'];

    $out = [];
    for(; $start <= $end; $start  ) {
        $answer = $start * $times;
        $out[] =  "$start x $times = $answer<br>";
    }

    $out = array_reverse($out);
    echo implode('',$out);
}

Alternatively, you can just walk the loop in reverse:

if (isset($_POST['calculate'])) {

    $start = (int)$_POST['startNumber'];
    $end = (int)$_POST['endNumber'];
    $times = (int)$_POST['times'];

    for(; $end >= $start; $end--) {
        $answer = $end * $times;
        echo "$end x $times = $answer<br>";
    }
}

A cleaner looking loop would probably involve range:

if (isset($_POST['calculate'])) {

    $start = (int)$_POST['startNumber'];
    $end = (int)$_POST['endNumber'];
    $times = (int)$_POST['times'];

    foreach(range($end, $start) as $i) {
        echo "$i x $times = ". ($i * $times) ."<br>\n";
    }
}

You can also add in array_map if you'd like:

if (isset($_POST['calculate'])) {

    $start = (int)$_POST['startNumber'];
    $end = (int)$_POST['endNumber'];
    $times = (int)$_POST['times'];

    array_map(function($i) use ($times){
        echo "$i x $times = ". ($i * $times) ."<br>\n";
    }, range($end, $start));
}

Or you can just avoid all of this and just reverse your $start and $end parameters.

So instead of this:

$start = $_POST['startNumber'];
$end = $_POST['endNumber'];

Use this:

$end = $_POST['startNumber'];
$start = $_POST['endNumber'];

But kindly also handle user submitted data with care, otherwise echoing it directly allows them to inject HTML onto the page and do things like run JavaScript. Best to typecast it to int or float in your case as the code expects it to be such. Putting (int) in front will typecast it. You could also use (float) or (double) etc. You can also do stuff like running it through a mathematical operation first like multiplying by 1 etc. In other situations where you're not working on numbers, but rather strings, you'd generally reach for htmlentities or htmlspecialchars before outputting a string like the input the user submitted back to the html so it's properly escaped to display as text within the html rather than allow them to utilize html and/or run javascript etc.

Thus instead of:

$start = $_POST['startNumber'];
$end = $_POST['endNumber'];

You'd have:

$end = (int)$_POST['startNumber'];
$start = (int)$_POST['endNumber'];

or:

$end = (float)$_POST['startNumber'];
$start = (float)$_POST['endNumber'];

or

$end = $_POST['startNumber'] * 1;
$start = $_POST['endNumber'] * 1;

Etc.

If I had to choose personally, I'd reach for range.

  •  Tags:  
  • Related