Option Menu with Date Shows as 1969-12-31 - php

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>

Related

PHP - Edit page , How to set default dropdownlist that have option from MySQL?

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.

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

Option value Not displaying in drop-down menu?

I'm using a date range on a web page to aggregate MySQL data and present it based on the selected time frame. For some reason the values for each option in the drop-down menus are not displaying. Here's the PHP I'm using:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?><?=$date ?></option>
<?php } ?>
</select>
And here's the HTML output:
<option value="2015-01-03" selected="selected" 2015-01-03<="" option=""></option>
The weirdest part is this was working for the longest time and suddenly the dates in both menus vanished. Any ideas why?
This happened becuase you are missing the ending > of tag
Modified code:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option value="<?=$date ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?=$date ?>
</option>
<?php } ?>
</select>
Your PHP snippet is missing a closing >:
<select name="date1" title="<?=$date1 ?>">
<?php foreach($availableDates as $date) { ?>
<option
value="<?=$date ?>"
<?php if($date == $date1) { ?> selected="selected"<?php } ?>
>
<?=$date ?>
</option>
<?php } ?>
</select>
Try this way also :
<select name="per1" id="per1">
<option selected="selected">Choose one</option>
<?php
foreach($names as $name) { ?>
<option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
<?php
} ?>
</select>
Now you can put your code here.
1. Add > tag closer after selected attribute
2. Remove shorthanded <?= ?> tag which is unwanted while you also using <?php ?>
<select name="date1" title="<?php print $date1; ?>">
<?php foreach($availableDates as $date) {
?><option value="<?php print $date; ?>"<?php if($date == $date1) { ?> selected="selected"<?php } ?>>
<?php print $date; ?>
</option>
<?php } ?>
</select>
I suggest this less error prone code.
$selHTML = '<select name="date1" title="'.$date1.'">';
foreach($availableDates as $date) {
$sel = ($date == $date1)?" selected":"";
$selHTML .= '<option value="'.$date.'"'.$sel.'>'.$date.'</option>';
}
$selHTML .= '</select>';
echo $selHTML;

HTML Option tag with selected value from PHP

Can you please help me out as to what I am doing wrong with this code:
<option value="">------------ Select ------------</option>
<?php while (($row = mysql_fetch_array($tech))) {
?>
<option value= <?php echo $row['id'] ?>
<?php echo $selectedtechname ?>
<?php echo "selected" ?>
>
<?php echo $row['technician_name'] ?></option>
<?php } ?>
All variables are working fine since I used Var_dump and they echo correctly so no errors is SQL etc. What I need to do is pass a php statement in the option tag of HTML to simply select the value of $selectedtechname from the list. The value to post is $row['id'] but the selected value to show should be $selectedtechname
Thanks
I see a similar post
populate a select box with php mysql
while($row = mysql_fetch_assoc($result)) {
if ($row['technician_name'] == $selectedtechname) {
echo '<option value=\"'.$row['id'].'" selected>'.$row['id'].'</option>';
} else {
echo '<option value=\"'.$row['id'].'">'.$row['id'].'</option>';
}
}
You can try the following:
$selected='';
<option value="">Select a value</option>
<?php
while ($row = mysql_fetch_array($tech)) {
?>
<?php
if($selectedtechname == $row['selectedtechname'])
{
$selected='selected';
}
else
{
$selected='';
}
?>
<option value="<?php echo $row['id']?>" <?php echo $selected;?> ><?php echo $row['value_to_display'];?></option>
<?php
}
?>

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>";
}
}

Categories