These is my code to get the information from the database. So status is the one where in my database would be either Interested or Not Interested.
while($row= mysqli_fetch_array($result))
{
$ID =$row['Particulars_ID'];
$name = $row['Name'];
$number =$row['Number'];
$status =$row['Status'];
$remarks =$row['Remarks'];
}
I would like to echo out the value Not Interested if my database shows that this person is not interested. However from my below code, it always show Interested no matter which person i click on.
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
first - you do not need the comma in the select, second - ensure that this is the only element with the id of status, third - simply check the $status value in each option and echo selected if it is.
echo "<select name = 'status' id = 'status'>
<option value='Interested'";
if($status == "Interested"){echo " selected";}
echo">Interested</option>
<option value='Not Interested' ";
if($status == "Not Interested"){echo " selected";}
echo">Not Interested</option>
</select><br>";
since the default is the first option Interested, then if ($status === "Not Interested") set the option attribute selected
<?php
if ($status === "Not Interested") $selected = "selected";
else $selected = "";
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested' $selected>Not Interested</option>
</select><br>";
Change this
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
to this
// Set the selected attributes based on the value of status
$interested = ($status === 'Interested') ? ' selected' : '';
$notInterested = ($interested === '') ? ' selected' : '';
echo <<< SELECT
<select name="status" id="status">
<option$interested>Interested</option>
<option$notInterested>Not Interested</option>
</select>
SELECT;
You don't need the value attribute if the value is the same as the text.
If there were more than two options, I recommend a loop like this:
<?php foreach ($options as $o) : ?>
<option<?php if ($o === $optionValue) ?> selected<?php endif ?>><?= optionValue ?></option>
<?php endforeach ?>
Related
I need to select value dynamically using PHP in drop down list but in my case its not working as expected. I am explaining my code below.
<?php
$citySelected = "";
if(!isset($_GET['city'])){
$citySelected = 'selected="selected"';
}
?>
<select id="selectedLoc" name="selectedLoc" class="chosen-select form-control">
<option value="">Select City</option>
<option value="0" <?php echo $citySelected; ?>>Global</option>
<?php
foreach ($locationArr as $key => $value) {
$id = $value['id'];
$city = $value['city'];
$location = $value['location'];
$selected = "";
if(isset($_GET['city']) && $_GET['city']== $value['id']) {
$selected ='selected="selected"';
}
echo "<option value='$id' $selected>$location</option>";
}
?>
</select>
Here I need when there is any query string value then it will match with respective id and select that option and if there is no query string value at all then the global option will select.
Inside if condition ';' is missing. It should be like
if(isset($_GET['city']) && $_GET['city']== $value['id']){echo ' selected="selected"';}else{echo '';}
You can also use conditional operator for code simplicity, eg:-
<?php echo (isset($_GET['city']) && $_GET['city']== $value['id'])? ' selected="selected"':''; ?>
I know only this method. this method is assume that you know the values in all <option>
<select name="agama" id="agama">
<option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
<option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
<option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
<option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
<option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
<option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>
.... the above code is example from other people not mine.
but My case is the <option> is select from database too.
I have 2 table, oav_event and oav_album
the oav_album has foreign key (event_id) from oav_event table
I want to check if row['event_id'] from oav_album table is equal to option value (from oav_event table) if true, then set selected="selected"
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['event_id']; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
the option will change depend on change in database table, so I don't know the value in option. How should I do?
<select name="event_id">
<?php
$sql = "SELECT * FROM oav_event";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$selected = "";
if($row['event_id'] == $Yourmatchvalue)
{
$selected = "selected";
}
?>
<option value="<?php echo $row['event_id']; ?>" selected="<?php echo $selected; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
</select>
may this helps your. you need to replace $Yourmatchvalue variable with your variable.
You can use $_GET as the method on your form and pass the id of the record using it:
while($row = mysqli_fetch_assoc($result)) {
if (!empty($_GET['event_id']) && $row['event_id'] == $_GET['event_id']) {
$selected = 'selected = "selected"';
} else {
$selected = '';
}
echo '<option '.$selected.' value="'.$row["event_id"].'">'.$row["event_date"].'</option>';
}
Here is a solution,
$selected_value = 'Hindu'; // This will come from database
Change option tag with this
<option value="<?php echo $row['event_id']; ?>" <?php echo ($row['event_id'] == $selected_value) ? 'selected="selected"' : ''; ?> >Event: <?php echo $row['event_date']; ?> </option>
Create one function which will create options list like this:
function setDropdownValue($selectQueue_list,$selectedVal)
{
$queueVal = '';
$selectQueue_list_res=$db->query($selectQueue_list);
while($selectQueue_list_res_row=$db->fetchByAssoc($selectQueue_list_res))
{
$val = $selectQueue_list_res_row['id'];
$name = $selectQueue_list_res_row['name'];
if($val == $selectedVal)
{
$queueVal .= "<option value='$val' selected='selected' label='$name'>$name</option>";
}
else
{
$queueVal .= "<option value='$val' label='$name'>$name</option>";
}
}
return $queueVal;
}
Then create a query:
$get_value_query="SELECT id, name FROM table";
$dropdown_selected_value = !empty($dropdown_value) ? $dropdown_value: ''; // Pass value which you want to be selected in dropdown
Then call this function:
$dropdown_options = setDropdownValue($get_value_query, $dropdown_selected_value);
Later when you get dropdown options in $dropdown_options, use jquery to populate the dropdown, like this:
$('#dropdown_select_id').html("$dropdown_options");
Give it a try, and let me know.
I has created dynamic list in php.
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo<option value='".$row['path']."'>'".$row['studentName']."'</option>";
}
?>
</select>
When I'm selected the list it give empty string. But when I'm not selected anything it return the first value which is Student Name. I tried to get the value from this line.
if(isset($_POST['student'])){
$selectedName = $_POST['student'];
}
I also try to checked whether the isset is set or empty. The isset is set but its empty.
This is the only solution Google provided.Ideas?
Edit
I have solved the problem. The explanation on the answer below.
At first I thought $_POST['student'] returning the text inside the <option> tag and I was wrong. I learnt that it return the value which is should be set in <option> attribute, So I just set the value to $row['studentName'], because $row['path'] returned empty string. Correct me if what I said is wrong. So basically, the code looks like below.
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo<option value='".$row['studentName']."'>'".$row['studentName']."'</option>";
}
?>
</select>
Thanks for all your responses. Your responses made me think what should I do.
Use below code:
<select name="student" id="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<option name='".$row['studentName']."' value='".$row['path']."'>'".$row['studentName']."'</option>";
}?>
</select>
You should be use this line isset($_POST['studentName']) && $selected = $_POST['studentName'] == $row['studentName'] ? 'selected = "selected"' : null; for selected option
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
$selected = isset($_POST['studentName']) && $_POST['studentName'] == $row['studentName'] ? 'selected = "selected"' : null;
echo "<option ".$selected." value='".$row['path']."'>".$row['studentName']."</option>";
}
?>
</select>
#dongcheng This is the same code but with better alignments. So you can see your code clearly.
<select name="student">
<option selected disable class="hideoption">Student Name</option>
<?php
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo '<option value="' . $row['path'] . '">' . $row['studentName'] . '</option>';
}
?>
</select>
use
$selectedStudent = !empty($_POST['student']) ? $_POST['student'] : '';
if ($selectedStudent) {
// your code
}
I have created a drop down list which is working perfectly fine, its fetching data from database and showing it in the drop-down list. The problem is that I am unable to identify that where to use 'selected'attribute in the select tag. Right now whatever the field I select it opens it, but in the drop down list it shows the first given name. I also tried to use 'selected' attribute, but it was showing the last item name in the drop-down list.
Kindly check it and guide me how to use 'selected' attribute in the loop.
<?php
//Drop Down List
$sub_query = "select * from sub_categories where category_id=$category_id ";
if (!$sub_query_run = mysql_query($sub_query))
{
echo mysql_error();
}
else
{
echo "<select name='menu1' id='menu1' >
<option value='#'> All</option> ";
while ($sub_query_fetch = mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch = mysql_fetch_array($sub_query_run);
$sub_category_id2 = $sub_query_fetch['sub_category_id'];
$sub_category_name = $sub_query_fetch['sub_category_name'];
echo "<option value='earings2.php?sub_category_id=$sub_category_id2' >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}
}
follow this example..
<select name="cate" id="cate" class="reginput" >
<option value="">Select Category</option>
<?php $s2="select * from tbl_category order by cate_name";
$q2=mysql_query($s2);
while($rw2=mysql_fetch_array($q2)) {
?>
<option value="<?php echo $rw2['id']; ?>"<?php if($rw2['id']==$row['cate_id']) echo 'selected="selected"'; ?>><?php echo $rw2['cate_name']; ?></option><?php } ?>
</select>
You have to add a condition for the selected item.
echo "<option value='earings2.php?sub_category_id=$sub_category_id2'";
if ($sub_category_id2 == $MATCHING_CATEGORY_ID) echo " selected";
echo ">".htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name'])."</option>";
Where $MATCHING_CATEGORY_ID is the category id that will be selected.
<option value="#" selected>ALL</option>
"selected" should be included in option tag
Try it like,
// get the category id from request parameter
$sc_id=isset($_REQUEST['sub_category_id']) ? $_REQUEST['sub_category_id'] : "";
while ($sub_query_fetch= mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch= mysql_fetch_array($sub_query_run);
$sub_category_id2= $sub_query_fetch['sub_category_id'];
$sub_category_name= $sub_query_fetch['sub_category_name'];
$sel='';
if($sc_id==$sub_category_id2)// get the selected item
$sel='selected="selected"';
echo "<option value='earings2.php?sub_category_id=$sub_category_id2' ".$sel." >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}
you need to use select in <option> tag
try this
while ($sub_query_fetch= mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch= mysql_fetch_array($sub_query_run);
$sub_category_id2= $sub_query_fetch['sub_category_id'];
$sub_category_name= $sub_query_fetch['sub_category_name'];
$selected = ($isSelected == $sub_category_id2) ? 'selected' : ''; should be your selected condition fetch from db
echo "<option ".$selected."
value='earings2.php?sub_category_id=$sub_category_id2' >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}
i would like to give a dropdown a default value with php / mysql queries
like in the db table users there is a record saying usergroup
and i want it so the dropdown default value changes to the value of that record i got this code so far:
Usergroup: <select value=\"" . $usergroup . "\" name='usergroup'>
<option value='4'>Administrator</option>
<option value='3'>Moderator</option>
<option value='2'>Donator</option>
<option value='1'>Regular Member</option>
</select>
and php part:
$username = mysql_real_escape_string(htmlentities(stripslashes($_POST['username'])));
$query="SELECT * FROM users where `username` like '{$username}'";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i=0;
while ($i < $num) {
$username2=mysql_result($result,$i,"username");
$name=mysql_result($result,$i,"name");
$usergroup=mysql_result($result,$i,"usergroup");
$email=mysql_result($result,$i,"email");
$ip=mysql_result($result,$i,"ip");
$profpic=mysql_result($result,$i,"profilepic");
$id=mysql_result($result,$i,"id");
$i++;
}
thanks in regards!
i gotten it to work with a few if statements, i was planning to use it that way but didn't want the code to become too messy:
if ($usergroup == 4) {
echo "<select name='usergroup'><option value='4' selected='selected'>Administrator</option><option value='3'>Moderator</option><option value='2'>Donator</option><option value='1'>Regulat Member</option></select><br>";
}
if ($usergroup == 3) {
echo "<select name='usergroup'><option value='4'>Administrator</option><option value='3' selected='selected'>Moderator</option><option value='2'>Donator</option><option value='1'>Regulat Member</option></select><br>";
}
if ($usergroup == 2) {
echo "<select name='usergroup'><option value='4'>Administrator</option><option value='3'>Moderator</option><option value='2' selected='selected'>Donator</option><option value='1'>Regulat Member</option></select><br>";
}
if ($usergroup == 1) {
echo "<select name='usergroup'><option value='4'>Administrator</option><option value='3'>Moderator</option><option value='2'>Donator</option><option value='1' selected='selected'>Regulat Member</option></select><br>";
}
This is probably what you are looking for:
<?php
// $userGroup = the value from the database
$userGroup = 4;
?>
<select>
<option <?php echo ($userGroup) == 1 ? "selected" : "" ?> value="1">Administrator</option>
<option <?php echo ($userGroup) == 2 ? "selected" : "" ?> value="2">Moderator</option>
<option <?php echo ($userGroup) == 3 ? "selected" : "" ?> value="3">Donator</option>
<option <?php echo ($userGroup) == 4 ? "selected" : "" ?> value="4">Regular member</option>
</select>
If you don't recognize the if/else syntax (shorthand), check out this guide. Really handy for situations like this.