While editing the record from database, i display already selected value and option to select other values too. But i want to avoid already selected value to display twice in dropdown. Not getting how to do it
here is my code
<label class="control-label">Sales Area</label>
<?php
$sql5 = "SELECT * FROM sales_area ORDER BY name";
$query5 = mysqli_query($con, $sql5);
?>
<select name="area" class="form-control" required>
<option value="<?php echo $row['sales_area']; ?>"><?php echo $row['areaname']; ?></option>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>"><?php echo $rs5["name"]; ?></option>
<?php } ?>
</select>
in $row['sales_area'], data already present in database, this should not display again.
haven't tested it, but should be something like this:
<select name="area" class="form-control" required>
<?php while ($rs5 = mysqli_fetch_array($query5)) { ?>
<option value="<?php echo $rs5["id"]; ?>" <?php if($rs5["id"] == $row['sales_area'] ) { echo "selected"; } ?> ><?php echo $rs5["name"]; ?>
</option>
<?php } ?>
</select>
Related
I want to echo the selected value from the database to update it then store it
for example I have an asset with category printers from table category which contains other categories and when I want to edit this asset on the edit page I should get a dropdown list contains all the categories and selected on printers then if I want to change it I will if not leave unchanged
The array is drop-down from table category inner joined with user_asset table in the database by asset_category as a foreign key
this is what I have done so far
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?>
</option>
<?php }?>
</select>
</div>
You can add if check if ($row['cate_name'] == 'computer') { ?> and then add selected to this option:
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required >
<?php while( $row = mysqli_fetch_array($result)) {
if ($row['cate_name'] == 'computer') { ?>
<option value="<?php echo $row['category_id'];?>" selected><?php echo $row['cate_name'];?></option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row['cate_name'];?></option>
<?php }
}?>
</select>
Notice: If you have multiple elements with that category it will select the last one.
the answer is very simple.. let's put this code
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {
if($row['cate_name']== printers) { ?>
<option value="<?php echo $row['category_id'];?>" selected="selected">
<?php echo $row['cate_name'];?> </option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?> </option>
<?php }?>
</select>
</div>
The logic is that using while loop, checking the condition using if class, and when it satisfies make it as selected. Then it will be echo as selected Value.
I need to get selected value from db into datalist box.Tell me how to do it. Here is the code.
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
?>
<option value="<?php echo $row['Rank_Name'];?>"></option>
<?php
}
?>
</datalist>
If i understood right , you need to select value in dropdownlist with other value also. You can achieve this by doing this
<?php
$select1="select Rank_Name from ranks where Rank_Status=1";
$q=mysqli_query($select1) or die($select1);
$row=mysqli_fetch_array($q); //here you are getting name of person whose rank is 1
?>
<datalist id="Rank_Name">
<?php
$s="select * from ranks ";
$q=mysqli_query($s) or die($s);
while($r=mysqli_fetch_array($q))
{ ?>
<option value="<?php echo $r['Rank_Name']; ?>"<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>>
<?php echo $r['Rank_Name']; ?>
</option>
<?php } ?>
</datalist>
In above code, this line <?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?> check if value are same from first query ,and if same then that option will be get selected automatically
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
echo "<option value=".$row['Rank_Name']."></option>";
}
?>
</datalist>
try this code.
im using echo <option> with while loop
Here is my PHP Code, I am trying to add a scroll bar to my autocomplete search, I have retrieved data from database and wanted to add a scroll bar.:
<?php
include('connection.php');
$result1 = mysql_query("select * from legal_pashto");
$result2 = mysql_query("select * from legal_dari");
$result3 = mysql_query("select * from pashto_pashto");
$result4 = mysql_query("select * from dari_pashto");
?>
<datalist id="words">
<?php while($row = mysql_fetch_assoc($result1)){?>
<option value="<?php print $row['english_word']; ?>
<option value="<?php print $row['pashto_word']; ?>
<option value="<?php print $row['pashto_word1']; ?>
<option value="<?php print $row['pashto_word2']; ?>
<option value="<?php print $row['pashto_word3']; ?>
<option value="<?php print $row['pashto_word4']; ?>
<?php }?>
<?php while($row2 = mysql_fetch_assoc($result2)){?>
<option value="<?php print $row2['english_word']; ?>
<option value="<?php print $row2['dari_word']; ?>
<option value="<?php print $row2['dari_word1']; ?>
<option value="<?php print $row2['dari_word2']; ?>
<option value="<?php print $row2['dari_word3']; ?>
<option value="<?php print $row2['dari_word4']; ?>
<?php }?>
<?php while($row3 = mysql_fetch_assoc($result3)){?>
<option value="<?php print $row3['pashto_word']; ?>
<option value="<?php print $row3['pashto_meaning']; ?>
<option value="<?php print $row3['pashto_meaning1']; ?>
<option value="<?php print $row3['pashto_meaning2']; ?>
<option value="<?php print $row3['pashto_meaning3']; ?>
<option value="<?php print $row3['pashto_meaning4']; ?>
<?php }?>
<?php while($row4 = mysql_fetch_assoc($result4)){?>
<option value="<?php print $row4['dariword']; ?>
<option value="<?php print $row4['darimeaning']; ?>
<option value="<?php print $row4['darimeaning1']; ?>
<option value="<?php print $row4['darimeaning2']; ?>
<option value="<?php print $row4['darimeaning3']; ?>
<option value="<?php print $row4['darimeaning4']; ?>
<?php }?>
</datalist>
I am not very good when it comes to html but in my opinion adding your PHP code may help other help you. Just saying.
Please modify this code so that i will get 2 option value from this code
<select name="student_class1" class="input-xlarge" >
<option value="null">--Select Class---</option>
<?php $sel_service = "select * from all_services where school_id='$school_id'";
$sel_service= mysql_query($sel_service);
while($display_class= mysql_fetch_assoc($sel_service))
{ ?>
<option value="<?php echo $display_class['sub_cat_id']; ?>"><?php echo ucfirst($display_class['sub_cat_name']); ?></option>
<?php } ?>
student_class1 giving me value sub_cat_id
i want
student_class2 will give me sub_cat_name
iwant both values by one select
plz modify it
i already spent my whole sunday on this problem
something like?
<form action="" method="get" accept-charset="utf-8">
<select name="student_class1" class="input-xlarge" >
<?php
$sel_service = "select * from all_services where school_id='$school_id'";
$sel_service= mysql_query($sel_service);
while ($row = mysql_fetch_assoc($sel_service)) {
?>
<option value="null">--Select Class---</option>
<option value="<?php echo $row['sub_cat_id']; ?>"></option>
<option value="<?php echo $row['sub_cat_name']; ?>"></option>
<?php
}
?>
</select>
</form>
This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>