Populate checkbox from array - php

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

Related

Create a dropdown menu with range or years automatically

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>

Filter days of week and just show working days in php

I am trying to create a drop down with just the working days of the week on. Monday - Friday. Here's my code:
<?php if ($_SESSION['month'] == $current_month) { $current_day = date("j") + 1;} else {$current_day = 1;} ?>
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date));
echo $weekday." "; ?>
<?php echo $i; ?>
</option>
<?php } ?>
</select>
</form>
This gives me the days of the week for the current month, but it shows all days. How can I only show Monday - Friday?
It looks like $weekday is getting the names for you. Just do a nocase string compare:
$weekday = date('D', strtotime($tmp_date));
if (strcasecmp($weekday, 'Sun') != 0
&& strcasecmp($weekday, 'Sat') != 0){
// Do something with valid days
}
<?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i; ?>
<?php if (!in_array(date('w', strtotime($tmp_date)), array(0, 6)) { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
$weekday = date('D', strtotime($tmp_date));
echo $weekday." "; ?>
<?php echo $i; ?>
</option>
<?php } ?>
This is a lot clearer then strtotime():
$start = DateTime::createFromFormat('Y-n-j', $_SESSION['year'].'-'.$_SESSION['month'].'-01');
$daysInMonth = $start->format('t');
$end = new DateTime("+{$daysInMonth} Days");
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $day) {
if (in_array($day->format('D'), array('Sat', 'Sun'))) continue;
printf('<option value="%s"%s>%s %u</option>',
$day->format('j'),
($_SESSION['day'] == $day->format('j')) ? ' selected' : '',
$day->format('D'),
$day->format('j')
);
}
Demo
This works for me:
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) {
$tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date));
if (strcasecmp($weekday, 'Sun') != 0
&& strcasecmp($weekday, 'Sat') != 0){ ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php echo $weekday." ".$i; ?>
<?php } ?>

Php.Advance weekly calendar one week [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Get next/previous ISO week and year in PHP
I am trying to write a script that will display the days of a week in a table and that will advance a week if a button is clicked. I have managed to get it working up until the point where it reaches the end of the year and then the dates all go wrong. He is what I have so far...
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
if($week_number < 10){
$week_number = "0".$week_number;
}
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" />
<input ype="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
Some of the values have been echo'd so I can check the values that are being passed are correct and I know I have probably taken extra steps I didn't need to but I am fairly new to this and wanted to make the code easier to folllow whilst I get it working. As I said, the add button works a treat until it hits new year.
Thanks
Ok, made some advancement, works fine until it gets to 2012 then it just runs through 2012 again rather than starting 2013
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else if(isset($_POST['back_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '-1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
/*if($week_number < 10){
$week_number = "0".$week_number;
}*/
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
In my opinion you are going to be way better served to make all your calculation based on a unix timestamp value and then convert to string only as needed for output. That way you don't have to deal with week number problems (i.e. week 0), you are not limited to having Monday be the first day of each week (as as is the basis of calculation in date("W")), and you won't have to make a bunch of hacks to look for edge conditions.
So assuming that $_POST['last_week'] is in your d-m-Y format something like this:
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
For the part where you are looping through the week to display you can use something like this:
for ($i = 0; $i < 7; $i++) {
$current_day_ts = $display_week_ts + ($i * 3600 *24);
echo date('d-m-Y', $current_day_ts);
}

How to set default choice in drop down list on first page load using PHP?

I created a drop down list with the below code, but I want to set a default choice on the first load of the page (i.e., show most recent date, not the first set of options). How can I do this?
<?php
$startyear = "";
$startmonth = "";
$startday = "";
$endyear = "";
$endmonth = "";
$endday = "";
$year = range(1998,2012);
$month = range(01,12);
$day = range(01,31);
if($_SERVER['REQUEST_METHOD']=='POST')
{
foreach($_POST as $key=>$value)
{
if(is_numeric($value))
{
$$key = $value;
}
}
}
?>
Form stuff here
<form name='update' action='' method='POST'>
Start: <select name='startyear'>
<?php foreach(array_reverse($year) as $y):?>
<option value="<?php echo $y?>"<?php echo((isset($startyear) && $startyear == $y)?' selected':null)?>><?php echo $y?></option>
<?php endforeach;?>
</select>
<select name='startmonth'>
<?php foreach($month as $m): $m = str_pad($m, 2, "0", STR_PAD_LEFT);?>
<option value="<?php echo $m;?>"<?php echo ((isset($startmonth) && $startmonth == $m)?' selected':null)?>><?php echo $m;?></option>
<?php endforeach;?>
</select>
<select name='startday'>
<?php foreach($day as $d): $d = str_pad($d, 2, "0", STR_PAD_LEFT);?>
<option value="<?php echo $d;?>"<?php echo ((isset($startday) && $startday == $d)?' selected':null)?>><?php echo $d;?></option>
<?php endforeach;?>
</select>
<input type='submit' value='View'/>
</form>
if($_SERVER['REQUEST_METHOD']=='POST')
{
...
} else {
// Set your defaults here.
}
Is this you want ?
$cur_day = date('j');
$cur_mon = date('n');
$cur_year = date('Y');
and then inside the options loop something like this
if($y == $cur_year) echo 'selected';
then current year will be selected by default, do the same for month and day.
Since your check for being selected is done like this:
<?php echo ((isset($startday) && $startday == $d)?' selected':null)?>
Simply define your variables with a default value, they are empty!
$startyear = "";
$startmonth = "";
$startday = "";
$endyear = "";
$endmonth = "";
$endday = "";

Select box: how to populate days php

Recently i ask how to populate years and i thought that i have all the answer i need. Thing is that i have a new problem with days. The problem seems to be more of mysql than php. In my table users, birthday field, i put "date" as the type which is in this format: 0000-00-00. Now when i use the current select box "day", it inserts 2 instead of 02 and then compromises the whole format when i join $year.$month.$day giving me and output like 1988-11-. How can i solve this? This is the code for days (the year is similar, and it's in the link above). Thanks
<?
$bday = $r['birthday'];
$part = explode("-", $bday);
$year = $part[0];
$month = $part[1];
$day = $part[2]; ?>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo $i. '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo $i;?></option> <? }?>
</select>
You can try
$bday = $r['birthday'];
$part = explode("-", $bday);
$year = $part[0];
$month = $part[1];
$day = $part[2]; ?>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo str_pad($i, 2, "0", STR_PAD_LEFT) . '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo str_pad($i, 2, "0", STR_PAD_LEFT);?></option> <? }?>
</select>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo ($i < 10 ? '0' . $i : $i). '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo $i;?></option> <? }?>
</select>

Categories