.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.
Related
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++;
}
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>";
}
?>
I'm trying to create a default value for my dynamic drop down list. After the user selects one of the options, they submit and that value is stored as a variable in the next page that I can access using $_POST['land']
I have created the same dynamic list in the next page and want that 'land' to appear first in the dynamic drop down menu. So far this is just the main code to show the dynamic drop down list. Any help would be appreciated. Thanks!
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
echo "<option value='$place'>$place</option>\n";
}
As far as I understand you want the item chosen on first page to appear as the default item in the second page.
Use this
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
echo "<option value='$place'";
echo ((isset($_POST['land']) && $_POST['land']==$place)?'selected="selected"':'');
echo ">$place</option>\n";
}
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
echo "<option value='$place' ";
if($_POST['land'] == $place) {
echo "selected='selected'";
}
echo ">$place</option>\n";
}
while($row = mysqli_fetch_assoc($result))
{
extract ($row);
$select = (isset($_POST['land']) && $_POST['land'] == $place)?"selected='selected'":"";
echo "<option value='$place' $select >$place</option>\n";
}
I am trying to populate a selection box (dropdown) from a mysql table in php, but I get blank results, Can someone look in my code and tell me what is wrong?
I need to populate the select with the dates available from my sql query, so they show up as seletion options....
<?php
echo JText::_('Please select the date:');
$database= &JFactory::getDBO();
$database->setQuery('SELECT training_id,training,trainingDate FROM training');
$result = $database->loadObjectList();
echo '<select name="whatever">';
foreach ($result as $row) {
echo '<option value="'.$row->training_id.'" />';
}
echo '</select>';
?>
You assign an option value, but you don't provide human-readable option text:
echo '<select name="whatever">';
foreach ($result as $row) {
<!-- here we go -->
echo '<option value="'.$row->training_id.'">'.$row->training.'</option>'; // always close options!!!
}
echo '</select>';
echo '<option value="'.$row->training_id.'">'.$row->trainingDate.'</option>';
If that doesn't work then your $row->training_id isn't set and so you'll need to debug that (e.g. doing a print_r($row) just before that line to see what is inside the $row object)
Try this:
while($row = mysql_fetch_object($result)) {
instead of this:
foreach($result as $row) {
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.