繁体   English   中英

如何在自动创建的 html/PHP 表中回显 SQL 表数据

[英]How to echo SQL table data in auto created html/PHP table

我有一个名为 res 的 SQL 表,其中包含一些行和列。 例如:

Name Class Sub1 Sub2 Sub3 sub4

s1    2     10   12  45   15
s2    2     50   12  14   60
s3    2     10   12  40   15
s4    2     20   12  14   15
s5    2     10   12  11   15
............................
............................
s500  2     11   12  13   16
a1    5     05   10  12   14
a2    5     45   10  16   14
a3    5     50   11  12   15
a4    5     45   10  12   14
............................
............................
a900  5     30   15   14  20

如果有人在 class.php 表单中输入 5,那么结果应该按照名称的降序显示在自动生成的 html/PHP 表中。 例如:

Name Class Sub1 Sub2 Sub3 Sub4
a2    5     45   10  16   14
a3    5     50   11  12   15
a4    5     45   10  12   14
a5    5     45   10  16   14
a6    5     50   11  12   15
a7    5     45   10  12   14
............................
............................

我的 class.php 代码是

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<table class="table table-bordered" >
<?php
$servername = "localhost";
$username = "adsdt";
$password = "ssfdfsg";
$dbname = "absdt";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$Class = mysqli_real_escape_string($conn, $_POST['Class']);


$sql = "SELECT * from res
    WHERE class = '$class'";



$result = $conn->query($sql);
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
    if (empty($columns)) {
        $columns = array_keys($row);
    }
    $resultset[] = $row;
}


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


    echo "<thead><tr class='info';><th>Name</th><th>Class</th><th>Sub1</th><th>Sub2</th><th>Sub3</th><th>Sub4</th></tr></thead><tbody><tr class='success';><td>{$row['name']}</td><td>{$row['class']}</td><td>{$row['Sub1']}</td><td>{$row['Sub2']}</td><td>{$row['Sub3']}</td><td>{$row['Sub4']}</td></tr></tbody></table>";
        echo "</table>";



// Print the data
while($row = mysql_fetch_row($result)) {
    foreach($row as $_column) {
        echo "{$_column}";
    }
}
    }
} else {
    echo "Information Not Available";
}

?>


</table>
</body>
</html>

我的代码正在获取符合条件的所有结果,但只在表中显示一个(第一个结果)结果,而所有其他结果只是在它们之间没有空格...我无法指定行号,因为我不知道确切的数字,所有行都因类而异,或者无法编写相同的重复代码来回显所有行,因为我不知道行到底是多少......

应在代码中进行哪些更改以在 html/PHP 生成的表中显示所有 SQL 表数据?

这是设置以 HTML 形式显示的动态列和记录所需的代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<?php

        # Your database credentials goes here
        $servername = "localhost";
        $username = "root";
        $password = "123456";
        $dbname = "stackoverflow";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        # Set Your Table class id to fetch records
        # You can set it from $_GET OR $_POST value
        $class = 5;
        //$class = mysqli_real_escape_string($conn, $_POST['Class']);

        # Fetch records 
        $sql = "SELECT * FROM res WHERE class = '$class'";

        $result = $conn->query($sql);
        $columns = array();
        $resultset = array();

        # Set columns and results array
        while ($row = mysqli_fetch_assoc($result)) {
            if (empty($columns)) {
                $columns = array_keys($row);
            }
            $resultset[] = $row;
        }


        # If records found
        if( count($resultset > 0 )) {
?>
            <table class="table table-bordered" >
                <thead>
                    <tr class='info';>
                        <?php foreach ($columns as $k => $column_name ) : ?>
                            <th> <?php echo $column_name;?> </th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>

                    <?php

                        // output data of each row
                        foreach($resultset as $index => $row) {
                        $column_counter =0;
                    ?>
                        <tr class='success';>
                            <?php for ($i=0; $i < count($columns); $i++):?>
                                <td> <?php echo $row[$columns[$column_counter++]]; ?>   </td>
                            <?php endfor;?>
                        </tr>
                    <?php } ?>

                </tbody>
            </table>

    <?php }else{ ?>
        <h4> Information Not Available </h4>
    <?php } ?>

</body>
</html>

希望,它可以帮助你的朋友。 我也修改了编码错误,所以不用担心:)

我遇到了同样的问题,所以我想出了一个函数,该函数可以将名为 $result 的数组中的 SQL 数据(表)打印为适当的 html 表,并且它会响应。 这意味着可以正确打印各种 SQL 表。

function printTable($result){
    if(!mysqli_num_rows($result) > 0){
        echo 'no results';
    }
    else{        
        echo '<table class="table">';
        $y=0;
        while($row = mysqli_fetch_assoc($result)){
            if ($y <= 0){
                echo "<tr>";
                for ($x = 0; $x < count(array_keys($row)); $x++){
                    echo "<th>";
                    echo array_keys($row)[$x];
                    echo "</th>";
                }
                echo "</tr>";
            }
            echo "<tr>";
            for ($x = 0; $x < count(array_keys($row)); $x++){
                echo "<td>";
                echo $row[array_keys($row)[$x]];
                echo "</td>";}
            echo "</tr>";
            $y++;
        }
        echo "</table>";
    }
    
}

您可以通过从 sqli 查询中获取 $result 并将其解析为如下功能来使用该函数:

$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
            printTable($result);}

输出看起来像这样:

    columnName1...columnName2...columnName3...columnNameX
    .....................................................
    row1Field1 ...row1Field2 ...row1Field3 ...row1FieldX
    row2Field1 ...row2Field2 ...row2Field3 ...row2FieldX
    row3Field1 ...row3Field2 ...row3Field3 ...row3FieldX
    rowXField1 ...rowXField2 ...rowXField3 ...rowXFieldX

评论:我知道胶水是否是正确的做法,因为我是初学者

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM