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

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

Related

Multiple html drop down with values from database using while loop

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

foreach select option error retrieve data horizontal

I have a problem when I retrieve data from database the result is
How to retrieve the data normally?
This is my codes..
$wewqry = $mysqli->prepare("SELECT itemname from table_item ");
$wewqry->execute();
$wewqry->bind_result($itemname);
$wewqry->store_result();
while ($wewqry->fetch()){
$table = array("itemname" => $itemname);
foreach ($table as $t => $w){
echo '<select name="itemname">';
echo "<option>$w</option>";
echo '</select>';
}
}
You should not create a new <select> each time through the loop, do it once outside the loop:
echo '<select name="itemname">';
while ($wewqry->fetch()) {
echo "<option>$itemname</option>";
}
echo '</select>';
You also don't need to make an array for each row you fetch. Just insert the result variable directly into the echo statement.

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>";
}
?>

querying database with array variable [duplicate]

This question already has answers here:
create dropdown menu on each loop assign database value to option element
(2 answers)
Closed 9 years ago.
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)
If 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)
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);
}
In your original code, you could remove the two foreach loops in the while loop. Use $pposition in place of $playerPosition and your code should work as expected. Here's your code, with my suggested changes, in your procedural style:
$position = array(
"THP",
"HKR",
"LH",
"LK4",
"LK5",
"FLH"
);
echo '<form name="selectPlayers" method="post">';
foreach ($position as $pposition) {
$result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info`
WHERE `position` = '$pposition'") or die(mysql_error());
echo '<select name="'. $pposition . '">';
while ($row = mysql_fetch_array($result)) {
print $pposition;
echo '<option value="' . $row['player_id'] . '">'.
$row['name'] .' '. $row['surname'].
'</option>';
} //end while loop
echo '</select>';
echo '<br />';
} //end opening for each loop
echo '</form>';

populate dropdown with table list

.hey guys how can i populate a dropdown list with the list of tables from a certain database?
$db = mysql_select_db('thepillar');
$sql = "SHOW TABLES";
$result = mysql_query($sql);
echo '<form method="post" id="try" action="pillar.php">';
echo 'Select Batch: ';
echo '<select name="batch" id="batch">';
echo '<option>';
while($r = mysql_fetch_assoc($result))
{
$tables = $r;
echo '<option>'.$tables.'</option>';
}
.i have tried the code above but the dropdown list is only filled with the word "Array" multiple times depending on how many tables are there in the database.
.help pls!
while($r = mysql_fetch_array($result))
{
echo $r[0]."<br />";
}
replace
$tables = $r;
with
$tables = $r['Tables_in_thepillar'];
also you got an extra echo '<option>';
above the loop
Your $tables variable is an associative array. You need to specify which index of the array you want to output between the <option> tags.
echo '<option>'.$tables['TABLE_NAME'].'</option>';
See the print_r output for what the index name is.

Categories