我环顾四周,却一无所获。
<?php
$viewCurrentFlightsQuery = $db->prepare("SELECT id, planet_1, planet_2 FROM flights WHERE universe = :universe AND company = :company");
$viewCurrentFlightsQuery->bindParam(":universe", $universe);
$viewCurrentFlightsQuery->bindParam(":company", $airlineNameGet);
$viewCurrentFlightsQuery->execute();
$viewCurrentFlights = $viewCurrentFlightsQuery->fetchAll();
echo "You currently have <strong>".$currentFlightsNumber."</strong> active flights!<br><br>";
foreach($viewCurrentFlights as $row){
$selected = $row["id"];
echo '<tr>';
echo '<td class="table">'.$row["planet_1"].'</td><td class="table">'.$row["planet_2"].'</td>';
?>
<td class="table"><form method="post" action="create.php"><input type="hidden" name="selected" value="<?php echo $selected;?>"><button type="submit">View Detail</button> </form></td>
<?php
echo '</tr>';
}
echo "</table><br>";
基本上我想要的只是在列planet_1
或planet_2
不同时显示每个元素。但是,除此之外,它们应该是可互换的 - 所以如果planet_1是X而planet_2是Y
,那么planet_1
是Y
而planet_2
是X
它们不会被foreach
循环显示两次。我甚至不确定这是否可行。
答案 0 :(得分:1)
尝试这个你可以用这个逻辑。
<?php
$viewCurrentFlightsQuery = $db->prepare("SELECT id, planet_1, planet_2 FROM flights WHERE universe = :universe AND company = :company");
$viewCurrentFlightsQuery->bindParam(":universe", $universe);
$viewCurrentFlightsQuery->bindParam(":company", $airlineNameGet);
$viewCurrentFlightsQuery->execute();
$viewCurrentFlights = $viewCurrentFlightsQuery->fetchAll();
echo "You currently have <strong>".$currentFlightsNumber."</strong> active flights!<br><br>";
$arr_temp = array(); // define a temp array
foreach($viewCurrentFlights as $row)
{
$temp_val1 = $row["planet_1"]." ".$row["planet_2"]; // add value in order 1, 2 in temp
$temp_val2 = $row["planet_2"]." ".$row["planet_1"]; // add value in order 2, 1 in temp
// now we will check either order 1 or order 2 in temp array if yes do not print the value
if(in_array($temp_val1, $arr_temp) || in_array($temp_val2, $arr_temp))
{
continue;
}
$arr_temp[] = $temp_val1;
$arr_temp[] = $temp_val2;
$selected = $row["id"];
echo '<tr>';
echo '<td class="table">'.$row["planet_1"].'</td><td class="table">'.$row["planet_2"].'</td>';
?>
<td class="table"><form method="post" action="create.php"><input type="hidden" name="selected" value="<?php echo $selected;?>"><button type="submit">View Detail</button> </form></td>
<?php
echo '</tr>';
}
echo "</table><br>";
?>