Jump to content

select from drop down menu, db existant...


runnerjp

Recommended Posts

humm been awhile since iv done php and need refreshing..

 

<?php $lines = file('runningclubs.txt');
echo '<select  id="club" name="club">';
foreach($lines as $line) {
    echo '<option>'.$line.'</option>';
}
echo '</select>';?>

 

which this how could i use

<?php echo $getuserprofile['club']?>

for the users allready selected club to be allready selected?

Link to comment
https://forums.phpfreaks.com/topic/142486-select-from-drop-down-menu-db-existant/
Share on other sites

<?php 
$lines = file('runningclubs.txt');
echo '<select  id="club" name="club">';
foreach($lines as $line) {
    if ($line == $getuserprofile['club']) 
          $line = 'value="' . $line . '" SELECTED';
    else
          $line = 'value="' . $line . '"';
    echo '<option ' . $line . '>'.$line.'</option>';
}
echo '</select>';
?>

 

Should do it, given that the club index of the getuserprofile should be the line you want to be selected.

oh ek..prmiso ... it now shows me

 

value="(the value from the text file)" eg. value="1"

 

 

instead of just 1

 

I am an idiot, sorry about that.

<?php 
$lines = file('runningclubs.txt');
echo '<select  id="club" name="club">';
foreach($lines as $line) {
    $rline = $line;
    if ($line == $getuserprofile['club']) 
          $line = 'value="' . $line . '" SELECTED';
    else
          $line = 'value="' . $line . '"';
    echo '<option ' . $line . '>'.$rline.'</option>';
}
echo '</select>';
?>

 

Should work.

sadly didnt select the one from my db :S

 

Then $line and $getuserprofile['club'] have seemingly different values. If you want it to work they need to be exactly the same.

 

Maybe print them both out for debugging and see where there is a difference, vardump may help also to verify each type also.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.