我的代码出了什么问题

时间:2018-02-24 12:24:27

标签: php html

我定位数据库中使用serialize()函数存储数据的表。所以我试图反序列化数据&获取以便在HTML表中显示。表格中的第一行,即表格数据()以某种方式正确显示。但是,下一个数据(通过循环)将显示在同一行而不是下一行。这个你能帮我吗。如果我的解释错了,请编辑它。这是我的代码......

<body style="font-family: verdana; letter-spacing: 2px">
<br><br>

<style>
table, td, th {    
    border: 1px solid #ddd;
    text-align: left;
    font-size: 14px;
}

table {
    border-collapse: collapse;
    width: 90%;
    margin: 0 auto;
}

th, td {
    padding: 10px;
}
</style>

<table style="border-collapse: collapse; margin: 0 auto" cellpadding="13" border="1" width="80%">
        <tr>
            <th>Status</th>
            <th>Company Name</th>
            <th>Address</th>
            <th>Your Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Website</th>
            <th>Software Link</th>
        </tr>
        <tr>

<?php

$con = mysqli_connect("localhost", "ghgh", "1223546", "rererer");
$que = "SELECT * FROM wp_db7_forms WHERE form_post_id = 6167";
$run = mysqli_query($con, $que);

while ($row = mysqli_fetch_array($run)) {
    $form_value = $row['2'];
    $form_date = $row['3'];

    $array = unserialize($form_value);
    $expire = date("Y-m-d", strtotime("+90 days"));

    while (list($key, $value) = each($array)) {
?>
        <td style="text-align: center;"><?php echo $value; ?></td>
<?php 
    }
} 
?>
        </tr>
    </table>
</body

2 个答案:

答案 0 :(得分:1)

您正在错误地管理tr标记。您确实将所有动态内容放在一行中,因为您只为它生成一个开头和一个结束tr标记 - 在您拥有的所有循环之外。

所以把它们放在外循环中:

    <table style="border-collapse: collapse; margin: 0 auto" cellpadding="13" border="1" width="80%">
        <tr>
            <th>Status</th>
            <th>Company Name</th>
            <th>Address</th>
            <th>Your Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Website</th>
            <th>Software Link</th>
        </tr>
<?php
    $con = mysqli_connect("localhost", "ghgh", "1223546", "rererer");
    $que = "SELECT * FROM wp_db7_forms WHERE form_post_id = 6167";
    $run = mysqli_query($con, $que);

    while ($row = mysqli_fetch_array($run))
    { ?>
        <tr>
<?php        
        $form_value = $row['2'];
        $form_date = $row['3'];
        $array = unserialize($form_value);
        $expire = date("Y-m-d", strtotime("+90 days"));
        while (list($key, $value) = each($array)) {
?>
            <td style="text-align: center;"><?php echo $value; ?></td>
<?php   } ?>
        </tr>
<?php } ?>
    </table>

答案 1 :(得分:0)

正如@trincot所说,你正在管理tr标签错误,这里是分隔php和HTML的代码的更清晰版本,以保持代码清洁和可读性:

<?php
    $connection = mysqli_connect('','','','');
    $result = mysqli_query($connection,'SELECT .... ');
    $rows = [];

    while($row = mysqli_fetch_array($result)) 
    {
      $rows[] = [
       'value' => $row['2'],
       'date'  => $row['3'],
       'expire' =>  date("Y-m-d", strtotime("+90 days")),
       'data' => unserialize($row['2'])
     ];    
    }
?>

<table style="border-collapse: collapse; margin: 0 auto" cellpadding="13" border="1" width="80%">
    <tr>
        <th>Status</th>
        <th>Company Name</th>
        <th>Address</th>
        <th>Your Name</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Website</th>
        <th>Software Link</th>
    </tr>
    <?php foreach($rows as $row) : ?>
    <tr>
       <?php foreach ($row['data'] as $key => $value) : ?>
          <td style="text-align: center;"><?= $value; ?></td>
       <?php endforeach; ?>
    </tr>
    <?php endforeach; ?>
</table>
相关问题