Is there any simple way to add none value in a dropdown list in php?? I tried the below but it doesn't work. It shows all lname values but not the 'None'.
I do not want to enter 1 none value in database and retrieve it. So if it is possible through php itself.
while ($row = mysqli_fetch_array($result))
{
echo "<option value=None>" . $row['lname'] . "</option> <br>";
}
Please help me.
Just add the "none"-option above the others.
echo '<option value="">None</option>';
while ($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['lname'] . '">' . $row['lname'] . '</option>';
}
Two things odd here:
- You need quotes around the None in value=None --
- a inside of a select options list isn't ever a smart thing to do.
Related
I am a bit stuck I am attempting to build out a table with HTML and PHP. Ok got that done. Now I want to add a dynamic dropdown as one of the column options but cant seem to figure out how to mix the PHP, the HTML and the needed FOR EACH loop.
I know its currently wrong but am posting a variable of what I have been trying below.
echo '<td>' . "<select>". "<option value =" . $sf_name . ' '. $sl_name . ">" . $sf_name . ' '. $sl_name . "</option>" .
foreach($Staff_On_Duty as $person){
"<option value =" . $sf_name_option=$person->Staff_First_Name . ' '. $sl_name_option=$person->Staff_Last_Name . ">" . $sf_name_option . ' '. $sl_name_option . "</option>"
}
. "</select>" .'</td>';
I need to have the currently selected individual at the top of the dropdown with the option to change that person out. the first .sf_name comes from higher in the code and gives me the name of the individual currently selected. The foreach runs from the $Staff_On_Duty query to give me everyone working right now. What is the right way to do this?
This is how I would do it, essentially only fill the dropdown and mark the option that should have been selected (other stuff is a matter of preference):
echo("<td><select>");
foreach($Staff_On_Duty as $person){
$sf_name_option=$person->Staff_First_Name; // separated for clarity, you could also use $person->Staff_First_Name everywhere
$sl_name_option=$person->Staff_Last_Name;
echo("<option value = $sf_name_option $sl_name_option");
if (($sf_name_option == $sf_name) && ($sl_name_option == $sl_name)) echo (" selected"); // this is the relevant part, but make sure variable values match!
echo(">$sf_name_option $sl_name_option</option>");
}
echo("</select></td>");
Note that I did not put any quotation marks cause I have no idea what your variables entail, but it would need them if you don't have them in yet, i.e.:
... value=\"$sf_name_option $sl_name_option\" ...
I am very new to php and am teaching it to myself, so please keep that in mind.
I am working on a project that presents users with a list of items, from a database, with checkboxes, and allows the user to check them. I want to save the values of the checked fields. This is the line of code that prints all of the options. It prints a course code and course title, with a checkbox.
echo "<input type='checkbox' name ='boxes'>" . $row['course'] . ' ' . $row['title'] . "<br>";
However, when I try to print the values selected, it doesn't work. I get an error that says invalid argument supplied foreach()
if(isset($_POST['submit_courses'])){
if(!empty($_POST['boxes'])){
foreach($_POST['boxes'] as $selected ){
echo $selected."</br>";
}
}
}
Please help!
Try:
echo "<input type='checkbox' name ='boxes[]'>" . $row['course'] . ' ' . $row['title'] . "<br>";
EDIT:
To better answer the question:
You need to add the value="" attribute to your input fields. So if you have an ID in your $row array then it would be like this...
echo "<input type='checkbox' name ='boxes[]' value=' . $row['id'] . '>" . $row['course'] . ' ' . $row['title'] . "<br>";
Now you should be able to get the selected id's from the populated values inside of the $_POST['boxes'] array. Just loop through them and do something with them like echo them out.
foreach ($_POST['boxes'] as $box_value) {
echo $box_value . "<br>";
}
Only the boxes that are selected will be in the array and you will have the identifier for whatever courses were selected.
What am I doing wrong here? When it populates the options it shows $name[0] and not the info from the DB. Though the correct number of options seem to be available.
<?php
//connect to database
$conn = mysqli_connect("example.com", "timemin", "Pass123", "timesheet");
//query database for items to populate
$sql = "SELECT DIS_NAME, NAME FROM INVITEM";
$query = mysqli_query($conn, $sql);
echo '<select>';
echo '<option value="">Choose your favorite fruit</option>';
while($name = mysqli_fetch_assoc($query)){
echo '<option value="' . '$name[1]' . '">' . '$name[0]' . '</option>';
}
echo '</select>';
echo $query;
?>
Should be as follows, you quoted variables, that was the problem.
echo '<option value="' . $name['DIS_NAME'] . '">' . $name['NAME'] . '</option>';
Also as RiggsFolly mentioned, you used fetch_assoc so the array keys will be named accordingly DIS_NAME and NAME.
Credit to RiggsFolly for spotting this.
In php, there are two types of quotes: single quotes and double quotes. Single quotes will not parse variables, double quotes will.
If you do want to use quotes, you could do something like this:
echo "<option value="."$name[1]".">$name[0]</option>";
So here, the double quotes will tell php to parse the variable names
However, I would recommend doing this:
echo '<option value="' . $name[1] . '">' . $name[0] . '</option>';
See this SO post for more.
This page generates the option pulled from database . another page brings here the year of joining of students via year_joining . rest of the mysql queries works absolutely fine ( tested )
<?php
include_once("../Include/connectdb.php");
if($_GET['year_join'])
{
$id=$_GET['year_join'];
$result1 = mysql_query("select distinct sub_id from subject_profile where batch='$id'
")or die(mysql_error());
while($subid = mysql_fetch_assoc($result1)){
$result2 = mysql_query("SELECT name FROM `subjects` WHERE `sub_id` LIKE
'$subid[sub_id]'");
$subject=mysql_fetch_assoc($result2);
if($subject[name]!=""){
//print "<OPTION value=".$tmp.'">'.$tmp.'</OPTION>';
//print "<OPTION value='$tmp'>'$tmp'</OPTION>";
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
//echo '<option value="'.$id.'">'.$data.'</option>';
//}
}
}
}
?>
FYI :
//print "<OPTION value=".$tmp.'">'.$tmp.'</OPTION>';
//print "<OPTION value='$tmp'>'$tmp'</OPTION>";
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
//echo '<option value="'.$id.'">'.$data.'</option>';
none of these are working ... :(
with simple echo $tmp it works
but when ever i put as
echo "<option value=";
the result is blank page ...
and when i am echo - ing just the variable its works perfectly fine
echo $tmp;
gives the list of all the subjects ..
For the problem regarding "not displaying output", I believe you have missed a <select> tag enclosing the <option> tags.
As a side note, in the query, change it like this:
"SELECT name FROM `subjects` WHERE `sub_id` LIKE '{$subid[sub_id]}'"
or,
"SELECT name FROM `subjects` WHERE `sub_id` LIKE '" . $subid[sub_id] . "'"
Still it is not safe. You should escape the values and better use a prepared statement using mysqli or PDO
The main problem is as per Akhilesh B Chandran's answer, with a missing <select> tag.
Other than that, there will be a problem when $subject['name'] has a space in it.
The problem is this line:
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
It should be like this:
echo '<option value="'.$subject['name'] . '">' . $subject['name'] . '</option>';
In what you currently have, the browser interprets the result as this:
<option value=insert name here">insert name here</option>
As it is visible, there is a missing quote after value=.
Your quotes don't match up, which is why PHP is failing.
Try changing:
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
to:
echo "<option value=".$subject['name'] . ">" . $subject['name'] . "</option>";
Basically, you start off using a ", and switch partway through to using '
I am using mysqli to query a database table to obtain the value for a dropdownlist. However, I also want to retrieve the corresponding description and load that into the option text. My query is as follows:
<?php
$query=('SELECT restaurantid,location from restaurant');
$result = mysqli_query($mysqli,$query);
echo '<select name="ddlStore">';
while($row=$mysqli->fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">';
'</option>';
}
echo '</select>';
?>
How do I revise the above so that the location field is populating in the dropdownlist's option text?
Updated code:
<?php
$query=('SELECT restaurantid,location from restaurant');
$result = mysqli_query($mysqli,$query);
echo '<select name="ddlStore">';
while($row=$mysqli->fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' .
htmlspecialchars($row['location']) .
'</option>';
}
echo '</select>';
?>
The above still does not display the locations as dropdownlist option text values.
Update #2:
When viewing the page, the dropdownlist doesn't show the values from the database table. Using IE Developer Tools, I see a script error in the while statement:
Call to undefined method mysqli::fetch_array()
Is there a more optimal way to structure the mysqli_fetch_array statement?
Change to...
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' . htmlspecialchars($row['location']) . '</option>';
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' . htmlspecialchars($row['location']) . '</option>';
But what have you tried so far?
Try this way (more info here)
$result = $mysqli->query($query)
while($row = $result->fetch_assoc()) {
}