首页 文章

如何在每个50条记录(最后一列除外)的列中使用HTML <table>标签显示结果?

提问于
浏览
1

我写了一个程序,目前生成540个不同的数字 .

我想在一个列中显示这540个不同的数字,每列有50个记录(除了记录数应小于50的最后一列),使用HTML <table> 标签,但由于环路太多,我无法这样做并且条件正在得到应用 .

以下是我的计划:

<?php
function findFourElements() { 

    $possible_numbers = '';

    $sr_no = 0;

    $row_count = 0;

    for ($i = 0; $i <= 9; $i++) {

        for ($j = 0; $j <= 9; $j++) {

            for ($k = 0; $k <= 9; $k++) {

                for ($l = 0; $l <= 9; $l++) {

                  $possible_numbers[0] = $i;
                  $possible_numbers[1] = $j;
                  $possible_numbers[2] = $k;
                  $possible_numbers[3] = $l;

                    if((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) {
                        $sr_no++;
                        echo $sr_no. ') ' . $possible_numbers[0].$possible_numbers[1].$possible_numbers[2].$possible_numbers[3]. '<br>';                        
                    }
                }
            }
        }
    }   
}

findFourElements(); 
?>

请有人用我的逻辑帮助我 .

2 回答

  • 0

    您可以使用此代码:

    <?php 
    function findFourElements()
    {
    
        $response = [];
    
        $possible_numbers = '';
        $sr_no = 0;
    
        $row_count = 0;
    
        for ($i = 0; $i <= 9; $i++) {
    
            for ($j = 0; $j <= 9; $j++) {
    
                for ($k = 0; $k <= 9; $k++) {
    
                    for ($l = 0; $l <= 9; $l++) {
    
                        $possible_numbers[0] = $i;
                        $possible_numbers[1] = $j;
                        $possible_numbers[2] = $k;
                        $possible_numbers[3] = $l;
    
                        if ((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) {
                            $sr_no++;
                            $response[] = $possible_numbers[0] . $possible_numbers[1] . $possible_numbers[2] . $possible_numbers[3];
                        }
                    }
                }
            }
        }
        return $response;
    }
    
    
    $response = findFourElements();
    
    $data = array_chunk($response, 50);
    
    $srNo = 1;
    ?>
    
    <table>
        <tr>
            <?php foreach ($data as $key => $value) { ?>
                <td>
                    <table>
                        <tr>
                            <th>Sr. No.</th> <th>Vehicle No.</th>
                        </tr>
                        <?php foreach ($value as $val) { ?>
                            <tr>
                                <td>
                                    <?php echo $srNo ?>
                                </td>
                                <td>
                                    <?php echo $val ?>
                                </td>
                            </tr>
                        <?php $srNo++; } ?>
                    </table>
                </td>
            <?php } ?>
        </tr>
    </table>
    

    如果您不想使用返回响应,可以在函数内添加额外的代码 .

  • 1

    你应该使用while语句 .

    <!DOCTYPE html>
    <html>
      <head>
        ...
      </head>
      <body>
    <?php
    
    /** Calculations
    (# = amount)
    540/50 = 10.8   (# of rows)
    50*0.8 = 40     (# of records in last row)
    */
    echo "<table>";
    
    
        $x = 0;
        $ranNum = 0;
        echo "<tr>";
    
            while ($x < 540) {
              /* a round number (1,2,3,4,5,...) %1 = 0.
              So (Number/50)%1 is only 0 if it's in the multiplication table of 50*/
              if ((($x/50)%1) == 0) { 
                echo "</tr>";
                echo "<tr>";
              }
              $ranNum = rand(1000,9999); // random number
              echo "<td id='Track".$x."'>".$ranNum."</td>";
              $x++;
            }
    
        echo "</tr>";
    
    
    echo "</table>";
    ?>
      </body>
    </html>
    

相关问题