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) {
Related
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.
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 have a web site that contains an HTML form, in this form I have a dropdownlist with list of agents that works in the company, I want to fetch data from MySQL database to this dropdownlist so when you add a new agent his name will appear as an option in the drop down list.
<select name="agent" id="agent">
</select>
To do this you want to loop through each row of your query results and use this info for each of your drop down's options. You should be able to adjust the code below fairly easily to meet your needs.
// Assume $db is a PDO object
$query = $db->query("YOUR QUERY HERE"); // Run your query
echo '<select name="DROP DOWN NAME">'; // Open your drop down box
// Loop through the query results, outputing the options one by one
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.htmlspecialchars($row['something']).'">'.htmlspecialchars($row['something']).'</option>';
}
echo '</select>';// Close your drop down box
# here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";
}
echo "</select>";
# here username is the column of my table(userregistration)
# it works perfectly
You need to fetch all rows from the database and then iterate over them, displaying a new <option> for each one of them. Pay attention to avoid XSS by using htmlspecialchars().
$pdo = new \PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'user', 'password', [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false
]);
// Select all values from the table Agents
$stmt = $pdo->prepare("SELECT Id, Name FROM Agents");
$stmt->execute();
echo '<select name="agent" id="agent">';
// For each row from the DB display a new <option>
foreach ($stmt as $row) {
// value attribute is optional if the value and the text is the same
echo '<option value="'.htmlspecialchars($row['Id']).'">';
echo htmlspecialchars($row['Name']); // The text to be displayed to the user
echo '</option>';
}
echo '</select>';
If you want to preselect one of the values, then you need to apply selected attribute to one of the <options>:
$selected = 'Somebody';
echo '<select name="agent" id="agent">';
foreach ($stmt as $row) {
if ($selected === $row['Name']) {
echo '<option value="'.htmlspecialchars($row['Id']).'" selected >';
} else {
echo '<option value="'.htmlspecialchars($row['Id']).'">';
}
echo htmlspecialchars($row['Name']);
echo '</option>';
}
echo '</select>';
What you are asking is pretty straight forward
execute query against your db to get resultset or use API to get the resultset
loop through the resultset or simply the result using php
In each iteration simply format the output as an element
the following refernce should help
HTML option tag
Getting Datafrom MySQL database
hope this helps :)
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";
}
.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.