drop down box from database

Discussion in 'PHP' started by tisucu, Apr 28, 2010.

  1. #1
    <?php
    
    include 'config.php';
    include 'opendb.php';
    
    $sql="SELECT id, name FROM Sites";
    $result=mysql_query($sql);
    
    $options="";
    
    while ($row=mysql_fetch_array($result)) {
    
        $id=$row["id"];
        $name=$row["name"];
        $options.="<OPTION VALUE=\"$id\">".$name;
    }
    ?>
    
    <html>
    <body>
    
    <form action="update.php" method="post">
    
    <SELECT NAME=id>
    <OPTION VALUE=$id>Choose
    <?=$options?>
    </SELECT>
    <input type="submit" value="Submit" />
    </form>
    
    </body>
    </html>
    Code (markup):
    $options.="<OPTION VALUE=\"$id\">".$name;

    need explanation of this line
     
    Last edited: Apr 28, 2010
    tisucu, Apr 28, 2010 IP
  2. majin22

    majin22 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I believe it should be:

    
    <?php
    
    include 'config.php';
    include 'opendb.php';
    
    $sql="SELECT id, name FROM Sites";
    $result=mysql_query($sql);
    
    $options="";
    
    while ($row=mysql_fetch_array($result)) {
        $id=$row["id"];
        $name=$row["name"];
        $options.="<OPTION VALUE=\"$id\">".$name."</option>";
    }
    ?>
    
    <html>
    <body>
    
    <form action="update.php" method="post">
    
    <SELECT NAME=id>
    <OPTION VALUE=$id>Choose
    <?=$options?>
    </SELECT>
    <input type="submit" value="Submit" />
    </form>
    
    </body>
    </html>
    
    Code (markup):
    this code $options.="<OPTION VALUE=\"$id\">".$name."</option>";

    put all the values found to the <option></option> tag

    .= is a shortcut to to

    $options = $options."<OPTION VALUE=\"$id\">".$name."</option>";
    Code (markup):
    hope this helps
     
    majin22, Apr 28, 2010 IP
  3. webria

    webria Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $options.="<OPTION VALUE=\"$id\">".$name."</OPTION>";
    You should have a closing option tag for each of the options. It should work fine, if rest of the code runs well.
     
    webria, Apr 29, 2010 IP
  4. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #4
    it is called concat equals, meaning string variable '$options' can be elongated with more following '$options' variable, without the .= symbol, the variable will be overwritten with the last $options initialized.
     
    bartolay13, Apr 29, 2010 IP