Reputation:
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> › <a href="dashboard.php?app=users">Roosters</a> › <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">«</a> <a href="?app=users&page=' . ($page - 1) . '" title="Vorige pagina">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?app=users&page=' . ($page + 1) . '" title="Volgende pagina">›</a> <a href="?app=users&page=' . $pages . '" title="Laaste pagina">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</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 :(
Upvotes: 0
Views: 3990
Reputation: 31749
Try with -
foreach ($iterator as $row){
echo '<option value="';
echo $row['firstname'];
echo '">'.$row['firstname'].'</option>';
}
Upvotes: 0
Reputation: 1177
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
Upvotes: 1