Forum Moderators: coopster
I'm a bit of a php (and programming in general) newb. I've been exposed to it enough, but haven't been able to use it enough to really consider myself proficient.
I've been working on an application that is nearly complete, I've just got some minor functionality issues that I'd like to get worked out.
I'm trying to read a record from the database and have it populate a form to be edited by user intervention. Right now, all fields in the form are pulling (and displaying) the data correctly with the exception of the <select> / drop down boxes.
I've tried a number of different things (from various posts I've found online) and come up with different results but I think I'm close.
For my first drop down, I'm trying to pull a value from 1-4 from the database, and have the selected value reflected in the form. If anyone can provide an example of this, I should be able to massage it enough to work for the other drop downs I've got on the form as well.
As I said, I think I'm close, the code below is almost what I want. Right now this returns 4 options in my select box, unfortunately all of them are the 'selected' value. I just want the selected value once, and the other 3 numbers in the array to be displayed. (Arrrg, it feels like the more I type, the more confusing this is! Please help!)
// setup select block
echo "<select name='tick_severity'>";
// setup and initialize array
$arr = array(1,2,3,4);
// build select from array
for($i = 0; $i < count($arr); $i++)
{
if ($sev == $arr[$i])
echo "<option value='" . $arr[$i] . "'>" . $arr[$i] . "</option>";
else
echo "<option value='" . $sev . "' selected='selected'>" . $sev . "</option>";
}
echo "</select>";
// end select block
// setup select block
echo "<select name='tick_severity'>";
// setup and initialize array
$arr = array(1,2,3,4);
// build select from array
for($i = 0; $i < count($arr); $i++)
{
if ($sev!= $arr[$i])
echo "<option value='" . $arr[$i] . "'>" . $arr[$i] . "</option>";
else
echo "<option value='" . $sev . "' selected='selected'>" . $sev . "</option>";
}
echo "</select>";
// end select block
I've tested it a few times, and it seems to return the proper results each time, I still can't believe it. I'm going to wait until tomorrow to make sure the little code Gremlins don't muck it up over night before moving my test code into production, but I think I finally got it.
Still, if anyone can see any flaws in this, (or just plainly a better way to do it), by all means, please let me know.
Thanks so much.
<tr>
<td align="left">Country</td>
<td><?php
$query = "SELECT * FROM gl_countries";
$results = mysql_query($query);
echo '<select name="country_id">';
while ($thisrow = mysql_fetch_array($results))
{
echo '<option value="'.$thisrow['country_id2'].'">'.$thisrow['country_name'].'</option>';
}
echo '</select>';?></td>
</tr> just to clarify:
I am selecting a list of countries from one table (gl_countries), and displaying them in a form as a dropdown list. The form is actually displaying a country_name for each country_id that I have in my gl_countries table. When you select the country_name in the form, it is actually selecting the country_id, and posting it into another table as country_id2 (I use the id, rather than the name just to make sure I have a unique value being posted back)