Hi Everyone...

I have been digging around the web for the last two days trying to fix this problem myself, but have come to the realization that I need some help.

I have a mysql query that selects all country names from my country db, (works as described)
I then have a select dropdown that is populated from the above query.
Here is the mysql select.

$result_country = $Db->query('select Country from countries order by Country Desc ');

Here is the select dropdown.

    <select name="country" tabindex="2" class="span3" value='<?php echo $country; ?>' id="country" data-placement="right" rel="tooltip" title="Select your Country..">
                            <?php if($country!=''){?>
                            <option value="<?php echo $country;?>"><?php echo $country;?></option>
                            <?php }else{ ?>
                            <option value="">-- Select Country --</option>
                            <?php }?>
                            <?php
                                 while($country_arr = mysql_fetch_array($result_country)){
                                    $tag_selected = ( (isset($_POST['country']) and $_POST['country'] == $country_arr['Country'] )? 'selected="selected"' : '' );
                                     ?>
                                     <option value="<?php echo $country_arr['Country']?>" <?php echo $tag_selected; ?>><?php echo $country_arr['Country']?></option>
                                     <?php
                                 }

                            ?>
    </select> 

This works great & as expected, I get a full list of Country names populating the dropdown box.

The problem is, I am teaching myself PHP PDO / MySqli for safer coding.

On my new form I have this query to select all country name from my countries db.

$result_country = $Db->query('select Country from countries order by Country Desc');

This works as described, I know it works using a foreach loop like this.

            <?php foreach($result_country as $row) : ?>
                <h2><?php echo $row['Country']; ?></h2>
            <?php endforeach; ?>

The problem I am having is adding this foreach to my select dropdown.
Any help with this would be greatly appreciated.
Thanks

updated

I have found the following here on Daniweb...

<?php
$pdo = new PDO('mysql:host=localhost;dbname=contisec_portal', 'root', '');
$sql = "SELECT dateid FROM date_header";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($stmt->rowCount() > 0) { ?>
  <select name="fileselect">
    <?php foreach ($results as $row) { ?>
      <option value="<?php echo $row['dateid']; ?>"><?php echo $row['dateid']; ?></option>
    <?php } ?>
  </select>
<?php } ?>

This gives me a list of Country names from my countries db.

What I am trying to do now is add in the error validation,
When the form is submitted, a new list of Country names is populating the dropdown box.

On my first piece of code, the dropdown holds the value...
Can anyone help with this please?

First off, that is really badly laid out.. What the hell is going on there?

Second of all: What it is you're trying to do?

I.e. add in the error validation, When the form is submitted, a new list of Country names is populating the dropdown box. I don't understand :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.