How to set selected option in dropdown - php

This will echo dropdown value take from database that
<select name="PACKAGE_ID" id="PACKAGE_ID" ng-model="FormData.Phases" class="form-control" required>
<?php
$result=mysqli_query($conn, "select * from unifi WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {
?>
<option name="UNIFI" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
<?php $result=mysqli_query($conn, "select * from streamyx WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
<option name="STREAMYX" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
<?php $result=mysqli_query($conn, "select * from webe WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
<option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
<?php $result=mysqli_query($conn, "select * from dome WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
<option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
<?php echo $row[ "PACKAGE_NAME"]; ?>
</option>
<?php } ?>
below is how im taking value from database based on ticket id
$sql = "SELECT * FROM cusinfo WHERE TICKET_ID = '".$strid."' ";
$query = mysqli_query($conn,$sql);
$result=mysqli_fetch_array($query,MYSQLI_ASSOC);
I want to echo selected value for eg.<<< $result["PACKAGE_ID"]==$row[ "PACKAGE_NAME"] echo selected >>

Because you are using the $result variable many times with subsequent queries, you will need to preserve $result["PACKAGE_CATEGORY"] as a new variable, like this:
$selected=$result["PACKAGE_CATEGORY"];
You can write inline condition statements like this:
echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"",($row[ "PACKAGE_NAME"]==$selected?" selected":""),">{$row["PACKAGE_NAME"]}</option>";
This means if the condition is true then selected will be echoed, if false an empty string will be echoed.
The same technique on multiple lines would look like this:
echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
echo $row["PACKAGE_NAME"]==$selected?" selected":"";
echo ">{$row["PACKAGE_NAME"]}</option>";
If you don't want to use an inline condition here is the standard syntax:
echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
if($row["PACKAGE_NAME"]==$selected){
echo " selected";
}
echo ">{$row["PACKAGE_NAME"]}</option>";

Related

Duple select from database in same table but diffent proposes

First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>

How to echo the selected value from an array fetched from the database

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.

selected value get from db into Data list option using php mysql

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

calling values from database to dropdown list, doesn't show all the values

i have a table in my database called categorys, it has two columns cat_id and cat_name ,, i finally managed to get the select right but it doesn't show all of the table rows, there's 22 row it only shows 11 of them ! how can i fix that ?
here's the code i used
$sql = "SELECT cat_id, cat_name FROM categorys";
$result = $conn->query($sql);
?>
<select name="taskOption">
<?php if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { ?>
<option value="<?php echo $row['cat_id']; ?>>
<?php echo $row['cat_name']; ?>
</option> <?php } ?>
</select>
<?php } ?>
Change here, missing closing double quote "
<option value="<?php echo $row['cat_id']; ?>">
You are missing the closing quote after your option value. So it takes two times through before it closes the value. Therefore, only half your rows are showing.
<?php
$sql = "SELECT cat_id, cat_name FROM categorys";
$result = $conn->query($sql);
?>
<select name="taskOption">
<?php if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){ ?>
<option value="<?php echo $row['cat_id']; ?>">
<?php echo $row['cat_name']; ?>
</option> <?php } ?>
</select>
<?php } ?>

Displaying a list of checkboxes based on an array

I have a list of checkboxes displayed below. This shows all the contactors and allows them to be selected via check box.
<?php
$query = "SELECT * FROM form_4 GROUP BY contractors ASC";
$result = mysql_query($query);
?>
<li><select multiple="multiple" size="10" name="contractors[]">
<option value="None Yet" selected="selected">None Yet
</option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['contractors'];?>"> <?php echo $line['contractors'];?> </option>
<?php
}
?>
</select></li>
I have an array saved in another place that I would like to generate the list above but with the items in the array below already checked/selected.
<?php
$options = unserialize('contractors');
$result = mysql_query("SELECT * FROM form_2 WHERE jobname = 'testjob' GROUP BY jobname ORDER BY biddate ASC LIMIT 0, 1");
while($row = mysql_fetch_array($result))
{
$contractors = unserialize($row['contractors']);
foreach ($contractors as $contractor)
echo "" . htmlspecialchars ($contractor).' - ';
?>
Any help would be greatly appreciated.
Try this :
<option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>checked="checked" <?php }?>> <?php echo $line['contractors'];?> </option>

Categories