Hi I am trying to populate an entire Drop down list with MySQL but I cant get it to work, can you please help?
My code:
$database=& JFactory::getDBO();
$database->setQuery('SELECT training_id,training,trainingDate FROM training ');
$result = $database->loadObjectList();
echo '<select name="whatever">';
while($row = mysql_fetch_array($result)) {
echo '<option value="$row[training_id" />';
}
echo '</select>';
Your echo string doesn't allow for embedded variables because you are using single quotes instead of double quotes.
Implement this echo instead:
echo '<option value="' . $row["training_id"] . '" />';
Without knowing what the output is, it's hard to know whether this is the only issue, but the glaring error in your code is this:
echo '<option value="$row[training_id" />';
Because this is in single quotes, the variable is not interpreted. You need to use double quotes (and close the square brackets!):
echo "<option value=\"{$row['training_id']}\" />";
Note that I have changed the style of variable interpretation to use curly brackets: I believe this is easier to read and clearer.
In addition to using single quotes, you are also:
Missing a closing square bracket.
Missing the closing tag for the <option>.
You probably want to change your output to something like this, so you display some option text to the user:
echo '<select name="whatever">';
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['training_id'] . '"> ' . $row['training'] . '</option>';
}
echo '</select>';
$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>';
Use #__ for table prefix.
$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>';
This code works, but populates the list with empty options. I had to put a simple echo in as shown below, and works just fine. For some reason, $row->teremnev is empty for me. Im sure this is not the right way, but it works.
$db =& JFactory::getDBO();
$query = "SELECT teremnev FROM #__teremlista";
$db->setQuery($query);
$result = $db->loadResultArray();
echo '<select name="termek">' ;
foreach ($result as $row) {
echo '<option value="'.$row->teremnev.'" />';
echo $row;
}
echo '</option>';
echo '</select>';
<select>
<option>Select Training</option>
while($row = mysqli_fetch_array($result))
{
echo "<option>". $row['training_name']."</option>";
}
</select>
Related
My php form here i made a drop down menu populated from a database...
<?php
include("mysql_connect.php")
?>
<html>
<head>
<link rel="stylesheet" href="form-style.css">
</head>
<body>
<form action="form_result.php" method="post">
<select name="cpu">
<?php
$result = $conn->query("SELECT * FROM cpus");
while ($row = $result->fetch_assoc()) {
echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";
}
?>
</select>
Here i tried to post the selected options from the drop down menu, but the results were just showing the ID number of the database table so i tried to make a function that would echo the Name of the product for that specific ID, but it didn't work...
<?php
include("mysql_connect.php");
function select_cpu() {
$result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
while ($row = $result->fetch_assoc()) {
echo "{$row['CpuManufacturer']} {$row['CpuName']}";
}
}
if(isset($_POST['submit'])) {
$cpu = $_POST['cpu'];
echo "<table>";
echo "<tr><th>You have selected:</th><tr>";
echo "
<tr><td hidden>".$cpu."</td><td>".select_cpu()."</td></tr>
";
}
echo "</table>";
?>
In the select_cpu function, you should return the string instead of echoing it:
function select_cpu() {
$result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
$str = "";
while ($row = $result->fetch_assoc()) {
$str .= "{$row['CpuManufacturer']} {$row['CpuName']}";
}
return $str;
}
Your first code snippet was almost right, but if you viewed the source you'd notice the <option> code was wrong. This is because you're mistaking the double quotes shown in HTML with what PHP sees. To output a double quote in a PHP echo, either escape it ($something = "a double quote: \"") or wrap it in single quotes instead ($something = 'a double quote: "').
Try changing this line:
echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";
to this:
echo '<option value="' . $row['CpuID'] . '">' . $row['CpuManufacturer'] . $row['CpuName'] . '</option>';
I have form with a number of drop boxes which have the numbers 1-5 in them. I can use this code to populate a drop down but was wondering if I can somehow only make the call to the db once but populate all the drop downs that use the same numbers?
<?php
$sql = "SELECT * FROM riskNumDrop";
$result = $conn->query($sql);
if (!$conn->query($sql)) {
echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
echo '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){echo '<option value='. $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
?> </select>
So Ideally, I generate the output once and reuse it multiple times. Im guessing an array (which $result already is) but how do I populate a drop down from it? TIA
Saving as a string would save you the processing of having to loop through the same data generating the same output multiple times. If this is what you want, you could do the following.
Replace:
echo '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){echo '<option value='. $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
?> </select>
with:
$drop = '<select class="assess" name="precontcons" style="width:4em">' ;
while($row = $result->fetch_assoc()){
drop .= '<option value='. $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}
$drop .= '</select>';
Then you can echo $drop several times if you want.
If for whatever reason you want different select attributes, you could just save the options list and print the select around that, like this:
$dropOptions = "";
while($row = $result->fetch_assoc()){
$dropOptions .= '<option value='. $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}
Then just echo '<select class="foo" name="bar">'.$dropOptions.'</select'>
You could store the data into an array, and then print the data by enumerating the array.
$risks = array();
// Load values into array.
while ($row = $result->fetch_assoc()) {
array_push($row['riskNumDrop']);
}
// Drop down #1
echo '<select>';
foreach ($risks as $risk) {
echo '<option value='. $risk .'>'. $risk .'</option>';
}
echo '</select>';
// Drop down #2
echo '<select>';
foreach ($risks as $risk) {
echo '<option value='. $risk .'>'. $risk .'</option>';
}
echo '</select>';
Simply get the results into an array using the fetch_all method. It returns all rows from the result set into an associative or numeric array. The you can do whatever you want with such array:
$result = $conn->query('SELECT * FROM riskNumDrop');
if (!$result) {
echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
exit; // fetch_* functions cannot be called on error (sice $result is false)
}
// get all rows from the result
$rows = $result->fetch_all(MYSQLI_ASSOC);
// output first dropdown...
echo '<select name="dropdown_1">';
foreach ($rows as $row) {
// options for the first dropdown (same as you did before)
}
echo '</select>';
// output second dropdown...
echo '<select name="dropdown_2">';
foreach ($rows as $row) {
// options for the second dropdown
}
echo '</select>';
I'm stuck with while in php this is the part that makes problem and still returning duplicate rows. When I use select in phpmyadmin I get what I'm looking for. I think problem is with while. Can you look at it?
<td><select name='movie_type'>
<?php
$query = 'SELECT movietype_id, movietype_label FROM `movietype`';
$result = mysql_query($query, $db) or die (mysql_error($db));
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<option value="' . $row['movietype_id'] . '">';
echo $row['movietype_label'] . '</option>';
}
}
?>
</select></td>
You don't need to use foreach loop, just change your code to:
while ($row = mysql_fetch_assoc($result)){
echo '<option value="' . $row['movietype_id'] . '">';
echo $row['movietype_label'] . '</option>';
}
I'm making simple basketball stats plugin to wordpress, and I'm using dropdown list a lot. I wanted to make function but I don't know how to pass arguments to MySQL. Here's my code:
function dropDown($tab, $option, $text){
$result = mysqli_query($con,'SELECT * FROM tab');
while($row = mysqli_fetch_array($result)){
echo "<option value=\"";
echo $row['option'] . "\">" . $row['text'];
echo "</option><br>";
}
}
and I would like to use it like this:
dropDown("team", "team_id", "name");
I tried with different quotation marks, dots etc but it doesn't seem to work.
#edit
I know PHP syntax (some of it) and I know how to use it, but I don't know how to pass $variables to MySQL query, and that's my main problem.
try
function dropDown($team, $team_id, $name) {
// use both three var where you want
$result = mysqli_query($con,'SELECT * FROM team');
echo "<select>";
while($row = mysqli_fetch($result)){
echo "<option value=\"";
echo $row['team_id'] . "\">" . $row['name'];
echo "</option>";
}
echo "</select>";
}
dropDown("team", "team_id", "name");
I have the following php code and it's working great for showing 1 column, but I need it to show the values of 10 columns.
<select size="1" name="domeinnaam">
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '</option>';
}
?>
</select>
I have tried to add a 2nd echo, but that corrupted the code. I also tried to
echo '<option>' . $row['domeinnaam1'] . $row['domeinnaam2'] . '</option>';
but that didnt work. because the result will then display as follows:
domain1
domain1
domain1domain2
and it should be
domain1
domain1
domain1
domain2
What will work?
Assuming you want each domain name to appear as an option in the select and the domain name fields in your db are domeinnaam1, domeinnaam2, domeinnaam3, etc., you would do the following...
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
$domains = array();
while ($row = mysql_fetch_array($resultaat))
{
if (!empty($row['domeinnaam1'])) $domains[] = $row['domeinnaam1'];
if (!empty($row['domeinnaam2'])) $domains[] = $row['domeinnaam2'];
}
?>
<select size="1" name="domeinnaam">
<?php
foreach ($domains as $domain)
{
echo "<option>$domain</option>";
}
?>
</select>
You should use PDO instead of mysql_ functions or the ADODB library works well. mysql_ functions are deprecated as of PHP 5.5
refer to http://www.php.net/manual/en/pdo.construct.php for PDO reference
Your code should not have major PHP configurations or initializations like this. I does not look smart. Tidy it up.
<select size="1" name="domeinnaam">
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '</option>';
}
?>
</select>
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysqli_query($conn, $sql) or die ($conn->mysqli_error());
?>
<select size="1" name="domeinnaam">
<?php
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '</option>';
}
?>
</select>
Next don't really try to break options, you could use javascript frameworks like JQuery to achieve a more stylish select. I don't really think you can do this:
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '<br />' .$row['domeinnaam2']. '</option>';
}
Using JQuery, you could a list item in a div:
<ul>
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<li>' . $row['domeinnaam1'] . '<br />' .$row['domeinnaam2']. '</li>';
}
</ul>
Then pass the selected item to a hidden field for later use using the onclick event.
If you want to display the 10 columns in one loop as separate options, this could do it.
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<option>' . $row['domeinnaam1'] .'</option>';
echo '<option>' . $row['domeinnaam2'] .'</option>';
echo '<option>' . $row['domeinnaam3'] .'</option>';
echo '<option>' . $row['domeinnaam4'] .'</option>';
echo '<option>' . $row['domeinnaam5'] .'</option>';
echo '<option>' . $row['domeinnaam6'] .'</option>';
echo '<option>' . $row['domeinnaam7'] .'</option>';
echo '<option>' . $row['domeinnaam8'] .'</option>';
echo '<option>' . $row['domeinnaam9'] .'</option>';
echo '<option>' . $row['domeinnaam10'] .'</option>';
}
I wonder where this would be useful and how. Anything can happen in programming. :)