4

How to exclude some of countries from List Menu at below? Now my code will list all of country name from database.

Example I want to exclude Albania country from the List Menu. How to implement it according to these code.

Code

    <?php $select_country=mysql_query("SELECT * FROM tbl_country ORDER BY country_name ASC")?>
    <select name="country"  onchange="return get_country(this.value);">
        <option value=""><?=SELECT_COUNTRY?></option>
        <? while($country=mysql_fetch_array($select_country)) {?>
           <option  <? if($_SESSION['getcountry']==$country['id']){ ?> selected="selected"<? }?> value="<?=$country['id']?>">
           <?=$country['country_name']?></option>
        <? } ?>
    </select>

Mysql

id    country_name
1   Afghanistan
2   Aland Islands
3   Albania
4   Algeria
5   American Samoa
6   Andorra
2
  • 4
    Tell us, what's your problem with Albania? Commented Aug 8, 2009 at 12:01
  • Nothing problem with Albania. Just an example :) Commented Aug 8, 2009 at 12:08

6 Answers 6

7

Either exclude that row from the result set:

SELECT * FROM tbl_country WHERE country_name != "Albania" ORDER BY country_name ASC

Or you skip it with PHP:

<?php
    while($country=mysql_fetch_array($select_country)) {
        if ($country['country_name'] == 'Albania') {
            continue;
        } ?>
    <option <?php if($_SESSION['getcountry']==$country['id']){ ?> selected="selected"<? }?> value="<?=$country['id']?>"><?=$country['country_name']?></option>
<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

2

If I have understood your question:

 mysql_query("SELECT * FROM tbl_country WHERE country_name <> 'Algeria' ORDER BY country_name ASC")

1 Comment

He wants to exclude Albania and not Algeria ;)
2
"SELECT * FROM tbl_country WHERE country_name <> "Albania" ORDER BY country_name ASC"

The "<>" is equivalent to "!=" and they both mean 'not equal to', so should in theory select all records from the table where country_name is not equal to the defined "Albania."

I think that the following works, also:

"SELECT * FROM tbl_country WHERE country_name NOT IN("Albania","other_country_name", "another_country_name") ORDER BY country_name ASC"

Comments

1

You could set an array of the countries you wish to exclude...

<?php

$excludeArray = array('Albania','United States') ;

$select_country=mysql_query("SELECT * FROM tbl_country ORDER BY country_name ASC");

echo <<<HTML
    <select name="country"  onchange="return get_country(this.value);">
    <option value=""><?=SELECT_COUNTRY?></option>
HTML;

while($country=mysql_fetch_array($select_country)) {
  if (!in_array($country['country_name'],$excludeArray)) {
    $selected = ($_SESSION['getcountry']==$country['id'] ? 'selected="selected"' : '');
    echo "<option value='" . $country['id'] . "' " . $selected . ">" . $country['country_name'] . "</option>" ;
  }
echo "</select>";


?>

Comments

1
<?php $select_country=mysql_query("SELECT * FROM tbl_country ORDER BY country_name ASC")?>
    <select name="country"  onchange="return get_country(this.value);">
    <option value=""><?=SELECT_COUNTRY?></option>
    <?php 
       $exclude = array('Afghanistan', 'Andorra');
       while($country=mysql_fetch_array($select_country)) {
         if(!in_array($country['name'], $exclude){
    ?>
    <option  <? if($_SESSION['getcountry']==$country['id']){ ?> selected="selected"<? }?> value="<?=$country['id']?>"><?=$country['country_name']?></option>
    <? }} ?>
    </select>

Comments

-2

why not just make a status on on your data base? for example albania status show than you can use SELECT FROM country WHERE status='show'

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.