我正在尝试显示 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']} </font></td>
<td><font style='{$most}'>{$rowr['character']} </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']} </font></td>
<td><font style='{$most}'>{$rowr['character']} </font></td>
<td><font style='{$most}'>{$rowr['year']}</font></td></tr>";
echo "</tbody>";
}
echo "</table><br><br>";
}
最佳答案
您正在为每一行创建一个表格。如果查看 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']} ";
if ($rowr['type'] == 0) {
$shows .= "<tr><td><font>{$rowr['knownFor']} </font></td>
<td><font>{$rowr['character']} </font></td>
<td><font>{$rowr['year']}</font></td></tr>";
$shows .= "</tbody></table><br><br>";
}
if ($rowr['type'] == 1) {
$movies .= "<tr><td><font>{$rowr['knownFor']} </font></td>
<td><font>{$rowr['character']} </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.
关于mysql - 使用 if 和 while 循环时在错误的位置拆分表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783715/