簡體   English   中英

在php中包含sql數據的HTML表格(表格行取決於sql列)

[英]HTML table with sql data in php (table row depend by sql columns)

您好,我在sql中獲得了此示例數據

$data = array(
  array('id' => '1','name' => 'name1','surname' => 'surname1'),
  array('id' => '2','name' => 'name2','surname' => 'surname2'),
  array('id' => '3','name' => 'name3','surname' => 'surname3'),
  array('id' => '4','name' => 'name4','surname' => 'surname4')
);

我想在html表中顯示,但我的代碼無法正常工作:

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$select_data = "SELECT * FROM dane ORDER BY `id` DESC";
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html">
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <table style="width: 100%;" border="1" cellspacing="5" cellpadding="5">
            <thead>
                <tr>
                    <th align="left" valign="middle">id</th>
                    <th align="center" valign="middle">name</th>
                    <th align="center" valign="middle">surname</th>                    
                </tr>
            </thead>
            <?php
            $result = mysql_query($select_data);
            while ($data = mysql_fetch_row($result)) { 
            }
            ?>
            <tbody>
                <tr>
                    <td align="center" valign="middle"><?php echo $data['id']; ?></td>
                    <td align="center" valign="middle"><?php echo $data['name']; ?></td>
                    <td align="left" valign="middle"><?php echo $data['surname']; ?></td>
                </tr>    
            </tbody>
        </table>
    </body>
</html>

但是我也不想html表中的行數取決於sql表中的列數。 例如,在這種情況下,我只想顯示三行(sql表中的三列)。 當我將列添加到sql表時,我希望html輸出表中的行動態增加。

有人可以幫我這個代碼嗎?

將代碼更改為此:

<tbody>
    <?php
    $result = mysql_query($select_data);
    while ($data = mysql_fetch_row($result)) {
        ?>
        <tr>
            <td align="center" valign="middle"><?php echo $data['id']; ?></td>
            <td align="center" valign="middle"><?php echo $data['name']; ?></td>
            <td align="left" valign="middle"><?php echo $data['surname']; ?></td>
        </tr>
        <?php
    }
    ?>
</tbody>

您正在關閉while循環,然后再顯示結果

您關閉while循環不正確:

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$select_data = "SELECT * FROM dane ORDER BY `id` DESC";
$result = mysql_query($select_data);
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html">
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <table style="width: 100%;" border="1" cellspacing="5" cellpadding="5">
            <thead>
                <tr>
                    <th align="left" valign="middle">id</th>
                    <th align="center" valign="middle">name</th>
                    <th align="center" valign="middle">surname</th>                    
                </tr>
            </thead>
            <tbody>
            <?php while ($data = mysql_fetch_row($result)):?>
                <tr>
                    <td align="center" valign="middle"><?php echo $data['id']; ?></td>
                    <td align="center" valign="middle"><?php echo $data['name']; ?></td>
                    <td align="left" valign="middle"><?php echo $data['surname']; ?></td>
                </tr>
            <?php endwhile;?>
            </tbody>
        </table>
    </body>
</html>

使用“ while(cond):”和“ endwhile;” 命令比使用帶有大括號的封裝更好地看到了東西的起點和終點。

請考慮將數據庫包裝程序從mysql_切換到PDOmysqli ,因為不再積極支持mysql。

您還可以改用:

<?php echo $data['id']?>

簡稱:

<?=$data['id']?>

在5.3之后,也可以不帶php短打開(我認為是5.3)

如果我正確理解您的問題,則希望返回的行數與表dane中的列數匹配。 下面的代碼應該做到這一點,我強烈建議使用mysqli。 從PHP 5.5.0開始不推薦使用mysql_query擴展名。

<?php
$db = new mysqli('localhost', 'root', '', 'test'); // server, user, pass, database
$table_name = 'dane'; // table

// Let's make sure we could establish a connection
if($db->connect_errno > 0){
    die('Unable to connect to the database ' . $db->connect_error);
}

// Build our select to return column names only
$select_cols = "SELECT column_name FROM information_schema.columns WHERE table_name='$table_name'";


if(!$result = $db->query($select_cols)){
    die('There was an error running the query.');
}

while($row = $result->fetch_assoc()){
    $cols[] = $row['column_name']; // Store the columns to an array. It will be further used.
}

// Implode the column names to a comma delimited string to use in the next select. It's also a good practice not to use asterisk in your select statements
$table_headers = implode(',', $cols);

// Query for records with a limit to number columns in the $table_name
$select_data = "SELECT $table_headers FROM $table_name ORDER BY `id` DESC LIMIT 0 , $result->num_rows"; 

if(!$result = $db->query($select_data)){
    die('There was an error running the query ' . $db->error);
}

while($row = $result->fetch_assoc()){
    $data[] = $row; // Store the data into an array to be used in the html table
}
$db->close(); // Close our connection
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html">
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <table style="width: 100%;" border="1" cellspacing="5" cellpadding="5">
            <thead>
                <tr>
                    <?php foreach ($cols as $k) : // Loop through columns ?>
                    <th align="center" valign="middle"><?php echo $k; ?></th>
                    <?php endforeach; ?>                  
                </tr>
            </thead>
            <tbody>

                    <?php foreach ($data as $k) : // Loop through each $data array() ?>
                    <tr>
                        <?php foreach ($k as $v) : // Let's display the records ?>
                        <td align="center" valign="middle"><?php echo $v; ?></td>
                        <?php endforeach; ?>
                    </tr>
                    <?php endforeach; ?>

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

我還自由地將列名動態顯示為表標題,這將消除在列增加時稍后手動添加它們的需要。 如果您想手動創建它們,只需用以下內容替換頂部的php部分:

<?php
$db = new mysqli('localhost', 'root', '', 'test'); // server, user, pass, database
$table_name = 'dane'; // table

// Let's make sure we could establish a connection
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

// Build our select to return column names only
$select_cols = "SELECT column_name FROM information_schema.columns WHERE table_name='$table_name'";


if(!$num_cols = $db->query($select_cols)){
    die('There was an error running the query.');
}

$select_data = "SELECT * FROM $table_name ORDER BY `id` DESC LIMIT 0 , $num_cols->num_rows"; 

if(!$result = $db->query($select_data)){
    die('There was an error running the query [' . $db->error . ']');
}

while($row = $result->fetch_assoc()){
    $data[] = $row; // Store the data into array to be used in the html table
}
$db->close(); // Close our connection

// print_r('<pre>');
// print_r($data);
// print_r('</pre>');
?>

並調整<thead></thead>之間的html代碼。 整個樣本很快就放在一起了,因此可以根據需要進行改進和調整。 請檢查是否有錯字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM