对于每个 php 函数 HTML 选择



我正在尝试使用 html 进行选择下拉列表,并且值必须是数据库中的内容,选择在 PHP 函数中,由于某种原因我的 foreach 循环无法正常工作,当我单击选择时,值为空,->我做错了什么:)

    function getWork() {
echo date ( 'l, F j, Y', strtotime ( 'friday + 1 weekdays' ) ) . "n";
    echo '<h1><a href="dashboard.php">Het terras</a> &rsaquo; <a href="dashboard.php?app=users">Roosters</a> &rsaquo; <a href="dashboard.php?app=users&action=new">setup</a></h1>';
    echo'<p>Selecteer de persoon waarvan u de data wilt aanmaken</p>'; 
    /*Foreach loop */ 
        $connection = mysqlConnect();
 // Find out how many items are in the table
    $total = $connection->query('SELECT COUNT(*) FROM intranet_users')->fetchColumn();
    // How many items to list per page
    $limit = 20;
    // How many pages will there be
    $pages = ceil($total / $limit);
    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));
    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;
    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);
    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?app=users&page=1" title="Eerste pagina">&laquo;</a> <a href="?app=users&page=' . ($page - 1) . '" title="Vorige pagina">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';
    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?app=users&page=' . ($page + 1) . '" title="Volgende pagina">&rsaquo;</a> <a href="?app=users&page=' . $pages . '" title="Laaste pagina">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';
    // Prepare the paged query
    $stmt = $connection->prepare('SELECT * FROM intranet_users');
    // Bind the query params
    $stmt->bindParam(':limit', $limit, PDO:: PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO:: PARAM_INT);
    $stmt->execute();
    // Do we have any results?
    if ($stmt->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);
        // Display the results  
    echo '<p><form method="post" action="foreach2.php">';
    echo '<select name="pty_select" >';
        foreach ($iterator as $row){
            echo '<option value="';
            echo $row['firstname'];
            echo '</option>"';  
        } 
     echo '   </select></p> '; 
    }   
    /*Einde loop */
    echo '
<input type="text" id="datepicker" name="datepicker" placeholder="Selecteer uw Datum"> <br/>
<input type="text" id="tijd" name="#" placeholder="Selecteer uw begijn tijd"> <br/>
<input type="submit" value="Save">
</form>';
}

我认为问题出在这里的某个地方:

// Display the results  
        echo '<p><form method="post" action="foreach2.php">';
        echo '<select name="pty_select" >';
            foreach ($iterator as $row){
                echo '<option value="';
                echo $row['firstname'];
                echo '</option>"';  
            } 
         echo '   </select></p> '; 

我知道有类似的问题,我已经阅读了HTML选择选项中的Foreach php函数,但仍然无法:(

echo '<p><form method="post" action="foreach2.php">';
    echo '<select name="pty_select" >';
        foreach ($iterator as $row){
            echo '<option value="'.$row['firstname'].'">';
            echo $row['firstname'];
            echo '</option>"';  
        } 
     echo '   </select></p> '; 

您没有正确关闭代码

尝试使用 -

        foreach ($iterator as $row){
            echo '<option value="';
            echo $row['firstname'];
            echo '">'.$row['firstname'].'</option>';  
        }

最新更新