Foreach php函数HTML选择

时间:2015-04-10 07:03:08

标签: php html foreach

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

    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> '; 

我知道有类似的问题,我读过Foreach php function inside HTML select options但仍然无效:(

2 个答案:

答案 0 :(得分:1)

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> '; 

你没有正确关闭标签

答案 1 :(得分:0)

尝试 -

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