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.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Default Value for PHP Drop Down List Populated By MySQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-08-10, 21:25
Diesel Dud Diesel Dud is offline
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
Reply With Quote
  #2 (permalink)  
Old 08-09-10, 05:21
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
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
Reply With Quote
  #3 (permalink)  
Old 08-09-10, 10:49
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
Quote:
Originally Posted by healdem View Post
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
Reply With Quote
  #4 (permalink)  
Old 08-09-10, 13:25
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
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>";

Reply With Quote
  #5 (permalink)  
Old 08-09-10, 14:18
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
Quote:
Originally Posted by futurity View Post
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!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On