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:
"'>"
Related
I am trying to retain the value selected in a drop down menu. Everything is working, but I don't know how to show and retain the selected value. How can I do this?
I've got this working using another way:
<?php if($_POST['selClass'] == $row1['class']) echo 'selected="selected"' ?>
but this leads to other problems, i.e. a blank option in my drop down menu.
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
You can approach this as
<?php
$selectedOption = '';
if($_POST){
$selectedOption = $_POST['selClass'];
}
?>
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($row1["class"] == $selectedOption)
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
else
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
There is two option you can choose whichever you feel ease.
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($_POST['selClass'] == $row1['class']){
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
}else{
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
}
?>
OR
<?php
$selectedClass = $_POST['selClass'];
while ($row1 = mysqli_fetch_array($rs5)) { ?>
<option value="<?php echo $row1['çlass']?>" <?php if(selectedClass == $row1['class']) { echo "selected='selected'"; }?> ><?php echo $row1['class']?></option>
<?php } ?>
I have some code that builds a select box. For the first select box; The selected option has a value of 1 then display it as selected then list everything else that doesn't hold the same value. Works good except the second box doesn't work. It obviously doesn't like me duplicating then changing the values. The second select box is looking for any values with 2. A third one would be a value of 3 etc.
Any way I can get the two of them working together?
<select name="first-box">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row['my_value']==1) {
echo "<option value='1' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==1) {
echo "<option value='1'>" . $row["title"] . "</option>";
}
}
}
?>
</select>
<select name="second-box">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row['my_value']==2) {
echo "<option value='2' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==2) {
echo "<option value='2'>" . $row["title"] . "</option>";
}
}
}
?>
</select>
This might help
<?php
if ($result->num_rows > 0) {
mysqli_data_seek($result,0);
while($row = $result->fetch_assoc()) {
if($row['my_value']==2) {
echo "<option value='2' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==2) {
echo "<option value='2'>" . $row["title"] . "</option>";
}
}
}
?>
If you are going to have more than one or two, then reading the data into an array and looping through that as needed will be better/easier
<?php
if ($result->num_rows > 0) {
$data=$result->fetch_all(MYSQLI_ASSOC);
}
// stop php and do the <select> tag here or print() it
foreach($data as $row) {
if($row['my_value']==1) {
echo "<option value='1' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==1) {
echo "<option value='1'>" . $row["title"] . "</option>";
}
}
// stop php or print() a </select>
// stop php and do the <select> tag here or print() it
foreach($data as $row) {
if($row['my_value']==2) {
echo "<option value='2' selected>" . $row["title"] . "</option>";
} else if ($row['my_value']!==1) {
echo "<option value='2'>" . $row["title"] . "</option>";
}
}
// stop php or print() a </select>
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.
In a form after submitting the data, if i want to edit form the data in the form should be from database i have to echo the selected value from the database if data is present else it has to show the options to select the values from another table where these options are present
here is the code
please correct it
<select name="ccname" class="form-control form-color">
<?php
if(isset($storyData)) {
echo '<option value="' . $storyData['ccname'] . '">' . $storyData['ccname'] . '</option>';
}
else{
echo "<option value="">CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'</option>';
}
?>
</select>
after working with concatenation operator finally debugged the code
here is the code
that i corrected
<select name="ccname" class="form-control form-color">
``<?php if(isset($storyData))
{
echo "<option value = '" . $storyData['ccname'] . "'>".$storyData['ccname']."</option>";
}
else{
echo "<option value='1'>CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'
</option>';
}
?>
</select>
I create this code to display a list of countries, I would retrieve the selected country but it does not work
<?php
include 'includes/combo.php';
?>
<?php
echo "<label for='pays'>Pays*</label>
<select id='pays' name='pays '>";
while ($row = mysql_fetch_array($combo)) {
echo "<option>$row[0]</option>";
}
echo "</select> ";
echo "<option>$row[0]</option>";
?>
and recover with a
if(isset($_POST['pays']))
$pays=$_POST['pays'];
else
$pays="";
echo $pays;
but the pays value is not recouped
echo"<option value=" . $row[0] . ">$row[0]</option>";
To retrieve the value of your option
Try this
<?php
include 'includes/combo.php';
?>
<?php
echo '<label for="pays">Pays*</label>
<select id="pays" name="pays">';
while ($row = mysql_fetch_array($combo)) {
echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
}
echo '</select>';
?>
You have one space after name in select
And your option hasn't value attribut