Jump to content


Photo

Dropdown List Help - Writing Value To A Procedure


  • Please log in to reply
2 replies to this topic

#1 tooNight

tooNight
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 April 2006 - 06:14 PM

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?

<?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>";
?>


My shocking attempt at it which I keep getting errors with.
<?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>";
?>


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 20 April 2006 - 08:53 PM

try

<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 tooNight

tooNight
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 April 2006 - 09:50 PM

Thanks this code should could in handy though fixed my code by changing:

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

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

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

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users