2013-11-01 3 views
0

У меня есть этот код, где я хочу предотвратить повторение заголовка таблицы. Кто-то может помочь? этот веб-сайт попросит меня поставить более подробную информацию, прежде чем я смогу опубликовать этот вопрос.заголовок таблицы повторяется в цикле while

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

танки

ответ

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

Поскольку вы вторя заголовок таблицы в foreach.So это будет повторяться, когда итерации occusrs..Try его вне петля.

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

спасибо так много дорогих людей !!! – Paco

Смежные вопросы