Jump to content

Recommended Posts

Hello,

 

Ive found this code by google

 

			<select name="user_position" id="user_position" class='textbox'>
				<?php 
				foreach($user_position as $key => $value) {
						if($_SESSION['user_position'] < $key) {
							$selected = '';
							if($key == $row['user_position']) {
								$selected = ' selected="selected"';
							}
							echo('<option value="'.$key.'"'.$selected.'>'.$value.'</option>');
						}		
					}
				?> 
			</select> </td>

 

But i cant make it work, i want im going to edit a user profile to 'auto' select the existing choice i have on the database ..

Any help please?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/
Share on other sites

There's a few things.  You don't need the if($_SESSION['user_position'] < $key

You don't need the parenthesis around your echo. And if you're going to concatenate the variables/ html for output your quotes aren't right.

<select name="user_position" id="user_position" class='textbox'>
               <select name="user_position" id="user_position" class='textbox'>
               <?php 
               foreach($user_position as $key => $value) {
                        $selected = '';
                        if($key == $row['user_position']) {
                          $selected = "selected='selected'";
                        echo "<option value='".$key."'".$selected.">".$value."</option>";
                     }      
                  }
               ?>
            </select> </td>
              
           

$user_position = array (
	1 => 'test drop-down 1',
	2 => 'test drop-down 2',
	3 => 'test drop-down 3'
);

Edided code:

<select name="user_position" id="user_position" class='textbox'>
               <select name="user_position" id="user_position" class='textbox'>
               <?php 
               foreach($user_position as $key => $value) {
                        $selected = '';
                        if($key == $row['user_position']) {
                          $selected = "selected='selected'";
                        echo "<option value='".$key."'".$selected.">".$value."</option>";
                     }      
                  }
               ?>
            </select> </td>

 

If i use the above fixed code its not even has a drop down list.. :(

Because I screwed up and had a bracket out of place try this


               foreach($user_position as $key => $value) {
                        $selected = '';
                        if($key == $row['user_position']) {
                          $selected = "selected='selected'";
                        }
                        echo "<option value='".$key."'".$selected.">".$value."</option>";   
                  }

also that tells me that $key never equals $row['user_position']  otherwise you would have had a single option. The default one.

try this.  Copy and paste the results.

<!--<select name="user_position" id="user_position" class='textbox'>-->
               <?php 
               foreach($user_position as $key => $value) {
                echo "key: ".$key.", value: ".$value.",   user_position:".$row['user_position']."<br>"
                        $selected = '';
                        if($key == $row['user_position']) {
                           $selected = ' selected="selected"';
                        }
                        <!--echo '<option value='".$key."'".$selected.">".$value."</option>-->";   
                  }
               ?>
            <!--</select>--> </td>

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';'  (line 52)

 

<!--<select name="user_position" id="user_position" class='textbox'>-->
               <?php 
               foreach($user_position as $key => $value) {
                echo "key: ".$key.", value: ".$value.",   user_position:".$row['user_position']."<br>"
                        $selected = ''; <--------------------------------
                        if($key == $row['user_position']) {
                           $selected = ' selected="selected"';
                        }
                        <!--echo '<option value='".$key."".$selected.">".$value."</option>-->";   
                  }
               ?>
            <!--</select>--> </td>

Parse error: syntax error, unexpected '<' in (line 56)

 

<!--<select name="user_position" id="user_position" class='textbox'>-->
               <?php 
               foreach($user_position as $key => $value) {
echo "key: ".$key.", value: ".$value.",   user_position:".$row['user_position']."<br>";
                         $selected = '';
                        if($key == $row['user_position']) {
                           $selected = ' selected="selected"';
                        }
                        <!--echo '<option value='".$key."".$selected.">".$value."</option>-->";   <-----------------------------
                  }
               ?>
            <!--</select>--> </td>

I guess I wasn't at the top of my game when I posted that.  :-\

  <?php 
               foreach($user_position as $key => $value) {
echo "key: ".$key.", value: ".$value.",   user_position:".$row['user_position']."<br>";
                         $selected = '';
                        if($key == $row['user_position']) {
                           $selected = ' selected="selected"';
                        }
                        // echo '<option value='".$key."'".$selected.">".$value."</option>";   
                  }
               ?>

there was also a single apostrophe missing after $key in the same line.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.