Intuitive Drop Down <Selected>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Intuitive Drop Down <Selected>

Post by meandrew »

I have a slight problem. I have a drop that is populated by a previous page selection. I want the returning set of results to beging with a default >please choose < instead of the first record in the set.

this is the code I am working with:

<?

echo "<select name=\"CityID\" size=\"1\" class='menuForm'>";

$result=mysql_query("SELECT CountyID, City, CityID FROM city WHERE CountyID=".$CountyID." ORDER BY City");
while ($row = mysql_fetch_array($result))
{
$county_id=$row['CountyID'];
$city_id=$row['CityID'];
$city=$row['City'];
echo "<option value=\"$city_id\"> $city </option>";
}
echo "</select>";
?>

Andrew
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Just add:

Code: Select all

echo '<option value="0">>Please Choose<</option>';
after

Code: Select all

echo '<select name="CityID" size="1" class="menuForm">';
Mac
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

Your reply was the solution to my problem and I can tell you that without that I would have been struggling for sometime infact I probably would have given up and tried something else.

I really appreciated how quickly you replied.

what is this forum for exactly? Is it for helping peole who program in php or is for those developing php on a higher level?

I'm not sure what I did wrong :(

Andrew
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The questions in the forum range from what might be considered more basic PHP to fairly intense discussions on classes and beyond to more 'advanced' PHP topics.

It's a place for helping people program PHP whether they just started learning or have been doing it for years and if you have a look at the sites linked to from the top navigation bar you'll see there's lots more to the PHP developers network.

Back to your code problem -

Code: Select all

<? 

/* Anything you only want to appear once, like the <select> tags and
   the default <option> info needs to appear before the while loop */
echo '<select name="CityID" size="1" class="menuForm">';
echo '<option value="0">>Please Choose<</option>';

/* On a side note, taking the SQL statement out of mysql_query() and 
    putting it into it's own variable will make it easier to debug if anything
    goes wrong with it. */
$sql = "SELECT CountyID, City, CityID FROM city WHERE CountyID=$CountyID ORDER BY City";
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>'); 

/* Within this while loop the code will be executed repeatedly until all
    of the records returned from the database have been dealt with,
    this means that if there are 10 records the code will be repeated 10 
    times so if you put the default <option> in there it will be shown 10
    times */
while ($row = mysql_fetch_array($result)) { 
    $county_id=$row['CountyID']; 
    $city_id=$row['CityID']; 
    $city=$row['City']; 
    /* This HTML code is repeated for each record */
    echo "<option value="$city_id"> $city </option>"; 
} 
/* The while loop is now over so you put the closing <select> tag because
    you only want one of it. */
echo "</select>"; 
?>
Mac
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

thanks mac thats a real help. I have been hacking php code for a few years and the most frustrating thing is I know what I am doing (a bit) but dont know why!

I thought your signature was aimed at me so that's why i asked the question about the forum :)

This is where the code is working
http://www.punterspower.co.uk/

still early days but it works at least :)

Andrew
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

meandrew wrote:I thought your signature was aimed at me so that's why i asked the question about the forum :)
Oops, sorry - really didn't mean you to think that. Glad you're getting somewhere with your code :) .

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

anyhow you read it and thought about it. That's worth some brownie points
:D
(I hope "brownie point" is the correct idiom, no offense, no joke, really meant as "well done"...)
Post Reply