Skip to content
Advertisement

How to get array from list

I have a problem for my program.

This my script:

$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;

for($i=1;$i<=$jumlah;$i++) {

<tr>
    <th> echo $no++; </th>

    for($a=$i;$a<=$jumlah;$a++) {
        echo "<td>".$x/$a."</td>";
    }
    <td>Count</td>
</tr>
}

and this is my result

result page

I want to replace the words “count” with the sum of each column in the same row. and my question, how I can add all column in one rows.

example : row 1 = 1 + 0.5 + 0,33 + 0.25 = 2,08

Thank you for helping, I am sorry if my English so bad.

Advertisement

Answer

Try something like this

initialize $count as zero before the inner loop and in inner loop increment county

  <?php
    $x=1;
    $no=1;
    $tot=0;
    $bobot=0;
    $jumlah=4;
    
    for($i=1;$i<=$jumlah;$i++) {
    ?>
    <tr>
        <th> <?php echo $no++;?> </th>
        <?php
        $count=0;
        
        for($a=$i;$a<=$jumlah;$a++) {
            echo "<td>".$x/$a."</td>";
            $count=$count+($x/$a);
        }
        ?>
        <td><?=$count;?></td>
    </tr>
    <?php
    }
    ?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement