Jump to content


Photo

<option selected>meh!</option>


  • Please log in to reply
11 replies to this topic

#1 Detail

Detail

    King Detail

  • Hosted
  • 7,767 posts
  • Location:Dayonic
  • Projects:Dayvi.com
  •  Blu Spy

Posted 22 December 2005 - 11:08 AM

I have:
<select name=group2 size=1>
        <option selected>Member</option>
        <option>Beta Tester</option>
        <option>3D Modeler</option>
        <option>Sound Technician</option>
        <option>Artist</option>
        <option>Webmaster</option>
        <option>Coder</option>
        <option>Mapper</option>
        <option>Skinner</option>
        <option>Story Writer</option>
     	 </select>
and i want the bit saying selected to move depending on what the person set this too last time they changed their profile. How do i do this?

#2 Athena

Athena

    Embody the Truth

  • Undead
  • 6,946 posts
  •  Former Community Leader

Posted 22 December 2005 - 11:13 AM

Perhaps you can add/use a php variable, which contains the option from their profile from last time (from the database). Then you can check what its value is and change the menu accordingly. I don't know that much about how your profiles are set up in the database, but it might work.

#3 Detail

Detail

    King Detail

  • Hosted
  • 7,767 posts
  • Location:Dayonic
  • Projects:Dayvi.com
  •  Blu Spy

Posted 22 December 2005 - 11:16 AM

In the database it's:
-----------
| Group2 |
-----------
| Coder |
-----------

Simple as that.

$profile['group2'] = "Coder";

#4 Athena

Athena

    Embody the Truth

  • Undead
  • 6,946 posts
  •  Former Community Leader

Posted 22 December 2005 - 11:21 AM

Hmm well then you could connect to the database and retrieve that value
$job = $profile['group2'] from that specific member after connecting with the database, something similar you did in the admin_form for the Hive, I think.

And then perhaps

if ($job == 'Coder')
print "<option >Member</option>
       <option>Beta Tester</option>
       <option>3D Modeler</option>
       <option>Sound Technician</option>
       <option>Artist</option>
       <option>Webmaster</option>
       <option selected>Coder</option>
       <option>Mapper</option>
       <option>Skinner</option>
       <option>Story Writer</option>";
Though I think a switch might be better in this case.
Remember though I'm still learning this stuff, so I don't know for sure if it'll work but it's worth a try I think. ;)

#5 Guest_ImmoMan_*

Guest_ImmoMan_*
  • Guests

Posted 22 December 2005 - 11:53 AM

What Blaat did was right, but the way you stored values is kinda inefficient. What you should really do is store the jobs as numeric indexes rather than as strings. That would allow you to iterate over all possible groups with a simple for-loop, and include 'selected' in the tag when the current value of the loop's counter matches the number in the database. ;)

#6 Detail

Detail

    King Detail

  • Hosted
  • 7,767 posts
  • Location:Dayonic
  • Projects:Dayvi.com
  •  Blu Spy

Posted 22 December 2005 - 12:56 PM

Blaat85, Your way i'd need to have 10 sets of that list. I'd also need to add another set each time i added an item to the list.

ImmoMan, and you never make any sense.


*Hoping that DCoder DeathRay or Blodo comes along...*

#7 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 22 December 2005 - 02:06 PM

Immoman is quite right.
You should make an array of jobs, store the index of the selected job in the DB and do the index > text conversions in PHP.

Additionally, I suggest abstracting the form elements (and any other html blocks you use repetitively) into custom functions like $html->form_dropdown() and similar, so you have a constant html output structure throughout your project. I use such a helper class in my projects, simplifies the work a lot. (Or if you'd rather use premade classes instead of making your own, check out PEAR::QuickForm.)

With only a numeric index stored in the database, php can build such a dropdown pretty easily:

<?php
/*
 generic dropdown building function
*/
function form_dropdown($name, $label='', $values = array(), $size = 1, $selindex = 0)
 {
  if (count($values) == 0) return '';
  $ind = 0;
  $ret = "<label for='$name'>$label\n<select id='$name' name='$name' size='$size'>\n";
  foreach ($values as $k => $v)
  {
   $sel = ($ind == $selindex) ? ' selected=\'selected\'' : '';
   $ind++;
   $ret .= "<option value='$k'$sel>$v</option>\n";
  }

  $ret .= '</select>\n</label>\n';
  return $ret;
 }

/*
 building an array of jobs, etc
*/
 $jobs = array
 (
 'Member',
 'Beta Tester',
 '3D Modeler',
 'Sound Technician',
 'Artist',
 'Webmaster',
 'Coder',
 'Mapper',
 'Skinner',
 'Story Writer'
 );

 $index = 1; // this will determine the index of a selected item. The first item in the dropdown is 0, the second one is 1, etc. You will want to set this to the value you got from the DB, most likely.

 echo form_dropdown('job', 'Select your job:', $jobs, 1, $index);
?>

And after they submit this form, you can just store intval($_REQUEST['job']) into the DB.
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#8 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 26 December 2005 - 12:06 AM

Yep, ImmoMan and DCoder have it right, but it can be done the way you have it set up as well.

Note, this is untested code...
//What the job is.
$job = $profile['group2'];

//Turn it into an option.
$option = '<option>' . $job . '</option>';

//Turn it into a selected option.
$selected = '<option selected>' . $job . '</option>';

//Make an array of all possible jobs, as options.
$selections = array(
'<option>Member</option>',
'<option>Beta Tester</option>',
'<option>3D Modeler</option>',
'<option>Sound Technician</option>',
'<option>Artist</option>',
'<option>Webmaster</option>',
'<option>Coder</option>',
'<option>Mapper</option>',
'<option>Skinner</option>',
'<option>Story Writer</option>'
);

//Count them.
$numselections = count($selections);

//Display them. If the current indice has a value of the job as an option, it will be selected, otherwise it will simply be displayed as an option.
for($i=0; $i<$numselections; $i++){
if($selections[$i] == $option)
echo $selected;
else
echo $selections[$i];
}
I think that should work.

Edited by DeathRay2K, 26 December 2005 - 03:30 AM.


#9 Blodo

Blodo

    The one who disagrees

  • Project Team
  • 3,002 posts
  • Location:Eastern Europe
  • Projects:siteMeister, Mental Omega
  •  The wise guy

Posted 26 December 2005 - 01:39 AM

Guess I'll add my bit to the box as well :lol:

// I'm gonna use a similar way to d2k although the code
// could be even simpler than that

// Declare vars
$job = $profile['group2'];
$arr_jobs_output = array();

// Make the array with all the jobs names
$arr_jobs = array('Member', 'Beta Tester', '3D Modeler', 'Sound Technician', 'Artist', 'Webmaster', 'Coder', 'Mapper', 'Skinner', 'Story Writer');

// Use foreach
foreach ($arr_jobs as $v) {

// This checks whether the current array value processed matches the job variable
// The rest is kind of self explanatory
if ($v == $job) {
$foo = '<option>';
} else {
$foo = '<option selected>';
}

$arr_jobs_output[] = $foo . $v . '</option>';

}
Styles are easily changed from the foo variable, instead of modifying an entire array.

ARGUMENT FROM CREATION, a.k.a. ARGUMENT FROM PERSONAL INCREDULITY (I)
(1) If evolution is false, then creationism is true, and therefore God exists.
(2) Evolution can't be true, since I lack the mental capacity to understand it; moreover, to accept its truth would cause me to be uncomfortable.
(3) Therefore, God exists.


#10 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 26 December 2005 - 05:04 AM

Doh! I didn't think of foreach. :lol:

#11 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 26 December 2005 - 09:05 AM

Just for the record, W3C validator requires full attributes, like selected='selected' and checked='checked' , not just selected or checked :lol:
http://www.w3.org/TR/xhtml1/#h-4.5
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#12 Blodo

Blodo

    The one who disagrees

  • Project Team
  • 3,002 posts
  • Location:Eastern Europe
  • Projects:siteMeister, Mental Omega
  •  The wise guy

Posted 26 December 2005 - 06:25 PM

Only if you use the xhtml specification. If you validate html4 the parser should ignore the single statements.

ARGUMENT FROM CREATION, a.k.a. ARGUMENT FROM PERSONAL INCREDULITY (I)
(1) If evolution is false, then creationism is true, and therefore God exists.
(2) Evolution can't be true, since I lack the mental capacity to understand it; moreover, to accept its truth would cause me to be uncomfortable.
(3) Therefore, God exists.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users