| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

08-08-10, 22:25
|
|
Registered User
|
|
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
|
|
|
Default Value for PHP Drop Down List Populated By MySQL
|
|
Hi,
I am in the process of creating a generic drop down list function.
Its values will be read from a MySQL database table.
My issue is with the default value.
I was hoping to have my table designed with a boolean column to determine the default record (default=1, all others set to 0). How can I implement this in my PHP code without doing 2 queries on the table and ultimately getting my default value as the 1st item in the drop down list? i.e. I would like my drop down list item order to be conserved regardless of what value I set as the default.
Thanks
|
|

08-09-10, 06:21
|
|
Jaded Developer
|
|
Join Date: Nov 2004
Location: out on a limb
Posts: 8,768
|
|
use an order by that includes the use first flag
eg
select my,column,list from mytable
order by isdefault DESC, my, sort, order ASC
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
|
|

08-09-10, 11:49
|
|
Registered User
|
|
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
|
|
|
|
Quote:
Originally Posted by healdem
use an order by that includes the use first flag
eg
select my,column,list from mytable
order by isdefault DESC, my, sort, order ASC
|
select my,
column,
list
from mytable
order by isdefault DESC,
my,
sort,
order ASC
I'm not sure I understand the explanation.
Just to clarify, if I had a list:
A,B,C,D
and B is default, I would like to maintain the order:
A,B,C,D
rather than have:
B,A,B,C
Thanks again
|
|

08-09-10, 14:25
|
|
Registered User
|
|
Join Date: May 2008
Posts: 239
|
|
Select your option data as normal:
Code:
select
value,
is_default
from my_table
order by value
Now in your php code:
PHP Code:
foreach ($rows as $row) {
if (isset($user_input)) {
$selected = $user_input == $row['value'] ? ' selected="selected"' : '';
}
else {
$selected = $row['is_default'] ? ' selected="selected"' : '';
}
echo "<option value=\"$row[value]\"$selected>$row[value]</option>";
}
|
|

08-09-10, 15:18
|
|
Registered User
|
|
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
|
|
Quote:
Originally Posted by futurity
Select your option data as normal:
Code:
select
value,
is_default
from my_table
order by value
Now in your php code:
PHP Code:
foreach ($rows as $row) {
if (isset($user_input)) {
$selected = $user_input == $row['value'] ? ' selected="selected"' : '';
}
else {
$selected = $row['is_default'] ? ' selected="selected"' : '';
}
echo "<option value=\"$row[value]\"$selected>$row[value]</option>";
}
|
awesome, works great.. thanks!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|