如何在数据表中的每个单元格上显示单列的值

问题描述 投票:0回答:1

嗨,我正在尝试按顺序将单列中的值打印到每个单元格表中。

因为我是开发新手,还在苦苦挣扎,请公开我的代码

您可以在下图中看到 iam 数字是垂直连续打印的。 所以我想要的输出是这样的

我的代码看起来像这样给出结果但它水平显示结果

    <?php 
if(!isset($_SESSION)) { 
  @session_start(); 
}
if (isset($_SESSION['sess_user_id']) && $_SESSION['sess_user_id'] != "") {
  echo '';
} else {
  @header('location:./');
}
include_once("../../connection.php");


$mydate = (isset($_POST['date_from'])) ? $_POST['date_from'] : ''; 
            
$date_check = date('Y-m-d',strtotime($mydate)); 

$sql1 = "SELECT * FROM list where kdate='$date_check'";
$queryRecords1 = mysqli_query($conn, $sql1) or die("error to fetch  data");

 ?>

<tbody>

<?php
   echo" <tr>";
   foreach ($queryRecords1 as $row1) :

    if($row1["kno"]>0)
    {
       echo $output1 = '
       
          <td>' .  $row1["kno"] . '</td>';
    }
    else
    {
       

    }
    

   endforeach; 

  echo"</tr>";
   ?>

</tbody>

和我用于数据表的 JavaScript

    <script>
document.title = 'Sales Report';
$(document).ready(function() {
    $('#example1').DataTable({
        "processing": true,
        "dom": 'lBfrtip',
        "lengthChange": false,
        "searching": false,
        "info": true,
        "autoWidth": false,
        "responsive": true,
        "retrieve": true,

        "lengthMenu": [
            [-1],
            ["All"]
        ],
        "bSort": false,
        "bLengthChange": false,
        "buttons": [{
            extend: 'print',
            footer: true,
            text: '<img src="dist/img/printer.png" width="24" height="24">',
            customize: function(win) {
                $(win.document.body)
                    .css({

                        "border-top": "1px solid grey",
                        "border-bottom": "1px solid grey",
                        "font-size": "10pt",
                        "text-align": "justify"


                    })



                $(win.document.body).find('table')
                    .addClass('compact')
                    .css('font-size', 'inherit');
            }


        }],

    });
});
</script>
javascript php html mysql datatables
1个回答
0
投票
  1. 从数据库中获取所有数据并将其存储在数组中。
  2. 根据数组长度计算表格要显示的行数和列数
  3. 遍历数组并相应地填充表格单元格

<?php
// Fetch all the data from the database and store it in an array
$data = [];
while ($row1 = mysqli_fetch_assoc($queryRecords1)) {
    if ($row1["kno"] > 0) {
        $data[] = $row1["kno"];
    }
}

// Calculate the number of rows and columns
$numRows = 4;
$numColumns = ceil(count($data) / $numRows);

// Loop through the data and populate the table cells
echo "<tbody>";
for ($i = 0; $i < $numRows; $i++) {
    echo "<tr>";
    for ($j = 0; $j < $numColumns; $j++) {
        $index = $i + $j * $numRows;
        if (isset($data[$index])) {
            echo "<td>" . $data[$index] . "</td>";
        } else {
            echo "<td></td>";
        }
    }
    echo "</tr>";
}
echo "</tbody>";
?>

© www.soinside.com 2019 - 2024. All rights reserved.