Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

html - how to solve this using while loop php

can anyone help me in making a table like the one below using PHP HTML code? seems to use looping and calculation formulas. I've tried coding but I'm stuck and have no more ideas until here, (please understand that I'm a newbie :"D)

    <table class="table">
            <thead>
                <th>No</th>
                <th>Qty</th>
                <th>Val1</th>
                <th>Val2</th>
            </thead>
<?php
$no=1;
$qty=6;
$val1=1;
while($no<=4){
?>
            <tbody>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $qty; ?></td>
                    <td><?php echo $val1; ?></td>
                    <td><?php echo $val1+$qty-1 ?></td>
                </tr>
<?php } ?>
            </tbody>
        </table>

enter image description here

column assumption:

  1. No: running number
  2. Qty: the value that determines the calculation of the Val2 column
  3. Val1: the first value / first row is determined by yourself, for the next value 1 is added from the value in the previous Val2 row.
  4. Val2: Qty + Val1 (one line)

expected output:

enter image description here

question from:https://stackoverflow.com/questions/65879760/how-to-solve-this-using-while-loop-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay, there is several things missing from your code.

  1. Move the from loop
  2. Add changes to the variables in code, so they update every time the loop is executed.
<table class="table">
    <thead>
        <th>No</th>
        <th>Qty</th>
        <th>Val1</th>
        <th>Val2</th>
    </thead>
    <tbody>
        <?php
            $qty = 6;
            $val1 = 1;
            $no = 1;
            while($no<=4) {
               $val2 = $qty * $no;
        ?>
        <tr>
            <td><?php echo $no; ?></td>
            <td><?php echo $qty; ?></td>
            <td><?php echo $val1; ?></td>
            <td><?php echo $val2; ?></td>
        </tr>
        <?php 
               $val1 += $qty;
               $no++;                
            } 
        ?>
    </tbody>
</table>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...