I have a date selector for sorting a calendar.
The "year" option is currently set to show options for: "Current year + 2, with the current year selected"
What I want is actually: "Every year, starting in 2012, with the current year selected and also show the next one year"
I can't sort out the math for the query.
code:
<label for="year">
<span class="label">Year</span>
<select name="year">
<?php
$year = (isset($_GET['year']) ? $_GET['year'] : date('Y'));
for ($i=date('Y');$i <= (date('Y')+2);$i++)
{
$checked = ($i == $year ? "selected" : "");
echo '<option value="'.$i.'" '.$checked.'>'.$i.'</option>';
}
?>
</select>
</label>
What about something like this?
<select name="year">
<?php
$initialYear = 2012;
$currentYear = date('Y');
for ($i=$initialYear;$i <= $currentYear+1 ;$i++)
{
$checked = ($i == $currentYear ? "selected" : "");
echo '<option value="'.$i.'" '.$checked.'>'.$i.'</option>';
}
?>
</select>
Related
<select name="year" >
<option value="0000"<?php echo $year == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i--) {
$selected = '';
if ($year == $i) $selected = ' selected="selected"';
print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
}
?>
</select>
This is my code for the user to choose the year he entered in a combobox, but i wanted to make a 2 year interval like 2000-2002 and etc until the present year. Help me how? Thank you!
You can use a $i=$i-2 instead of $i--.
Something like that:
<select name="year" >
<option value="0000"<?php echo $year == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i=$i-2) {
$selected = '';
$year2 = $i-2;
if ($year == $i) $selected = ' selected="selected"';
print('<option value="'.$year2. " " . $i .'" '.$selected.'> '.$year2.' '.$i.'</option>'."\n");
}
?>
</select>
I am trying to create two drop downs with HTML and PHP. The first is an auto submit that sends a month to a session variable:
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<?php $month = date('n'); // current month
for ($x = 0; $x < 12; $x++) { ?>
<option value="<?php echo date('m', mktime(0,0,0,$month + $x,1)); ?>"
<?php echo date('m', mktime(0,0,0,$month + $x,1)) == $_SESSION['month'] ? "selected='selected'":""; ?> >
<?php echo date('F Y', mktime(0,0,0,$month + $x,1)); ?>
<?php } ?>
</select>
</form>
This works great. But I then need a second drop down with the day name and number:
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Select Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) {
$tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date)); { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php echo $weekday." ".$i; ?>
<?php } ?>
<?php } ?>
</option>
<?php } ?>
</select>
</form>
This uses a temp date with a session variable as '2014' which I set before. I need to remove the session year variable and get the first drop down to know which year the month selected is and then pass this to the temp date so that it populates the day name and number correctly for the next year (2015) if you choose January onwards. At the moment it only shows the day name and number from the current year (2014) in this case.
<?php
if(isset($_POST)) {
$date = explode('-', $_POST['month']);
$year = $date[0];
$month = $date[1];
echo "Year: ".$year." Month: ".$month;
}
?>
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('+%d months', $i));
$value = date('Y-m', $time);
$label = date('F Y', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
</select>
This gives me the year and month as separate values.
I had to check the print out of the first script, because it didn't make much sense...
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<option value="11"
>
November 2014 <option value="12"
>
December 2014 <option value="01"
>
January 2015 <option value="02"
>
February 2015 <option value="03"
>
March 2015 <option value="04"
>
April 2015 <option value="05"
>
May 2015 <option value="06"
>
June 2015 <option value="07"
>
July 2015 <option value="08"
>
August 2015 <option value="09"
>
September 2015 <option value="10"
>
October 2015 </select>
</form>
Might be I'm missing something, because it seemed to work, but it doesn't seem like you're closing the option tags. You are doing this:
<option value="11">November 2014
<option value="12">December 2014
Instead of this:
<option value="11">November 2014</option>
<option value="12">November 2014</option>
This doesn't seem to be the issue though, at least not in chrome, because when I checked the head data being sent, it was just fine:
month: 11
What happens when you send in the variable is that whatever page you send this form data with the method POST against will have to handle it further on. In PHP you do that with the global variable $_POST, in this case $_POST['month'].
if(!empty($_POST['month']))
{
echo $_POST['month'];
}
If you want to make it a session variable, the page you send the form data to will have to communicate with your browser to set this (through the reply HTTP header field). To do anything related to session in PHP you must first start a session using the session_start() before any HTML/data is sent against the browser, and the same goes for if you want to set a session variable using for example session_set_cookie_params. You also got setcookie, which also must be used before any data/HTML is sent to the browser and the variables can be accessed through $_COOKIE.
See if you can make some sense out of this, I've just used $_POST in this case, you can swap it with session or cookie and remember to send it before any data/html to the browser.
<?php
$selmonth = 0;
if(!empty($_POST['month']))
{
$selmonth = $_POST['month'];
}
$month = date('n'); // current month
echo '<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>';
for($i=0; $i<12; $i++)
{
$monthtime = mktime(0,0,0,$month + $i,1);
$monthnum = date('m', $monthtime);
echo '
<option value="'.$monthnum.'"'.
($monthnum == $selmonth ? ' selected="selected"' : '').
'>'.date('F Y', $monthtime).'</option>';
}
echo '
</select>
</form>';
?>
<select required="" class="btn btn-warning btn-raised" onchange="loadMonthYear(this)">
<?php
$fdt = date('Y-m-1');
$tdt = date('Y-m-1', strtotime("-12 months"));
while (date('Ymd', strtotime($fdt)) >= date('Ymd', strtotime($tdt))) {
echo '<option value="'.date('M, Y', strtotime($fdt)).'"><a data-tag="' . date('M, Y', strtotime($fdt)) . '" href="javascript: void(0);">' . date('M, Y', strtotime($fdt)) . '</option>';
$fdt = date('Y-m-1', strtotime("-1 months", strtotime($fdt)));
}
?>
</select>
I have a form where the user inserts and selects some data in a form, one of the select fields is his/her birth year.
The drop-down list is populated fine, BUT after submitting the form for validation, I can not preserve the selected year and the user has to re-select it again!
This is what I've done:
<select size="1" name="birthYear" tabindex="7">
// Please Select Option
<option selected value="-1" <?php if(isset($_POST['birthYear']) && $_POST['birthYear'] == '-1') { echo 'selected="selected"'; } ?> >Please Select</option>
// populate birth years range
<?php
$currentYear = date('Y');
$minimumBirthYear = $currentYear - 10;
$MaximumBirthYear = $currentYear - 100;
for($i = $minimumBirthYear; $i >= $MaximumBirthYear; $i--){
echo '<option value="'.$i.'">'.$i.'</option><br />';
}
?>
</select>
Can you please help me in applying the
<?php if(isset($_POST['birthYear']) && $_POST['birthYear'] == '-1') { echo 'selected="selected"'; } ?> >
in the for loop? I've tried it in different ways but with no luck!
Thanks in advance...
Try this:
for($i = $minimumBirthYear; $i >= $MaximumBirthYear; $i--){
echo '<option '.(isset($_POST['birthYear']) && $_POST['birthYear'] == $i ? 'selected' : '').' value="'.$i.'">'.$i.'</option><br />';
}
<select size="1" name="birthYear" tabindex="7">
// Please Select Option
<option selected value="-1">Please Select</option>
// populate birth years range
<?php
$currentYear = date('Y');
$minimumBirthYear = $currentYear - 10;
$MaximumBirthYear = $currentYear - 100;
for($i = $minimumBirthYear; $i >= $MaximumBirthYear; $i--)
{
echo sprintf("<option value='%s' %s >%s</option><br/>", $i, isset($_POST['birthYear']) && $_POST['birthYear']? "selected='selected'": "", $i);
}
?>
I have an HTML select tag like this:
<select name="since_date">
<option value="<?php $sixmonths = date('Y-m-d', strtotime('-6 months')); echo $sixmonths; ?>">6 months</option>
<option value="<?php $fivemonths = date('Y-m-d', strtotime('-5 months')); echo $fivemonths; ?>">5 months</option>
<option value="<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>">4 months</option>
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
</select>
And I want when for example 4 months is selected a new select tag to appear like this one:
<select name="until_date_4months">
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
<option value="<?php $now = date('Y-m-d'); echo $now; ?>">Now</option>
</select>
Every time a period is chosen (Ex. 6 months, 5 months) a new select must appear with options lower than the selected one plus a new option "Now" like above. How is this possible? What is the approach? I want to use these information with a submit button. (Send with JQuery a GET request to a specific PHP file <- this I know how to handle).
I can for example use JavaScript like this:
function sinceDateValue(selection) {
if (selection.value == "<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>") {
document.getElementbyId(until_date_4months).style.display = "block"
}
}
and use for the second submit id="until_date_4months" and style="display: none;". Also for the first submit onchange="testValue(this); but this will just show the submit and I want to send only two information with the form: since_date and until_date...
You can create the options dynamically using a loop:
<?php for ($i = 6; $i >= 0; $i--) { ?>
<option value="<?php echo date('Y-m-d', strtotime('-' . $i . ' months')); ?>">
<?php echo ($i == 0) ? 'Now' : $i . ' months'; ?>
</option>
<?php } ?>
As for the jQuery, you can grab the elements based on the one selected, and populate your select box with those:
$('select[name="since_date"]').on('change', function() {
var newOpts = $('option:selected', this).nextAll().clone();
$(newOpts).show().appendTo('#example');
});
Here's a complete example fiddle
You can do smomething like this, all you need to do is set the right values of the options.
$("#one").change(function(){
var htmlOf2 = "", d;
for(var i=1;i<parseInt($(this).find("option:selected").text()) + 1;i++)
{
d = new Date();
d = d.setMonth(d.getMonth() + (i * -1));
// This sets the right month, but is not set as a value yet. I don't know what format you want
htmlOf2 += "<option value=''>" + (i + " months") + "</option>";
}
htmlOf2 += "<option>NOW</option>";
$("#two").html(htmlOf2);
}).trigger("change");
Live fiddle: http://jsfiddle.net/5qaw7/1/
Hi I've data for year store as serialize like this
a:2:{i:0;s:4:"2011";i:1;s:4:"2013";}
and have a list of predefined year like this:
$current_year = date('Y');for($year = 2011; $year < $current_year; $year++) { $year;}
So, I want to populate a list of checkboxes like below:
[x] 2011
[ ] 2012
[x] 2013
...
If the year is not in the predefined year (in this case 2011,2012 and 2013), the year should be unchecked.
I've search but so far the nearest solution is not in PHP
$years = unserialize($mysql_years);
$current_year = date('Y');
for ($year = 2011; $year < $current_year; $year++) {
$checked = '';
if (in_array($year, $years)) {
$checked = ' checked';
}
echo "<input type=checkbox value=$year$checked>";
}
I have found the solution. Check this.
<?php
$dat='a:2:{i:0;s:4:"2011";i:1;s:4:"2013";}';
$data=unserialize($dat);
$current_year = date('Y');
for($year = 2011; $year <= $current_year; $year++)
{
if(in_array($year,$data)) { $checked="CHECKED"; } else { $checked=""; } ?>
<input type="checkbox" value="<?php echo $year; ?>" name="year[]" <?php echo $checked; ?> > <?php echo $year; ?> <br/>
<?php } ?>