Page 1 of 1

Intuitive Drop Down <Selected>

Posted: Tue Mar 11, 2003 2:47 am
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

Posted: Tue Mar 11, 2003 2:49 am
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

Posted: Tue Mar 11, 2003 3:20 am
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

Posted: Tue Mar 11, 2003 3:34 am
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

Posted: Tue Mar 11, 2003 3:49 am
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

Posted: Tue Mar 11, 2003 3:55 am
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

Posted: Tue Mar 11, 2003 9:48 am
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"...)