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
Intuitive Drop Down <Selected>
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Just add:
after
Mac
Code: Select all
echo '<option value="0">>Please Choose<</option>';Code: Select all
echo '<select name="CityID" size="1" class="menuForm">';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
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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 -
Mac
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>";
?>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
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK