Multiple html drop down with values from database using while loop - php

i already have a while loop that displays select inputs in my form depending on how many passengers the user is booking. Each drop down was supposed to show all data from a column in my database. The problem is that only the first drop down displays the data i fetched which is seats 1 to 30.
Query to get the airline id of the current flight
$result = mysqli_query($conn,"select * from flight_seat_status where flight_number = '$_SESSION[departure]'");
Variables i used to start the counting in the loop
$print = 1; $name = 0;
My while loop with the problem
echo "<h1 class='adminContent_h1'>Please choose a seat</h1>";
while($print <= $_SESSION['passengers']){
echo "<label style = 'width:170px;'>". $_SESSION['firstname'][$name]."'s Seat</label>";
echo "<select class = 'content_dropdown' style = 'width:15%; margin-bottom:20px;' name = 'passengers[]'>";
while($row = mysqli_fetch_assoc($result)){
echo "<option value='".$row['seat_id']."'>".$row['seat_number']."</option>";
}
echo "</select><br>";
$print++;
$name++;
}
it is able to print out a number of drop downs depending on the user but only the first drop down contains the data.
what is wrong with my code? please explain

You have to set the pointer back to 0 for using the result again
// set the pointer back to the beginning
mysqli_data_seek($result, 0);
while($row = mysqli_fetch_assoc($result)){
// do whatever here...
}

Please use below code. All the seats records you have to fetch once outside passenger loop and store in another array and then you have to use seats array inside the passenger loop each time.
echo "<h1 class='adminContent_h1'>Please choose a seat</h1>";
$seats = [];
while($row = mysqli_fetch_assoc($result)){
$seats[] = $row;
}
while($print <= $_SESSION['passengers']){
echo "<label style = 'width:170px;'>". $_SESSION['firstname'][$name]."'s Seat</label>";
echo "<select class = 'content_dropdown' style = 'width:15%; margin-bottom:20px;' name = 'passengers[]'>";
foreach($seats as $res_seat){
echo "<option value='".$res_seat['seat_id']."'>".$res_seat['seat_number']."</option>";
}
echo "</select><br>";
$print++;
$name++;
}

Related

Organise seats into tablerows in html output with PHP

I'm putting together something that's mean to allow for a user to book seats for a cinema showing. The row and seat numbers for every showing are stored in the database. I'm currently extracting them in the following method so that users can click on a seat button to select that seat for their booking:
echo "<form>";
echo "<table>";
while($row = mysqli_fetch_assoc($result)){
$rownum = $row['row'];
$seat = $row['seat'];
echo "<tr><td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td></tr>";
}
echo "</table>";
Right now this just outputs html showing all of the buttons as a single row in the table. I'd like the output to show seating across a single table row for every one of the table rows in the cinema screen. I'm not sure how to do this exactly given that each row is of differing lengths. E.g row A has twelve seats while row C has eight.
What would be the best way of accomplishing this?
You could easily update your code so that you will get a new table row every time the mysql row has another value. One thing to note is that you might want to add (depending on whether you're already sorting your results) the following ORDER BY row,seat.
echo "<form>";
echo "<table>";
echo "<tr>";
while($row = mysqli_fetch_assoc($result)){
if (!isset($oldrownumber)) $oldrownumber = $row['row'];
else if ($oldrownumber != $row['row']) {
echo "</tr><tr>";
$oldrownumber = $row['row'];
}
$rownum = $row['row'];
$seat = $row['seat'];
echo "<td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td>";
}
echo "</tr>";
echo "</table>";

Heading come in all the rows

Heading come in all the rows need in first row
Need help with where i am going wrong
thank you
below is the code
<?php
$connection = mysql_connect('localhost', 'aria2', 'osvOSUxlY6wYLZzC'); //The Blank string is the password
mysql_select_db('torres');
$query = "SELECT * FROM reports"; //You don't need a ; like you do in SQL
$result = mysql_query($query) or die(mysql_error());;
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<table><tr><th>CSQ_Name</th>
<th>CSQ_ID</th>.......
$row..............
//$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
You only need to move the header portion out of the loop:
echo '<table><tr><th>CSQ_Name</th><th>CSQ_ID</th></tr>'; // start a table tag in the HTML
while ($row = mysql_fetch_array($result)) { //Creates a loop to loop
echo '<tr>';
// output data here
echo '<td>...</td><td>...</td>';
echo '</tr>';
}
echo "</table>"; //Close the table in HTML

Populate multiple drop-down lists with a single query

I have four drop-down lists that I would like to populate with values from an MSSQL table. All four lists should contain the same values. The query looks like this:
$data = $con->prepare("SELECT ID, Code FROM Table WHERE Code = :value ORDER BY Code");
$input = array('value'=>'value'); //'value' is hardcoded, not a variable
$data->execute($input);
And here is the code for my drop-downs:
<?php
echo "<select name=\"proj1[]\">";
while($row = $data->fetch(PDO::FETCH_BOTH))
{
echo "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "</select>";
?>
This works fine for one drop-down. If I try to create another one (proj2[], proj3[], proj4[]) and apply the same query, however, the PHP page stops loading at that point and the second drop-down does not populate. The only way I've found around it is to copy the query and change the variables ($data becomes $data2 for proj2[], and so on). I'd really rather not have to write the same query four times. Is there a way around it?
$select = '';
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$select .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "<select name=\"proj1[]\">";
echo $select;
echo "</select>";
echo "<select name=\"proj2[]\">";
echo $select;
echo "</select>";
//etc...
Why not just put all of it in a veriable and then using it 4 times?
Somthing like this:
<?php
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$options .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
for($i = 0; $i <= 4; $i++){
echo "<select name=\"proj1[]\">";
echo $options;
echo "</select>";
}
?>

create dropdown menu on each loop assign database value to option element

Im working on a page for a sports team where the coach can select his team. What im trying to do is:
1) Print different positions
2) Assign next to position name, players who are ONLY relevant to the position. (i.e if the positions name is flanker only flankers should be displayed in the drop-down menu)
My logic for the above problem is:
Assign position names to array called $position Loop over array
querying database, with position having a different value after each
loop.
Selected players that match the position gets assigned to an array
called $playerName
Print the $position variable
Create dropdown menu for each position
assign value from $playername array to the option element in the drop
down menu.
Now there should be different positions with dropdown menus, next to
them, containing player names relevant to position.
//create position array
$position = array(
"THP",
"HKR",
"LH",
"LK4",
"LK5",
"FLH"
);
echo '<form name="slectPlayers" method="post">';
foreach ($position as $pposition) {
$result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info` WHERE `position` = '$pposition'") or die(mysql_error());
while ($row = mysql_fetch_array($result)) { //create arrays
$id[] = $row['player_id'];
$playerName[] = $row['name'];
$playerLastName[] = $row['surname'];
// print position and open select element
foreach ($position as $playerPosition) {
print $playerPosition;
echo '<select>';
foreach ($playerName as $name) { //assign playername to position element
echo '<option>' . $name;
'</option>';
echo '</select>';
echo '<br />';
} //close assign player nae to position loop
} //end print position and open select loop
} //end while loop
} //end opening for each loop
echo '</form>';
unfortunately for me either my logic is wrong or my code is incorrect. This is the ouput I get: (Note only the name Tendai is displayed in all dropdown meus no other names appear)
Ive been struggling with this one all morning if someone could point me in the right direction it would be greatly appreciated
(note to modirators the picure above does not contain real names and is only a fictional database)
This could be your problem
foreach ($position as $pposition) {
$result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info` WHERE `position` = '$pposition'") or die(mysql_error());
while ($row = mysql_fetch_array($result)) { //create arrays
$id[] = $row['player_id'];
$playerName[] = $row['name'];
$playerLastName[] = $row['surname'];
// print position and open select element
print $pposition;
echo '<select>';
foreach ($playerName as $name) { //assign playername to position element
echo '<option>' . $name;
'</option>';
echo '</select>';
echo '<br />';
} //close assign player nae to position loop
} //end while loop
} //end opening for each lo
I have removed foreach ($position as $playerPosition) {
You can do it like this. the logic is something different
function get_players_by_position($position)
{
$query = "SELECT
`player_id`,
`name`,
`surname`
FROM `player_info`
WHERE `position` = $position
ORDER BY name ";
$result = mysql_query($query);
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}
return $data;
}
foreach($position as $pposition){
$data = get_players_by_position($pposition);
echo $pposition;
echo '<select>';
foreach($data as $row){
echo '<option '.$row['id'].'>' . $row['name'].'</option>';
}
echo '</select>';
echo '<br />';
unset($data);
}

loop through database and show in table

I am trying to loop though my users database to show each username in the table in their own row. I can only get it to show one user but loops through this the number of rows there are. Code is below
<?php
require_once ('../login/connection.php');
include ('functions.php');
$query = "SELECT * FROM users";
$results=mysql_query($query);
$row_count=mysql_num_rows($results);
$row_users = mysql_fetch_array($results);
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
?>
Thanks
mysql_fetch_array fetches a single row - you typically use it in a while loop to eat all the rows in the result set, e.g.
echo "<table>";
while ($row_users = mysql_fetch_array($results)) {
//output a row here
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You're only fetching one row:
$row_users = mysql_fetch_array($results);
You're never calling a fetch again so you're not really looping through anything.
I'd suggest changing your loop to the following:
echo "<table>";
while ($row = mysql_fetch_array($results)) {
echo "<tr><td>".($row['email'])."</td></tr>";
}
echo "</table>";
The while loop will loop through the results and assign a row to $row, until you run out of rows. Plus no need to deal with getting the count of results at that point. This is the "usual" way to loop through results from a DB in php.
In the new MYSQLI, I use this coding
$query = "SELECT * FROM users";
$results=mysqli_query($con,$query);
$row_count=mysqli_num_rows($results);
echo "<table>";
while ($row = mysqli_fetch_array($results)) {
echo "<tr><td>".($row['id'])."</td></tr>";
}
echo "</table>";
mysqli_query($con,$query);
mysqli_close($con);
Your problem is in this line:
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
You're echoing out a <table> tag again. Remove that so your code looks like this:
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You don't need to output a table within a table the way you're doing it.
$result = mysql_query("SELECT `email` FROM `users`");
$num_emails = mysql_num_rows($result);
echo "<table><caption>There are/is $num_emails email(s)</caption>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>{$row['email']}</td></tr>";
}
echo '</table>';
Something like that should work.

Categories