表头在while循环中重复

时间:2013-11-01 09:55:53

标签: php

我有这个代码,我想防止表头重复。 有人可以帮忙吗?在我发布此问题之前,本网站要求我提供更多详细信息。

if (isset($_POST['toys'])) {
$query = 'SELECT * FROM `toys` WHERE size = ?';
$sth = $db->prepare($query);
foreach($_POST['toys'] as $each_check) {
    if( ! $sth->execute(array($each_check)) ) {
        die('MySQL Error: ' . var_export($sth->error_info(), TRUE));
    }

    echo "<table>";
    echo "<tr>
            <th>ratio</th>
            <th>size</th>
            <th>built</th>
            <th>description</th>            
          </tr>";

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

        echo "<tr><td>" . $row['ratio'] .
            "</td><td>" . $row['size'] .
            "</td><td>" . $row['built'] .
            "</td><td>" . $row['description'] .
            "</td></tr>";
     }
    echo "</table>";
   }
 }

3 个答案:

答案 0 :(得分:0)

<table>循环

之前添加foreach标记
echo "<table>";
echo "<tr>
    <th>ratio</th>
    <th>size</th>
    <th>built</th>
    <th>description</th>            
    </tr>";

foreach($_POST['toys'] as $each_check) {
    if( ! $sth->execute(array($each_check)) ) {
        die('MySQL Error: ' . var_export($sth->error_info(), TRUE));
    }
    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

        echo "<tr><td>" . $row['ratio'] .
            "</td><td>" . $row['size'] .
            "</td><td>" . $row['built'] .
            "</td><td>" . $row['description'] .
            "</td></tr>";
     }
}

// And then echo it after foreach

echo "</table>";

答案 1 :(得分:0)

因为你正在回显foreach中的表头。所以当迭代发生时会重复它。在循环之外尝试它。

if (isset($_POST['toys'])) {
$query = 'SELECT * FROM `toys` WHERE size = ?';
$sth = $db->prepare($query);

//Open the table before loop
 echo "<table>";
    echo "<tr>
            <th>ratio</th>
            <th>size</th>
            <th>built</th>
            <th>description</th>            
          </tr>";
foreach($_POST['toys'] as $each_check) {
    if( ! $sth->execute(array($each_check)) ) {
        die('MySQL Error: ' . var_export($sth->error_info(), TRUE));
    }

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

        echo "<tr><td>" . $row['ratio'] .
            "</td><td>" . $row['size'] .
            "</td><td>" . $row['built'] .
            "</td><td>" . $row['description'] .
            "</td></tr>";
     }
   }
   //Close the table after the loop
   echo "</table>";
 }

答案 2 :(得分:0)

修改您的代码,如下所示

if (isset($_POST['toys'])) {
    $query = 'SELECT * FROM `toys` WHERE size = ?';
    $sth = $db->prepare($query);
    echo "<table>";
    echo "<tr>
            <th>ratio</th>
            <th>size</th>
            <th>built</th>
            <th>description</th>            
          </tr>";
    foreach ($_POST['toys'] as $each_check) {
        if (!$sth->execute(array($each_check))) {
            die('MySQL Error: ' . var_export($sth->error_info(), TRUE));
        }

        while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

            echo "<tr><td>" . $row['ratio'] .
            "</td><td>" . $row['size'] .
            "</td><td>" . $row['built'] .
            "</td><td>" . $row['description'] .
            "</td></tr>";
        }
    }
    echo "</table>";
}