Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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!
 
 
Register now for full access!
Old 04-15-2008, 05:25 PM Re: [How to] echo 'selected' in OPTION value ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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>";

__________________
Only a biker knows why a dog sticks his head out the window.

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

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 08:13 PM Re: [How to] echo 'selected' in OPTION value ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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>";

__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-16-2008, 08:43 AM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 08:55 AM Re: [How to] echo 'selected' in OPTION value ?
Super Spam Talker

Latest Blog Post:
PSD Squirrel Launched
Posts: 933
Trades: 7
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 :/
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Sir P is offline
Reply With Quote
View Public Profile Visit Sir P's homepage!
 
Old 04-16-2008, 09:07 AM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 10:05 AM Re: [How to] echo 'selected' in OPTION value ?
Super Spam Talker

Latest Blog Post:
PSD Squirrel Launched
Posts: 933
Trades: 7
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
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Sir P is offline
Reply With Quote
View Public Profile Visit Sir P's homepage!
 
Old 04-16-2008, 01:49 PM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 02:28 PM Re: [How to] echo 'selected' in OPTION value ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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 ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-16-2008, 05:04 PM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 09:52 AM Re: [How to] echo 'selected' in OPTION value ?
Super Spam Talker

Latest Blog Post:
PSD Squirrel Launched
Posts: 933
Trades: 7
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?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Sir P is offline
Reply With Quote
View Public Profile Visit Sir P's homepage!
 
Old 04-17-2008, 01:26 PM Re: [How to] echo 'selected' in OPTION value ?
Extreme Talker

Posts: 177
Trades: 0
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, 04:25 PM Re: [How to] echo 'selected' in OPTION value ?
ISSC's Avatar
Skilled Talker

Posts: 65
Name: A. Mansouri
Location: Jeddah, K.S.A
Trades: 0
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, 02:05 PM Re: [How to] echo 'selected' in OPTION value ?
Ultra Talker

Posts: 407
Trades: 1
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.
__________________
[
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
]
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 Search this Thread
Search this Thread:

Advanced Search

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 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   XML



Page generated in 0.46329 seconds with 12 queries