Values from database

Hi,
I’m trying to select countries from database and present them in a select box.
I managed to do that, but I don’t want the countries that have already been selected to show up again (that is, I want to avoid duplicate values).

I tried using array_unique but couldn’t get it.
Is there another way to do this?
Thanks in advance…


<select name="country">
<?php
include ('connection2host.php');
$get_country = mysql_query("SELECT country FROM mainweb WHERE who='host'");

while ($row = mysql_fetch_array($get_country)){
$country = $row['country'];

echo '<option value="'.$country.'">'.$country.'</option>';
}
?>
</select>


SELECT DISTINCT country FROM mainweb WHERE who = 'host';

:slight_smile:

If you absolutely had to do it the PHP/Array then you would have an existing countries array, and check if the current country is in that array and if not then add it.

However AnthonySterling’s method is much better, general rule of thumb, if you’re doing something with PHP that can be done with SQL, SQL is usually the faster way to do it. :slight_smile:

I was of the opinion that I can use php within html?

but when I try to use the following code in an html document it doesn’t work?
are there special rules in using this php code for the html document?


<form action="hostdatabase.php" method="post">
<div style="text-align: center;">Country:
<select name="country">
<?php
include ('connection2host.php');
$get_country = mysql_query("SELECT DISTINCT country FROM mainweb WHERE who='host'");

while ($row = mysql_fetch_array($get_country)){
$country = $row['country'];

echo '<option value="'.$country.'">'.$country.'</option>';
}
?>
</select>
<p><input name="submit" value="Search" type="submit" /> </p>
</form>

That looks okay to me.

it’s written alright, but if the document is saved as .html than the select box is very narrow and empty.
If the document is saved as .php than it works.

I need it in .html if possible since all my links are leading to .html

Try this quick demo.


<?php
error_reporting(-1);
ini_set('display_errors', true);

include 'connection2host.php';

function countries($distinct = true){
  $result = mysql_query(sprintf(
    "SELECT %s country FROM mainweb WHERE who = 'host';",
    $distinct ? 'DISTINCT' : ''
  ));
  $countries = array();
  while($record = mysql_fetch_assoc($result)){
    array_push($countries, $record['country']);
  }
  return $countries;
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <form action="#" method="post">
      <select name="country">
        <?php foreach(countries() as $country): ?>
          <option value="<?php echo $country; ?>">
            <?php echo $country; ?>
          </option>
        <?php endforeach; ?>
      </select>
      <input type="submit" value="submit" />
    </form>
  </body>
</html>

Oh.

You cannot use PHP in an HTML document. You could however instruct your webserver to parse .htm(l) files as PHP with some configuration trickery.

Failing that, you could use a spot of JavaScript to ‘fetch’ the country list from a .php script.