Reply
[How to] echo 'selected' in OPTION value ?
Old 04-15-2008, 03:16 PM [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Hi,

In Edit page...
How could I echo "selected" on OPTION witch in MySQL row table ?
In this code:
PHP Code:
 $sql=mysql_query("SELECT * FROM `table` ORDER BY `id` ASC") or die(mysql_error());
   while(
$cc=mysql_fetch_array($sql)){
   echo 
"<option value='".$cc["id"]."'>".$cc["name"]."</option>
"
;
                                        } 
Started link: http://www.webmaster-talk.com/php-fo...d-one-row.html

Many Thanks in Advance,
A. Mansouri
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
When You Register, These Ads Go Away!
Old 04-15-2008, 04:25 PM Re: [How to] echo 'selected' in OPTION value ?
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Simply by adding it in the echo:
PHP Code:
$sql=mysql_query("SELECT * FROM `table` ORDER BY `id` ASC") or die(mysql_error());
while(
$cc=mysql_fetch_array($sql)){
   
$selected=($cc['selected']==1)?'selected="true"':"";
   echo 
"<option $selected value='".$cc["id"]."'>".$cc["name"]."</option>";

__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 04-15-2008 at 04:29 PM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 04-15-2008, 06:01 PM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Thanks for tring help me..

But It's not that simple as you think, it's Multible data in one row,
I mean like if you have `main` D.B table contain `Auth` row, for example thats row:
$Auth = $auth['1,8,12,46'];
I want the `table` (The Example code in my First Post) with `id` = 1,8,12, And 45 echo selected.

That's It!

Many Thanks
A. Mansouri
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-15-2008, 07:13 PM Re: [How to] echo 'selected' in OPTION value ?
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Well, it's a simple you stated it. You need to stretch what I explained you to do just what you said:
PHP Code:
$sql=mysql_query("SELECT * FROM `table` ORDER BY `id` ASC") or die(mysql_error());
while(
$cc=mysql_fetch_array($sql)){
  switch(
$cc['id']){
    case 
1:
    case 
8:
    case 
12:
    case 
46:
      
$sel='selected="true"';
      break;
    default:
      
$sel="";
      break;
  }  
  echo 
"<option $sel value='".$cc["id"]."'>".$cc["name"]."</option>";

__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 04-16-2008, 07:43 AM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Hello,

1,8,12, and 46 examples of just coming from database row.
It is not fixed but depends on the `Auth` row of users table.

It can't code static 'case' `id`.

Regards,
Abdulrahman Mansouri
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-16-2008, 07:55 AM Re: [How to] echo 'selected' in OPTION value ?
Super Spam Talker

Posts: 882
ISSC, I think you need to explain exactly what you are trying to select ... I'd like to help but am a bit confused.

Quote:
I want the `table` (The Example code in my First Post) with `id` = 1,8,12, And 45 echo selected.
If your return all those rows into a dropdown, you cant select them all - dropdowns only let you select one... bit confused :/
Sir P is offline
Reply With Quote
View Public Profile
 
Old 04-16-2008, 08:07 AM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Thanks Sir_P for your tring help me...

In:
<select size='10' name='Auth[]' class='drop' multiple='multiple'>
<OPTION....
</select>

You can select multiple data by Hold Ctrl + Click what you want...

I hope this make more specific explain..

Regards,
A. Mansouri
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-16-2008, 09:05 AM Re: [How to] echo 'selected' in OPTION value ?
Super Spam Talker

Posts: 882
Oh yes of course.. ok next bit ..

Quote:
1,8,12, and 46 examples of just coming from database row.
It is not fixed but depends on the `Auth` row of users table.
You need to have some pattern to your data... all the rows you need to show selected either need to be known (id for example) or have a common feature, maybe an extra coloum that says 'selected' 1 or 0.. and then when your printing your <option> youd just use <option if($selected==1){echo(" selected");}</option> .. that kind of idea
Sir P is offline
Reply With Quote
View Public Profile
 
Old 04-16-2008, 12:49 PM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
I can't Add separate row in DB for each `Auth`
Simply because the Auth is coming from new DB.
So, we have to look for other solution.

Regards.
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-16-2008, 01:28 PM Re: [How to] echo 'selected' in OPTION value ?
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Quote:
because the Auth is coming from new DB
I really don't see where the problem is !?

You make 1 select to get the <option> values, and another select on another db to get the auth id's.

But are you sure it comes from another db, or does it comes from another table in the same db ?
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 04-16-2008, 04:04 PM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Sorry, I mean another table in same D.B
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-17-2008, 08:52 AM Re: [How to] echo 'selected' in OPTION value ?
Super Spam Talker

Posts: 882
It doesnt really matter where the auth ID is stored, whatever you do you need to have some way of knowing which ID's should be selected...

if you dont know which rows need to be selected in the list, how can it work?
Sir P is offline
Reply With Quote
View Public Profile
 
Old 04-17-2008, 12:26 PM Re: [How to] echo 'selected' in OPTION value ?
Super Talker

Posts: 122
I do this quite a bit. This is what I do.....well I'll just post some code..


PHP Code:
<select name="dept" cols = 19>
                    <option value="N/A" selected>N/A</option>
                    <option value="Server" <?php if($_SESSION['position'] == "Server") { echo 'selected = "selected"'; } ?>>Servers</option>
                    <option value="Break Service Crew" <?php if($_SESSION['position'] == "Break Service Crew") { echo 'selected = "selected"'; } ?>>Break Service Crew</option>
                    <option value="Set-up Crew" <?php if($_SESSION['position'] == "Set-up Crew") { echo 'selected = "selected"'; } ?>>Set-up Crew</option>
                    <option value="bqt" disabled>--Bqt Tech Service--</option>
                    <option value="Tech Service" <?php if($_SESSION['position'] == "Tech Service") { echo 'selected = "selected"'; } ?>>Tech Service</option>
                    <option value="Set-up Tech Service" <?php if($_SESSION['position'] == "Set-up Tech Service") { echo 'selected = "selected"'; } ?>>Set-up Tech Service</option>
                    <option value="Captain" <?php if($_SESSION['position'] == "Captain") { echo 'selected = "selected"'; } ?>>Captain</option>
                    <option value="Intern" <?php if($_SESSION['position'] == "Intern") { echo 'selected = "selected"'; } ?>>Intern</option>
                    <option value="Higher Ups" disabled>--Higher Ups--</option>
                    <option name="Manager" <?php if($_SESSION['position'] == "Manager") { echo 'selected = "selected"'; } ?>>Manager</option>
                    <option name="Event Supervisor" <?php if($_SESSION['position'] == "Event Supervisor") { echo 'selected = "selected"'; } ?>>Event Supervisor</option>
                    <option name="Set-up Crew Supervisor" <?php if($_SESSION['position'] == "Set-up Crew Supervisor") { echo 'selected = "selected"'; } ?>>Set-up Crew Supervisor</option>
                    </select>

I would try something like that.
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 04-19-2008, 03:25 PM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 63
Name: A. Mansouri
Location: Jeddah, K.S.A
Till now this is the only solution I have:
PHP Code:
$ComAuth "1,3,5,14";
 
$co_sql1=mysql_query("SELECT * FROM `company` WHERE `id` IN (".$ComAuth.") ORDER BY `id` ASC") or die(mysql_error());
   while(
$co1=mysql_fetch_array($co_sql1)){
   echo 
"<option selected value='".$co1["id"]."'>".$co1["name"]."</option>
"
;
                                          }

 
$co_sql2=mysql_query("SELECT * FROM `company` WHERE `id` NOT IN (".$ComAuth.") ORDER BY `id` ASC") or die(mysql_error());
   while(
$co2=mysql_fetch_array($co_sql2)){
   echo 
"<option value='".$co2["id"]."'>".$co2["name"]."</option>
"
;
                                          } 
Creating 2 db one for selected one.. and another one for none selected one.

But this sortin all selected db in Top, then it will sort the none selected (Second db).

Can any help from here ?

Regards,
A. Mansouri
ISSC is offline
Reply With Quote
View Public Profile Visit ISSC's homepage!
 
Old 04-20-2008, 01:05 PM Re: [How to] echo 'selected' in OPTION value ?
Ultra Talker

Posts: 408
A cop-out method of doing it is just to echo the selected row twice. Once at the beginning, because it was selected, and then normally echo out all the options after.
Lucas3677 is offline
Reply With Quote
View Public Profile Visit Lucas3677's homepage!
 
Reply     « Reply to [How to] echo 'selected' in OPTION value ?
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS&n