0

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.

4
  • 1
    Well for the first part, WHERE planet_1 != planet_2 can be added to your SQL Commented Feb 20, 2014 at 4:46
  • Doesn't exactly help. It's supposed to be if two rows in the table have the same text for the columns planet_1 and planet_2 it only shows them as equal. Probably my terrible wording. Thanks anyway. :) Commented Feb 20, 2014 at 4:50
  • Yeah I know, I am still figuring out a way to cross check ha, this will solve the duplicate problem though. Commented Feb 20, 2014 at 4:51
  • Is it possible that you may need value X to be repeated several times under planet_1 in your output? Commented Feb 20, 2014 at 4:53

1 Answer 1

1

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>";
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.