Jump to content

Editing drop down list with PHP-Mysql


mpempas

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.

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.