FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » php filling in listbox value based on db record ??
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
php filling in listbox value based on db record ?? [message #169399] Thu, 09 September 2010 13:21 Go to next message
PAkerly is currently offline  PAkerly
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
I've got an html page that fills in text boxes based on an sql
record. Doing things like this:

$sql = "SELECT date, user1, user2, user3, id
FROM myuserdata
WHERE id= '$txtid'
ORDER BY id";

//Get a result set from the query (this is the array)
$rs = mysql_query($sql) or die(mysql_error() . "<br />Sql:" . $sql);

//Declare an empty array to store the result set in
$array = array();

//Iterate through the result set we declared earlier, looping through
each row
while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
{
//Append the row from the database, onto the array :)
$array[] = $row;
}

and then to fill in text boxes based on the previously selected ID I
do...
foreach($array as $row):

<input name="txtdate" type="text" id="txtdate" value="<?php echo
$row['date'];?>"/>

SO NOW I WANT TO ALSO SELECT A DEFAULT VALUE FOR A LISTBOX BASED ON
THE ID....

<select name="lstuser2" id="lstuser2">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>

So lets say that the record for this user called for the listbox to be
set to C b/c thats whats in the DB for this person, how could I do
this based on the record?

thanks.
Re: php filling in listbox value based on db record ?? [message #169401 is a reply to message #169399] Thu, 09 September 2010 14:03 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 9, 9:21 am, PAkerly <pake...@gmail.com> wrote:
> I've got an html page that fills in text boxes based on an sql
> record.  Doing things like this:
>
> $sql = "SELECT date, user1, user2, user3, id
>                 FROM myuserdata
>                 WHERE id= '$txtid'
>                 ORDER BY id";
>
> //Get a result set from the query (this is the array)
> $rs = mysql_query($sql) or die(mysql_error() . "<br />Sql:" .  $sql);
>
> //Declare an empty array to store the result set in
> $array = array();
>
> //Iterate through the result set we declared earlier, looping through
> each row
> while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
> {
>      //Append the row from the database, onto the array :)
>      $array[] = $row;
>
> }
>
> and then to fill in text boxes based on the previously selected ID I
> do...
> foreach($array as $row):
>
>     <input name="txtdate" type="text" id="txtdate" value="<?php echo
> $row['date'];?>"/>
>
> SO NOW I WANT TO ALSO SELECT A DEFAULT VALUE FOR A LISTBOX BASED ON
> THE ID....
>
>   <select name="lstuser2" id="lstuser2">
>     <option value="A">A</option>
>     <option value="B">B</option>
>     <option value="C">C</option>
>   </select>
>
> So lets say that the record for this user called for the listbox to be
> set to C b/c thats whats in the DB for this person,  how could I do
> this based on the record?
>
> thanks.

I think this is a pretty standard method:

$values = array("A", "B", "C");
echo "<select ...>";
foreach ($values as $option)
{
$selected = ($row['field'] == $option) ? "selected" : "";
echo "<option value='$option' $selected>$option</option>";
}
echo "</select>";
Re: php filling in listbox value based on db record ?? [message #169403 is a reply to message #169401] Thu, 09 September 2010 15:37 Go to previous messageGo to next message
PAkerly is currently offline  PAkerly
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
On Sep 9, 10:03 am, matt <matthew.leonha...@gmail.com> wrote:
> On Sep 9, 9:21 am, PAkerly <pake...@gmail.com> wrote:
>
>
>
>> I've got an html page that fills in text boxes based on an sql
>> record.  Doing things like this:
>
>> $sql = "SELECT date, user1, user2, user3, id
>>                 FROM myuserdata
>>                 WHERE id= '$txtid'
>>                 ORDER BY id";
>
>> //Get a result set from the query (this is the array)
>> $rs = mysql_query($sql) or die(mysql_error() . "<br />Sql:" .  $sql);
>
>> //Declare an empty array to store the result set in
>> $array = array();
>
>> //Iterate through the result set we declared earlier, looping through
>> each row
>> while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
>> {
>>      //Append the row from the database, onto the array :)
>>      $array[] = $row;
>
>> }
>
>> and then to fill in text boxes based on the previously selected ID I
>> do...
>> foreach($array as $row):
>
>>     <input name="txtdate" type="text" id="txtdate" value="<?php echo
>> $row['date'];?>"/>
>
>> SO NOW I WANT TO ALSO SELECT A DEFAULT VALUE FOR A LISTBOX BASED ON
>> THE ID....
>
>>   <select name="lstuser2" id="lstuser2">
>>     <option value="A">A</option>
>>     <option value="B">B</option>
>>     <option value="C">C</option>
>>   </select>
>
>> So lets say that the record for this user called for the listbox to be
>> set to C b/c thats whats in the DB for this person,  how could I do
>> this based on the record?
>
>> thanks.
>
> I think this is a pretty standard method:
>
> $values = array("A", "B", "C");
> echo "<select ...>";
> foreach ($values as $option)
> {
>   $selected = ($row['field'] == $option) ? "selected" : "";
>   echo "<option value='$option' $selected>$option</option>";}
>
> echo "</select>";

I'm not having a lot of luck with this....
It sort of does what I want, I guess I just dont know where I need to
put it. I thought at one point I had it working well but the listbox
had 4 selections A, B, C, C - I'm guessing because C was what the
record actually had. Is this the way it should work?
Re: php filling in listbox value based on db record ?? [message #169406 is a reply to message #169403] Fri, 10 September 2010 07:38 Go to previous message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 9/9/2010 5:37 PM, PAkerly wrote:
> On Sep 9, 10:03 am, matt<matthew.leonha...@gmail.com> wrote:
>> On Sep 9, 9:21 am, PAkerly<pake...@gmail.com> wrote:
>>
>>
>>
>>> I've got an html page that fills in text boxes based on an sql
>>> record. Doing things like this:
>>
>>> $sql = "SELECT date, user1, user2, user3, id
>>> FROM myuserdata
>>> WHERE id= '$txtid'
>>> ORDER BY id";
>>
>>> //Get a result set from the query (this is the array)
>>> $rs = mysql_query($sql) or die(mysql_error() . "<br />Sql:" . $sql);
>>
>>> //Declare an empty array to store the result set in
>>> $array = array();
>>
>>> //Iterate through the result set we declared earlier, looping through
>>> each row
>>> while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
>>> {
>>> //Append the row from the database, onto the array :)
>>> $array[] = $row;
>>
>>> }
>>
>>> and then to fill in text boxes based on the previously selected ID I
>>> do...
>>> foreach($array as $row):
>>
>>> <input name="txtdate" type="text" id="txtdate" value="<?php echo
>>> $row['date'];?>"/>
>>
>>> SO NOW I WANT TO ALSO SELECT A DEFAULT VALUE FOR A LISTBOX BASED ON
>>> THE ID....
>>
>>> <select name="lstuser2" id="lstuser2">
>>> <option value="A">A</option>
>>> <option value="B">B</option>
>>> <option value="C">C</option>
>>> </select>
>>
>>> So lets say that the record for this user called for the listbox to be
>>> set to C b/c thats whats in the DB for this person, how could I do
>>> this based on the record?
>>
>>> thanks.
>>
>> I think this is a pretty standard method:
>>
>> $values = array("A", "B", "C");
>> echo "<select ...>";
>> foreach ($values as $option)
>> {
>> $selected = ($row['field'] == $option) ? "selected" : "";
>> echo "<option value='$option' $selected>$option</option>";}
>>
>> echo "</select>";
>
> I'm not having a lot of luck with this....
> It sort of does what I want, I guess I just dont know where I need to
> put it.

Hi,

You should put it here somewhere:

<select name="lstuser2" id="lstuser2">
--> around these options
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>

</select>


> I thought at one point I had it working well but the listbox
> had 4 selections A, B, C, C - I'm guessing because C was what the
> record actually had. Is this the way it should work?

Are you saying that you have 4 options in your selectbox, and two are
the same? Sounds like a mistake in your PHP code or in your SQL query.

Maybe you should post the code that produces the SELECT in here.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Eregi possible problem
Next Topic: My head is spinning
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 13:25:34 GMT 2024

Total time taken to generate the page: 0.02816 seconds