selected state mysql array - php

I am trying to combine a table select output with a piece of code I found to keep the selected state:
<select>
<?php
$desired_option = 'arsenal';
$arr = array('arsenal', 'aston villa', 'birmingham', 'blackpool', 'bolton');
for($i = 0; $i < count($arr); $i++) {
$selected = ($arr[$i] == $desired_option) ? 'selected="selected"' : '';
echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>";
}
?>
</select>
<select id="teams" onchange="this.form.submit();" name="teamid">
<?
include('db.php');
$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error());
while ($teamsData = mysql_fetch_array($getTeams))
{
?>
<option value="<? echo $teamsData['id']; ?>" ><? echo $teamsData['name']; ?></option>
<?
}
?>
</select>
I have tried everything. Any ideas?
Thanks :)

<select id="teams" onchange="this.form.submit();" name="teamid">
<?
include('db.php');
$selected = 'team_to_be_selected';
$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error());
while ($teamsData = mysql_fetch_array($getTeams))
{
?>
<option value="<?php echo $teamsData['id']; ?>" <?php echo ($teamsData['name'] == $selected) ? 'selected="selected"' : ''; ?>><?php echo $teamData['name'];?></option>
<?
}
?>
</select>

Related

How to set Selected value to dropdown list from database PHP/SQL

I want to set selected values in dropdown list to the one im editing.
$id = $_GET['edit'];
$result = $polaczenie->query("SELECT * FROM wizyty WHERE idwizyty=$id")
or die($mysqli->error());
while($row=mysqli_fetch_assoc($result))
{
$id = $row['idwizyty'];
$data = $row['data'];
$pracownik = $row['pracownik'];
$usluga = $row['usluga'];
$klient = $row['klient'];
$time = $row['starttime'];
$cena = $row['cena'];
}
Dropdown list
<label>Pracownik</label>
<?php
$query = "SELECT imie, nazwisko FROM `pracownicy`";
$wynik1 = mysqli_query($polaczenie, $query);
?>
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1)):;?>
<option selected="<?php echo $pracownik?>"><?php echo $row1[0] .' '. $row1[1] ;?></option>
<?php endwhile; ?>
</select><br><br>
I tried to do it like this but it's not working.
The only value for ‘selected’ attribute is ‘selected’
<option value="value" selected="selected">value</option>
So your code should look something like this
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1));?>
$value = $row1[0] .' '. $row1[1];
$selected = $value == $pracownik ? 'selected="selected"' : '';
<option <?php echo $selected?>><?php echo $value;?></option>
<?php endwhile; ?>
</select><br><br>

advice on tidying up php that is checking DB entry & changing select dropdown 'selected'

I am looking for advice and the best way to tidy up the following php code.
I am checking the database for $this->item->values[''] and changing a select dropdown's selected="selected" accordingly.
How can this be made cleaner/simpler?
<?php
$minbedroomsCheck = '';
if ( $this->item->values['min-bedrooms'] < '0' ) {
$minbedroomsCheck = 'selected="selected"';
}
$minbedroomsanyCheck = '';
if ( $this->item->values['min-bedrooms'] == '0' ) {
$minbedroomsanyCheck = 'selected="selected"';
}
$minbedrooms1Check = '';
if ( $this->item->values['min-bedrooms'] == '1' ) {
$minbedrooms1Check = 'selected="selected"';
}
$minbedrooms2Check = '';
if ( $this->item->values['min-bedrooms'] == '2' ) {
$minbedrooms2Check = 'selected="selected"';
}
$minbedrooms3Check = '';
if ( $this->item->values['min-bedrooms'] == '3' ) {
$minbedrooms3Check = 'selected="selected"';
}
$minbedrooms4Check = '';
if ( $this->item->values['min-bedrooms'] == '4' ) {
$minbedrooms4Check = 'selected="selected"';
}
$minbedrooms5Check = '';
if ( $this->item->values['min-bedrooms'] == '5' ) {
$minbedrooms5Check = 'selected="selected"';
}
?>
<select id="min-bedrooms" name="min-bedrooms" class="profile-select">
<option value="" <?= $minbedroomsCheck ?>>Min Bedrooms</option>
<option value="0" <?= $minbedroomsanyCheck ?>>Any</option>
<option value="1" <?= $minbedrooms1Check ?>>1+</option>
<option value="2" <?= $minbedrooms2Check ?>>2+</option>
<option value="3" <?= $minbedrooms3Check ?>>3+</option>
<option value="4" <?= $minbedrooms4Check ?>>4+</option>
<option value="5" <?= $minbedrooms5Check ?>>5+</option>
</select>
You can do like below,
First store the min-bedrooms count in database
Then store the min-bedrooms selected value in database,
The retrieve the min-bedrooms count and min-bedrooms selected value and do a for loop as below
<?php
$min_bedrooms = 6; // this should be stored in database
$min_bedrooms_selected = 4; //$this->item->values['min-bedrooms']
?>
<select class="profile-select" name="min-bedrooms" id="min-bedrooms">
<?php for($i = 0; $i < $min_bedrooms; $i++){ ?>
<?php if($i == $min_bedrooms_selected){ ?>
<option value="<?php echo $i;?>" selected="selected"> <?php echo $i;?></option>
<?php }else{ ?>
<option value="<?php echo $i;?>"> <?php echo $i;?></option>
<?php } ?>
<?php } ?>
</select>

Set first value of dynamic HTML option to empty

<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option value="">Select</option>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Above is the php code of the select option. Populated with data from database table, how can I set that the first value if empty. I tried adding a Select but this is the result.
The option select keeps repeating. What to do with this?
Try this
<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
?>
<option value="">Select</option>
<?php
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Just move that option line "Select" out of your php code:
<select id="section" name="section">
<option value="">Select</option>
<?php
...your php code
?>
</select>

Fetch Date from mysql db into dropdown box in PHP

I am developing user edit form where users can edit their info. I wants only that date come in combo box which was user add in table as Date of Birth. In following code I successfully pull Month into Combo box which user added in table.
IN FOLLOWING LINE I RUN SELECT QUERY TO FETCH DATA FROM DB INCLUDING YEAR, MONTH AND DAY:
<?php
$cnic_selected=$_POST['all_cnic'];
if(isset($cnic_selected))
{
$query = mysql_query("SELECT refer_id, emp_name, emp_father, emp_cnic, YEAR(emp_dob) AS byear, MONTH(emp_dob) AS bmonth, DAY(emp_dob) AS bday FROM staff_users WHERE emp_cnic = '$cnic_selected'");
$count = mysql_num_rows($query);
while($row = mysql_fetch_object($query))
{
$ref = $row->refer_id;
$name = $row->emp_name;
$fname = $row->emp_father;
$cnic = $row->emp_cnic;
$byear = $row->byear;
$bmonth = $row->bmonth;
$bday = $row->bday;
}
?>
<html>
<head>
</head>
<body>
<table width="95%">
<form name="edituserform" action="" method="Post" onSubmit="return validateform(this);">
<tr><td width="125px"><b>Referral Code</b></td>
<td><?php echo $ref; ?></td></tr>
<tr><td><b>Full Name</b></td>
<td><input type="text" name="emp_name" id="emp_name" value="<?php echo $name; ?>" />
</td></tr>
<tr><td><b>Father's Name</b></td>
<td><input type="text" name="emp_father" id="emp_father" value="<?php echo $fname; ?>" />
</td></tr>
<tr><td><b>CNIC Number</b></td>
<td><?php echo $cnic; ?></td></tr>
<tr><td><b>Date of Birth</b></td>
<td>
/* Run Loop to generate 31 days of the month */
<select name="dt">
<option value='--'>--</option>
<?php
for ($d=1; $d<32; $d++)
{
"<option value='$d'>$d</option>";
}
?>
</select>
/* Here I manually type months list. Use "Selected" to fetch that month which user select in DB */
<select name="month">
<option value="--">--</option>
<option value='01' <?php echo ($bmonth == 1) ? 'selected="selected"': ''; ?>>
Jan</option>
<option value='02' <?php echo ($bmonth == 2) ? 'selected="selected"': ''; ?>>
Feb</option>
<option value='03' <?php echo ($bmonth == 3) ? 'selected="selected"': ''; ?>>
Mar</option>
<option value='04' <?php echo ($bmonth == 4) ? 'selected="selected"': ''; ?>>
Apr</option>
<option value='05' <?php echo ($bmonth == 5) ? 'selected="selected"': ''; ?>>
May</option>
<option value='06' <?php echo ($bmonth == 6) ? 'selected="selected"': ''; ?>>
Jun</option>
<option value='07' <?php echo ($bmonth == 7) ? 'selected="selected"': ''; ?>>
Jul</option>
<option value='08' <?php echo ($bmonth == 8) ? 'selected="selected"': ''; ?>>
Aug</option>
<option value='09' <?php echo ($bmonth == 9) ? 'selected="selected"': ''; ?>>
Sep</option>
<option value='10' <?php echo ($bmonth == 10) ? 'selected="selected"': ''; ?>>
Oct</option>
<option value='11' <?php echo ($bmonth == 11) ? 'selected="selected"': ''; ?>>
Nov</option>
<option value='12' <?php echo ($bmonth == 12) ? 'selected="selected"': ''; ?>>
Dec</option>
</select>
<select name="year">
<option value='--'>--</option>
<?php
for ($y=2000; $y>1950; $y--)
{
echo "<option value='$y'>$y</option>";
}
?>
</select>
</td></tr>
</table>
</body>
</html>
Now my question is Please tell how to pull Year and Day from table into combo box which user was added while I am using Loop to generate years and days!??
Regards,
MAT
For days you are missing an echo and a check as to which day is selected
<?php
for ($d=1; $d<32; $d++)
{
"<option value='$d'>$d</option>";
}
?>
Should be
<?php
for ($d=1; $d<32; $d++)
{
if($d==$bday)
{
echo "<option value='$d' selected='selected'>$d</option>";
}
else
{
echo "<option value='$d'>$d</option>";
}
}
?>
Same for year
for ($y=2000; $y>1950; $y--)
{
if($y==$byear)
{
echo "<option value='$y' selected='selected'>$y</option>";
}
else
{
echo "<option value='$y'>$y</option>";
}
}

selected value get from db into dropdown select box option using php mysql error

I need to get selected value from db into select box. please, tell me how to do it. Here is the code.
Note: 'options' value depends on the category.
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
</select>
<?php
}
?>
I think you are looking for below code changes:
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
<option value="ASP" <?php if($options=="ASP") echo 'selected="selected"'; ?> >ASP</option>
</select>
The easiest way I can think of is the following:
<?php
$selection = array('PHP', 'ASP');
echo '<select>
<option value="0">Please Select Option</option>';
foreach ($selection as $selection) {
$selected = ($options == $selection) ? "selected" : "";
echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>';
}
echo '</select>';
The code basically places all of your options in an array which are called upon in the foreach loop. The loop checks to see if your $options variable matches the current selection it's on, if it's a match then $selected will = selected, if not then it is set as blank. Finally the option tag is returned containing the selection from the array and if that particular selection is equal to your $options variable, it's set as the selected option.
for example ..and please use mysqli() next time because mysql() is deprecated.
<?php
$select="select * from tbl_assign where id='".$_GET['uid']."'";
$q=mysql_query($select) or die($select);
$row=mysql_fetch_array($q);
?>
<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php $s="select * from tbl_new_user where type='client'";
$q=mysql_query($s) or die($s);
while($rw=mysql_fetch_array($q))
{ ?>
<option value="<?php echo $rw['login_name']; ?>"<?php if($row['clientname']==$rw['login_name']) echo 'selected="selected"'; ?>><?php echo $rw['login_name']; ?></option>
<?php } ?>
</select>
Just Add an extra hidden option and print selected value from database
<option value="<?php echo $options;?>" hidden><?php echo $options;?></option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
Select value from drop down.
<select class="form-control" name="category" id="sel1">
<?php foreach($data as $key =>$value) { ?>
<option value="<?php echo $data[$key]->name; ?>"<?php if($id_name[0]->p_name==$data[$key]->name) echo 'selected="selected"'; ?>><?php echo $data[$key]->name; ?></option>
<?php } ?>
</select>
THE EASIEST SOLUTION
It will add an extra in your options but your problem will be solved.
<?php
if ($editing == Yes) {
echo "<option value=\".$MyValue.\" SELECTED>".$MyValue."</option>";
}
?>
$option = $result['semester'];
<option >Select</option>
<option value="1st" <?php if($option == "1st") echo 'selected = "selected"'; ?>>1st</option>
<option value="2nd" <?php if($option == "2nd") echo 'selected = "selected"'; ?>>2nd</option>
<option value="3rd" <?php if($option == "3rd") echo 'selected = "selected"'; ?>>3rd</option>
<option value="4th" <?php if($option == "4th") echo 'selected = "selected"'; ?>>4th</option>
<option value="5th" <?php if($option == "5th") echo 'selected = "selected"'; ?>>5th</option>
<option value="6th" <?php if($option == "6th") echo 'selected = "selected"'; ?>>6th</option>
<option value="7th" <?php if($option == "7th") echo 'selected = "selected"'; ?>>7th</option>
<option value="8th" <?php if($option == "8th") echo 'selected = "selected"'; ?>>8th</option>
</select>
BEST code and simple
<select id="example-getting-started" multiple="multiple" name="category">
<?php
$query = "select * from mine";
$results = mysql_query($query);
while ($rows = mysql_fetch_assoc(#$results)){
?>
<option value="<?php echo $rows['category'];?>"><?php echo $rows['category'];?></option>
<?php
}
?>
</select>
You can also do like this ....
<?php $countryname = $all_meta_for_user['country']; ?>
<select id="mycountry" name="country" class="user">
<?php $myrows = $wpdb->get_results( "SELECT * FROM wp_countries order by country_name" );
foreach($myrows as $rows){
if( $countryname == $rows->id ){
echo "<option selected = 'selected' value='".$rows->id."'>".$rows->country_name."</option>";
} else{
echo "<option value='".$rows->id."'>".$rows->country_name."</option>";
}
}
?>
</select>
Answer is simple.
when u pass value from dropdown.
Just use as if else.
for eg:
foreach($result as $row) {
$GLOBALS['output'] .='<option value="'.$row["dropdownid"].'"'.
($GLOBALS['passselectedvalueid']==$row["dropwdownid"] ? ' Selected' : '').'
>'.$row['valueetc'].'</option>';
}
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php echo $options == 'PHP' ? 'selected' : ''; ?> >PHP</option>
<option value="ASP" <?php echo $options == 'ASP' ? 'selected' : ''; ?> >ASP</option>
</select>
<?php
}
?>
USING PDO
<?php
$username = "root";
$password = "";
$db = "db_name";
$dns = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
$conn = new PDO($dns,$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "select * from mine where username = ? ";
$stmt1 = $conn->prepare($sql);
$stmt1->execute(array($_POST['user']));
$all = $stmt1->fetchAll(); ?>
<div class="controls">
<select data-rel="chosen" name="degree_id" id="selectError">
<?php
foreach($all as $nt) {
echo "<option value =$nt[id]>$nt[name]</option>";
}
?>
</select>
</div>
I'm using eval() PHP function like this:
My PHP code:
$selOps1 = $selOps2 = $selOps3 = '';
eval('$selOps'. $dbRow["DBitem"] . ' = "selected";');
Then in my select box I use it like this:
<select>
<option <?=$selOps1?> value="1">big</option>
<option <?=$selOps2?> value="2">Middle</option>
<option <?=$selOps3?> value="3">Small</option>
</select>
Put value from db into a variable and check like following code example
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
This may help you.
?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res))
{
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
// Assuming $list['options'] is a coma seperated options string
$arr=explode(",",$list['options']);
<?php foreach ($arr as $value) { ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } >
</select>
<?php
}
?>

Categories