Populate select drop down from a database table

前端 未结 4 1746
迷失自我
迷失自我 2020-12-06 13:42

I have a table (\"venues\") that stores all the possible venues a volunteer can work, each volunteer is assigned to work one venue each.

I want to create a select dr

相关标签:
4条回答
  • 2020-12-06 14:03
    <!DOCTYPE html>
    <html>
    <head>
        <title>table binding</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    
    </head>
    <body>
        <div id="mydiv" style="width:100px;height:100px;background-color:yellow">
    
            <select id="myselect"></select>
        </div>
    
    </body>
    </html>
    
    
    <?php
    include('dbconnection.php');
    
    $sql = "SHOW TABLES FROM $dbname";
    $result = mysqli_query($conn,$sql);
    
    if (!$result) {
        echo "DB Error, could not list tables\n";
        echo 'MySQL Error: ' . mysqli_error();
        exit;
    }
    
    while ($row = mysqli_fetch_row($result)) {
        echo "<script>
        var z = document.createElement('option');
        z.setAttribute('value', '".$row[0]."');
        var t = document.createTextNode('".$row[0]."');
        z.appendChild(t);
        document.getElementById('myselect').appendChild(z);</script>";
    
    }
    
    
    
    ?>
    
    0 讨论(0)
  • 2020-12-06 14:18

    assuming you have an array of venues...personally i don't like to mix the sql with other wizardry.

    function displayDropDown($items, $name, $label, $default='') {
      if (count($items)) {
        echo '<select name="' . $name . '">';
        echo '<option value="">' . $label . '</option>';
        echo '<option value="">----------</option>';
        foreach($items as $item) {
          $selected = ($item['id'] == $default) ? ' selected="selected" : '';
          echo <option value="' . $item['id'] . '"' . $selected . '>' . $item['name'] . '</option>';
        }
        echo '</select>';
      } else {
        echo 'There are no venues';
      }
    }
    
    0 讨论(0)
  • 2020-12-06 14:18
            <?php 
            $query = "SELECT * from blogcategory";
            //$res = mysql_query($query);
            $rows = $db->query($query);
            echo "<select name = 'venue'>";
            // while (($row = mysql_fetch_row($res)) != null)
            while ($record = $db->fetch_array($rows)) 
            {
                echo "<option value = '{$record['CategoryId']}'";
                if ($CategoryId == $record['CategoryId'])
                    echo "selected = 'selected'";
                echo ">{$record['CategoryName']}</option>";
            }
            echo "</select>";
            ?>
    
    0 讨论(0)
  • 2020-12-06 14:29
    $query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";
    
    $res = mysql_query($query);
    echo "<select name = 'venue'>";
    while (($row = mysql_fetch_row($res)) != null)
    {
        echo "<option value = '{$row['venue_id']}'";
        if ($selected_venue_id == $row['venue_id'])
            echo "selected = 'selected'";
        echo ">{$row['venue_name']}</option>";
    }
    echo "</select>";
    

    :)

    0 讨论(0)
提交回复
热议问题