1

我正在尝试显示 2 个表格,1 个用于节目,1 个用于电影,但目前每条记录显示 1 个表格

这是页面http://starsqa.com/ana-lucasey-about

这是新的代码片段:

  if ($result['order'] == 1 OR $result['order'] == 2 OR $result['order'] == 6 OR $result['order'] == 4) {
            $qry_stringr = "SELECT roles.starID, starName, roles.knownFor, roles.`character`, roles.`year`, most, type FROM roles INNER JOIN stars ON roles.starID = stars.starID WHERE roles.starID = {$result['starID']} ORDER BY roles.`year` DESC";
            $prepr = $pdo_conn->prepare($qry_stringr);
            $prepr->execute(array());

        $showsArray = array();
        $moviesArray = array();

 while ($rowr = $prepr->fetch(PDO::FETCH_ASSOC)) {
      if ($rowr['type'] == 0) {
         $showsArray[] = $rowr;
      }
      else if($rowr['type'] == 1) {
         $moviesArray[] = $rowr;
      }
 }
echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Show</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
 foreach($showsArray as $row) {
                    $most = $rowr['most'] == 1 ? ' color:#DE5635;' : '' OR $rowr['most'] == 0 ? ' color:black;' : '';
                    echo "<tr><td><font style='{$most}'>{$rowr['knownFor']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                          <td><font style='{$most}'>{$rowr['character']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                          <td><font style='{$most}'>{$rowr['year']}</font></td></tr>";
                    echo "</tbody>";
 }
 echo "</table><br><br>";
echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Movie</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
 foreach($moviesArray as $row) {
                    $most = $rowr['most'] == 1 ? ' color:#DE5635;' : '' OR $rowr['most'] == 0 ? ' color:black;' : '';
                    echo "<tr><td><font style='{$most}'>{$rowr['knownFor']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                          <td><font style='{$most}'>{$rowr['character']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                          <td><font style='{$most}'>{$rowr['year']}</font></td></tr>";
                    echo "</tbody>";
 }
 echo "</table><br><br>"; 
 }
4

1 回答 1

1

您正在为每一行创建一个表。如果您查看 while 循环,您可以看到对于数据的每次迭代,您正在创建一个新表。有很多方法可以解决这个问题,我会创建两个数组,一个用于节目,一个用于电影。在循环期间,将行添加到适当的数组中。毕竟,循环遍历每个数组以创建表。

例如:

 $showsArray = array();
 $moviesArray = array();

 while ($rowr = $prepr->fetch(PDO::FETCH_ASSOC)) {
      if ($rowr['type'] == 0) {
         $showsArray[] = $rowr;
      }
      elseif($rowr['type'] == 1) {
         $moviesArray[] = $rowr;
      }
 }
 echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Show</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
 foreach($showsArray as $row) {
     //create the table row here.
 }
 echo "</table>";
echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Movie</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
 foreach($moviesArray as $row) {
     //create the table row here.
 }
 echo "</table>";

另一种方法是在while循环期间,而不是使用echo,将HTML附加到一个变量,然后在循环之后输出它。

$movies = "";
$shows = "";
while ($rowr = $prepr->fetch(PDO::FETCH_ASSOC)) {
        echo "{$rowr['type']} - {$rowr['knownFor']} &nbsp;&nbsp;&nbsp;";
        if ($rowr['type'] == 0) {
            $shows .= "<tr><td><font>{$rowr['knownFor']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                  <td><font>{$rowr['character']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                  <td><font>{$rowr['year']}</font></td></tr>";
            $shows .= "</tbody></table><br><br>";
        }
        if ($rowr['type'] == 1) {
            $movies .= "<tr><td><font>{$rowr['knownFor']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                  <td><font>{$rowr['character']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
                  <td><font>{$rowr['year']}</font></td></tr>";
            $movies .= "</tbody></table>";
        }
    }
   //add code here to create your
   //tables and echo $shows and $movies in the appropriate places.
于 2013-05-28T04:27:01.300 回答