0

I am trying to make a select dropdown with html and the value must be something from the database, the select is in a php function and for some reason my foreach loop is not working correctly, when I click on the select, the values are empty, -> I am doing something wrong :)

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

I think the problem is somewhere here:

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

I know there are similiar questions, I've read Foreach php function inside HTML select options but still not working :(

4
  • echo '<option value="'; < missing " ? Commented Apr 10, 2015 at 7:05
  • print_r($iterator); and check what it show? Commented Apr 10, 2015 at 7:05
  • @satishrajak 'IteratorIterator Object ( ) ' Commented Apr 10, 2015 at 7:06
  • object is empty means no value come to your drop down . what is $iterator = new IteratorIterator($stmt); mean Commented Apr 10, 2015 at 7:08

2 Answers 2

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

You didn't close the tag properly

Sign up to request clarification or add additional context in comments.

1 Comment

works! thanks :) edit: 9 minutes for accepting answer
0

Try with -

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

2 Comments

Parse error: syntax error, unexpected 'firstname' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\terras\includes\functions.php on line 358
Solved.. That was for the quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.