Fetch Date from mysql db into dropdown box in PHP - 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>";
}
}

Related

PHP getting value from a dynamic select list

I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks
You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"

Option Menu with Date Shows as 1969-12-31

I have an option menu that works properly when there are records to display. I sometimes have a new item that still does not have records and I would like the option menu not to display any dates. Instead it displays 1969-12-31.
<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
<?php
do {
?>
<option value="<?php echo $row_RecordsetDate['date']?>"<?php if
($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?
php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date']));
?></option>
<?php
} while ($row_RecordsetDate = mysql_fetch_assoc($RecordsetDate));
$rows = mysql_num_rows($RecordsetDate);
if($rows > 0) {
mysql_data_seek($RecordsetDate, 0);
$row_RecordsetDate = mysql_fetch_assoc($RecordsetDate);
}
?>
</select>
How can I have the option menu display blank if no records or just state no date available?
<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
<?php
while($row = mysql_fetch_assoc($RecordsetDate)){
if(isset($row['date'])){
if(strlen($row['date'])){
echo '<option value="'.$row['date'].'"';
if($varDate_Recordset1 == $row['date']){
echo 'selected';
}
$date = date('l, F d, Y',strtotime($row['date']));
echo '>'.$date.'</option>';
}
}
}
?>
</select>
Through trial and error, I came up with the following solution. I placed an if else statement in the option value.
<option value="<?php echo $row_RecordsetDate['date']?>"<?php if ($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date'])); $rows = mysql_num_rows($RecordsetDate); if(!empty($rows)){
echo $dates;
}else{
echo ""; }?></option>
Below is the complete solution.
<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
<?php
do {
?>
<option value="<?php echo $row_RecordsetDate['date']?>"<?php if ($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date'])); $rows = mysql_num_rows($RecordsetDate); if(!empty($rows)){
echo $dates;
}else{
echo ""; }?></option>
<?php
} while ($row_RecordsetDate = mysql_fetch_assoc($RecordsetDate));
$rows = mysql_num_rows($RecordsetDate);
if($rows > 0) {
mysql_data_seek($RecordsetDate, 0);
$row_RecordsetDate = mysql_fetch_assoc($RecordsetDate);
}
?>
</select>

how to add check bo0x in selection box with multiple selection box

I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);

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
}
?>

selected state mysql array

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>

Categories