Jump to content

Archived

This topic is now archived and is closed to further replies.

tooNight

Dropdown List Help - Writing Value To A Procedure

Recommended Posts

Hi there,

Trying to have a selectbox that queries the database which contains two fields countryID and countryName and outputs countryID. I can get it to work ok but not when I put [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]if($form->value("country") == "$row[countryID]"){ echo "selected"; }[/quote] which needs to be in to write the value to my procedures. If someone would be so kind to point me in the right direction and tell me what syntax mistake I am making it would be a great help.

Thanks very much

The code below works but I need to insert if($form->value("country") == "$row[countryID]"){ echo "selected"; } where it says HERE, what is the best way of doing this?
[code]

<?php
$p2=mysql_query("SELECT countries.countryID, countries.countryName FROM countries") or die(mysql_error());
echo "<select name=country>";
while($row=mysql_fetch_assoc($p2)) {
echo "<option value=$row[countryID]HERE>$row[countryName]</option>";
}
echo "</select>";
?>
[/code]


My shocking attempt at it which I keep getting errors with.
[code]
<?php
$p2=mysql_query("SELECT countries.countryID, countries.countryName FROM countries") or die(mysql_error());
echo "<select name=country>";
while($row=mysql_fetch_assoc($p2)) {
echo "<option value=$row[countryID] {if($form->value("country") == "$row[countryID]"){ echo "selected"; }}>$row[countryName]</option>";
}
echo "</select>";
?>
[/code]

Share this post


Link to post
Share on other sites
try

[code]<?php
include 'db.php'; // db connect

function countryOptions($current) {
        $sql = "SELECT ID, countryName
                 FROM country
                 ORDER BY countryName";
        $str = "<option value=''>- select country -</option>\n";

        $res = mysql_query($sql) or die (mysql_error());
        while (list ($id, $name) = mysql_fetch_row($res)) {
               $selected = $id==$current ? 'SELECTED' : '';
               $str .= "<option value='$id' $selected>$name</option>\n";
        }
        return $str;
}

$country = '';

if (isset($_GET['country'])) {
    $country = $_GET['country'];
    echo "You selected $country";
}

?>
<FORM method='GET'>
<select name='country'>
        <?php echo countryOptions($country)?>
</select>

<INPUT TYPE='SUBMIT'  name='submit' value='Submit'>
</FORM>[/code]

Share this post


Link to post
Share on other sites
Thanks this code should could in handy though fixed my code by changing:

so this:
[code]echo "<option value=$row[countryID] {if($form->value("country") == "$row[countryID]"){ echo "selected"; }}>$row[countryName]</option>";
} [/code]

becomes:
[code]echo "<option value=$row[countryID] ".(($form->value('country')==$row['countryID'])?'selected':'').">$row[countryName]</option>";
} [/code]

But think your way is more efficient, so maybe will implement it later if I have time.

Thanks

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.