2

I am trying to make a table, where the $f1,$f2..etc. will be at the top, and the min increments by five at the left column.
However, I keep ending up with the $f1 header generated all along the side instead of the min increment.
The actual values in the table should be the header multiplied by the sidebar. My requests work fine, but the actual cells aren't the way i intend to have them.

<table class="table">
<?php 
$max = 60;
$min = 30;

$f1= 5;
$f2= 10;
$f3= 15;
$f4= 20;
$f5= 25;

$count = array($f1,$f2,$f3,$f4,$f5);
$NumRows = $max;
echo '<br />';
for($i=$min;$i<=$NumRows;$i=$i+5){
    echo '<tr>';
    for($j=0; $j <= 4; $j++) {
        if($i ==$min || $j ==0) {
            echo '<td class="cell1">'.$count[$j].'</td>';

        }
        else {
            echo '<td class="cell2">'.$i*$count[$j].'</td>';
        }
    }
    echo '</tr>';
}
?>
</table>

it should essentially look like a multiplication table.

i

11
  • Can you provide a sample set of all the php variables you are fetching from request? Commented Apr 5, 2020 at 11:14
  • @ParthManaktala i've just edited with some example values! Commented Apr 5, 2020 at 11:16
  • Also please provide the expected output. Commented Apr 5, 2020 at 11:16
  • @ParthManaktala the expected output is a plain html page with a table, how should I add it into the table? Or should i explain it more? Commented Apr 5, 2020 at 11:20
  • 1
    @ParthManaktala I've added an image of the way its meant to look. The leftmost column is meant to be the min, incrementing by 5 (30,35,40..60) while the top is the values i put in the count array. Commented Apr 5, 2020 at 11:28

3 Answers 3

1

Simplified example without all these for loops and ifs (fiddle):

echo '<table>';
$max = 60;
$min = 30;

$f1= 5;
$f2= 10;
$f3= 15;
$f4= 20;
$f5= 25;

$count = array($f1,$f2,$f3,$f4,$f5);
$NumRows = $max;

echo '<tr>';
echo '<td>&nbsp;</td>';
foreach ($count as $item) {
    echo '<td>' . $item . '</td>';
}
echo '</tr>';

for($i=$min;$i<=$NumRows;$i=$i+5){
    echo '<tr>';
    echo '<td>' . $i . '</td>';
    foreach ($count as $item) {
        echo '<td>' . $i * $item . '</td>';
    }    
    echo '</tr>';
}
echo '</table>';
Sign up to request clarification or add additional context in comments.

Comments

0

This should get you going

<?php 
$max = 60;
$min = 30;

$f1= 5;
$f2= 10;
$f3= 15;
$f4= 20;
$f5= 25;

$count = array($f1,$f2,$f3,$f4,$f5);
$NumRows = $max;
echo '<br />';
for($i=$min;$i<=$NumRows;$i=$i+5){
    echo '<tr>';
    for($j=0; $j <= 4; $j++) {
        if($i == $min){
            echo '<td class="cell1" style="color:red">'. $count[$j] .'</td>';;
        }
        else if($j ==0) {
            echo '<td class="cell1" style="color:blue">'. $count[$j] . "(" . $i . ")" .  '</td>';
        }
        else {
            echo '<td class="cell2">'.$i*$count[$j].'</td>';
        }
    }
    echo '</tr>';
}
?>

1 Comment

Remove the style later, it is just for reference and understanding, Also if the second if-else. You can have both the values so select if you need the increment i.e 5 or $count[$j] which will give you 30,35 etc
0

Here is solution. You need to fill first column with $i instead of $count[$j]

<table class="table">
<?php 
    $max = 60;
    $min = 30;
    $increment = 5;

    $f1= 5;
    $f2= 10;
    $f3= 15;
    $f4= 20;
    $f5= 25;

    $count = array($f1,$f2,$f3,$f4,$f5);
    echo '<tr><td></td>';
    foreach ( range(1, count($count)) as $cell ) {
        echo '<td>' . $cell * $increment . '</td>';
    }
    echo '</tr>';
    for ( $i=$min; $i <= $max; $i += $increment ) {
        echo '<tr>';
        echo '<td class="cell1">' . $i . '</td>';
        for ( $j=0; $j <= 4; $j++ ) {
                echo '<td class="cell2">' . $i * $count[$j] . '</td>';
        }
        echo '</tr>';
    }
?>
</table>

3 Comments

this works perfectly, but I am missing the values for the top column (should have 5 above 150, 10 above 300) etc. How can i do this? thanks!
Does't it give one extra row with elements 150 300 450 600 750 ?
@saxagi7 I've changed answer

Your Answer

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