How to insert drop-down-list into table cell? - php

Is it possible to insert drop-down-list into table cell?
Thank you!
Here is some of the code:
$sql_query="SELECT nameOfwarning FROM warnings";
$sodss=mysqli_query($d,$sql_query);
if (mysqli_num_rows($result)<1)
?>
<select name = "warnings">
<option value="Choose one of the warnings: " </option>
<?php
while ($row = mysqli_fetch_array($warnings)) {
print "<option value=";
print $row["id_warning"];
print ">" ;
print $row["warning"];
print "</option>";
}
?>
</select><br>
<?php
print "<tr><td>Insert drop down list here?: </td><td><input type=\"text\" name=\"OR HERE\"></td></tr>";

I took the liberty to also format your code. Hope you don't mind. You should do that as a general practice to make it more readable.
<?php
$sql_query = "SELECT id_warning, warning FROM warnings";
$result = mysqli_query($d, $sql_query);
$dropDownHtml = '';
if (mysqli_num_rows($result) > 0) {
$dropDownHtml .= '<select name = "warnings">';
$dropDownHtml .= '<option value="Choose one of the warnings: " </option>';
while ($row = mysqli_fetch_array($result)) {
$dropDownHtml .=
'<option value="{$row["id_warning"]}">' .
$row["warning"] .
'</option>';
}
$dropDownHtml .= '</select><br>';
}
?>
<tr>
<td><?php echo $dropDownHtml; ?></td>
<td><input type=\"text\" name=\"OR HERE\"></td>
</tr>
It looks like you skipped some of the code, because what the SQL query should return doesn't match with the results array, also there is some variable mismatching. I changed it a bit to look believable, you should make sure you adapt it back to your specific case if you don't want to post all of your code in the question.

Related

Make select options from database

I want to make select options list with data from my database
I did something like this
<?php
if ($resultt->num_rows > 0) {
// output data of each row
while($row = $resultt->fetch_assoc()) {
?>
<select name="name" class="custom-select">
<?php
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
?>
</select>
<?php
}
} else {
echo "0 results";
}
?>
I see my output but every data is in seperated "box". I just want list to choose for example options1 or 2.
What I did wrong?
You need to take the <select> element outside the loop.
<?php
if ($resultt->num_rows > 0) {
echo '<select name="name" class="custom-select">';
// output data of each row
while($row = $resultt->fetch_assoc()) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo '</select>';
} else {
echo "0 results";
}
its happen because you put <select> inside loop so it create every time new <select>
just put your select outside loop like as follow
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
You are nesting select elements inside each loop
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='".$row['name']."'>" .$row['name']."</option>";
}
?>
</select>
And watch out for unnecessary single quote that will break your html:
"'>"

How can I add an empty value in a selection tag?

I have made a form that lets the user to select a year and place they want. The select tag for year is as below:
<strong>Tahun: </strong>
<?php
echo ('<select name="tahun">');
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo ('<option value= "'.$i.'">'.$i.'</option>');
}
echo ('</select>');
?>
And code for place selection tag as below:
<strong>Tempat: </strong>
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error._LINE_);
while($row = $results->fetch_assoc()){
echo ('<option value= "'.$row['nama'].'">'.$row['nama'].'</option>');
}
echo ('</select>');
?>
Now, what I want to do is add empty value with a message "Please choose one" for both selection tag. So, can someone help me how to add that value and message in this tag?
If you wanted to add select options to your code, this is how it should be:
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo ('<option>Please choose one </option>'); // Added this here
while ($row = $results->fetch_assoc()) {
echo ('<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>');
}
echo ('</select>');
?>
When you add default option with empty value then you should add as <option value="">Select...</option>
<strong>Tahun: </strong>
<?php
echo '<select name="tahun">';
echo '<option value="">Please Choose One</option>';
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo '<option value= "'.$i.'">'.$i.'</option>';
}
echo '</select>';
?>
and
<?php
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo '<select name="tempat">';
echo '<option value="">Please choose one</option>'; //Please choose one
while ($row = $results->fetch_assoc()) {
echo '<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>';
}
echo '</select>';
?>

How to add php in html option tag?

I want to add values in option tag from sql database with the help of php. I have tried it but it is not showing anythingHere is my code which I have tried
<form action="addsubcategory.php" method="post">
<input type="text" name="sub_category" placeholder="Add sub category">
<?php
$res = mysqli_query($conn, "SELECT * FROM product_category");
echo '<select name="testSelect" id="testId">';
//Here is the problem
while ($record = mysqli_fetch_array($res)) {
echo '<option value="'.$record['category_name'].'">' . "</option";
}
echo '</select>';
?>
</form>
Where you have this:
echo '<option value="'.$record['category_name'].'">' . "</option";
You need this:
echo '<option value="'.$record['category_name'].'">' . "</option>";
Try with
echo '<option value="'.$record['category_name'].'">'.$record['category_name'].' "</option">';
You have only added value to option tag.
while ($record = mysqli_fetch_array($res)) {
echo '<option value="'.$record['category_name'].'">' . "</option";
}
instead of above code you should use:
while ($record = mysqli_fetch_array($res)) {
echo '<option value='.$record["category_name"].'>'. $record["category_name"] .'</option>';
}
Now above code will add value and text in option tag. Text to view and value to set or get value from db.

Create and fill combo box with mysql data with php

I have done google searches and found several answers but I cannot get this to work. I am trying to read a field from ym mysql database and create and populate a combo box containing those values. The database connection works but all I get is a list of the values and no combo box. I am posting my code, along with the results.
<?php
$cn=mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("db_name",$cn) or die(mysql_error());
$sql = "SELECT LoadNumber FROM tblLoads";
$result = mysql_query($sql);
?>
<select name="loads" size=1>
<?
while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";
}
echo "</select> "
?>
Here is my output:
1637825
1637933
1638102
1638109
1638574
1638683
>
Please, this is driving me crazy, i don't see what I am doing wrong. it does not create the combo box.
You are complicating your code! Some part of it is wrapped in PHP and part of it is not.
Change this...
<select name="loads" size=1>
<?
while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";
}
echo "</select> "
?>
To this...
<select name="loads" id="loads">
<?php while($ri = mysql_fetch_array($result)) { ?>
<option value="<?php echo $ri['LoadNumber'] ?>"><?php echo $ri['LoadNumber'] ?></option>
</select>
<?php } ?>
Finally... you should stop coding in mysql_ !!! It has been deprecated and will soon cease to exist. When that happens, all of your sites using mysql_ code will stop functioning.
You should really be learning pdo_mysql
Try this.
<select name="loads" size=1>
<?php while($ri = mysql_fetch_array($result))
{
echo "<option value=" .$ri['LoadNumber'] . ">" . $ri['LoadNumber'] . "</option>";?>
<select>
just move the last select from the php script.

PHP select option using value from MySQL

with this code I get dropdown menu to select:
<select name='select'>
$sql = mysql_query("SELECT sport FROM kategorija");
mysql_close();
while ($result = mysql_fetch_array($sql)) {
<OPTION selected='{$kategorija}' VALUE='" . $result[0] . "'>" . $result[0] . "</OPTION>
}
</select></td>
This is how my table looks:
ID DropDownMenu xxx yyy zzz
How to set that dropdown menu selected value is the value connected to that ID.
In my case I always get last value from dropdown menu as selected one.
Try this: UPDATED
$out = '<select name="select">';
$sql = mysql_query("SELECT sport FROM kategorija");
mysql_close();
while ($result = mysql_fetch_assoc($sql)) {
$out .= "<OPTION selected='" .$result['$kategorija']. "' VALUE='" .$result[0]. "'";
if ($result['$kategorija'] == 'something') {
$out .= "selected";
}
$out .= ">" .$result[0]. "</OPTION>";
}
$out .= "</select></td>";
echo $out;
first store the selected sport in database to a variable
eg:
$sport='cricket';//substitute with database fetch value
Sport: <select name="sport" >
<option value="All" >All</option>
<?php $a="select * FROM kategorija";
$d2=mysql_query($a);
while($d4=mysql_fetch_array($d2))
{?> <option value="<?php echo $d4['sport']; ?>"<?php if($sport==$d4['sport']){ echo "selected";} ?> > <?php echo $d4['sport']; ?> </option> <?php
}
?></select>

Categories