drop down selection with PHP

Discussion in 'PHP' started by Kyriakos, Apr 21, 2009.

  1. #1
    Hi,

    i want to select with php the category of a record that i want to display when i update it. Like this

    [​IMG]


    this is my PHP code:
    <select name="cid" class="input" style="padding:2px;">
    
    <option value="<?php echo $row['cid']; ?>" selected>
    
    <?php echo $row['cid'];?></option>
    
    <?php $result2 = mysql_query("SELECT * FROM menu ORDER BY id");
    
    while($row2 = mysql_fetch_assoc($result2)) { ?>
    
    <option value="<?php echo $row2['id'];?>">
    
    <?php echo $row2['id'].". ".$row2['title'];?></option>
    
    <?php } ?></select>
    PHP:
    i hope that you can help me. thanks in advance
     
    Kyriakos, Apr 21, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I'm a little unsure as to what you are asking for - do you want the information in the whole form to update when you change selection? If so, you can't do that with PHP alone - then you'd need to look to javascript.

    The selected record will, as far as I can see, affect which row in the database would be updated with the values entered in the form, if used as is. But I think you mean that you want the form to update the displayed information based on the select box?

    If there is something else you want to do, please tell us in more detail.
     
    PoPSiCLe, Apr 21, 2009 IP
  3. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #3
    try this
    
    <select name="cid" class="input" style="padding:2px;">
    <?php $result2 = mysql_query("SELECT * FROM menu ORDER BY id");
    
    while($row2 = mysql_fetch_assoc($result2)) {
    	$selected = ($row['cid'] != $row2['id']) ? ' selected' : '';
    ?>
    
    <option value="<?php echo $row2['id'];?>" <?php echo $selected; ?>>
    
    <?php echo $row2['id'].". ".$row2['title'];?></option>
    
    <?php } ?></select>
    
    Code (markup):
     
    misbah, Apr 21, 2009 IP