I've looked around for this and have found nothing.
<?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>";
Essentially what I want is only to display each element if the column planet_1
or planet_2
are different. However, on top of this they should be interchangeable - so if planet_1 is X and planet_2 is Y
, then if planet_1
is Y
and planet_2
is X
they will not be shown twice by the foreach
loop. I'm not even sure if this is possible.
try this you can do with this logic.
<?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>";
?>