<?php
include("top.html");
include("adventure_shared.php");
?>

    <h2>Countries that fit your preferences for your next adventure are:</h2>

        <?php
        print_countries();
        ?>
  </body>
</html>

<?php
function print_countries() {
    $languages = "'" . implode("', '", $_GET["languages"]) . "'";

    $query = "SELECT DISTINCT c.name
              FROM Countries c
              JOIN CountriesLanguages cl ON c.code = cl.country_code
              WHERE c.continent = '" . $_GET["continent"] . "'
                  AND cl.language IN ($languages)
                  AND cl.official = 'T'
                  AND (((c.gnp * 1000000) / c.population) < 10000
                       OR c.life_expectancy < 70)
            ORDER BY c.name";
    $results = perform_query($query);

    if (mysql_num_rows($results) > 0) {
        ?>
        <ul>

        <?php
        while ($row = mysql_fetch_array($results)) {
            ?>
            <li><?= $row["name"] ?></li>
            <?php
        }
        ?>

        </ul>
    <?php
    } else {
        ?>

        <p>Sorry there were no countries matching your preferences.</p>
        <?php
    }
}
?>
