0

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

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>";
   }
 }

坦克

4

3 回答 3

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>";
于 2013-11-01T09:58:29.097 回答
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>";
 }
于 2013-11-01T09:58:34.153 回答
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>";
}
于 2013-11-01T09:59:12.330 回答