I have a php script that checks every Sunday of the week and displays data on that day.
Users can change their value for that day to "Yes" or "No'.
I want to add a possibility that the users can't change their choice after every Thursday at 23:59 before that sunday.
I only want the first Sunday not to be editable.
Since this all is in a loop that shows every Sunday until 11 Sundays in the future. I need to find a way to only do this for the first Sunday.
$datum_gisteren = new datetime('yesterday');
echo '<tr>';
echo '<td bgcolor="#3c741a" style="color:#FFFFFF;"> '.$current_user->first_name.''.' '.''.$current_user->last_name.'</td>
<form action="" method="post">';
for ($i=0; $i<=11; $i++){
$date_modified = $datum_gisteren->modify('next sunday')->format('d-m-y');
$datum = get_the_author_meta( $date_modified, $current_user->ID );
$time = '23:59';
if(date('l') === 'Thursday' && date('H:i') >= $time && $date_modified === '06-09-15') {
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
'.(($datum == '1') ? '<option value="1" selected disabled="disabled">Nee</option>' : '').'
'.(($datum == '2') ? '<option value="2" selected disabled="disabled">Ja</option>' : '').'
</select></td>';
}else{
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
<option value="1" '.(($datum == '1') ? 'selected' : '').' >Nee</option>
<option value="2" '.(($datum == '2') ? 'selected' : '').' >Ja</option>
</select></td>';
}
}
Most of the work is done (I believe) but as you can see. The $date_modified in the first if-statement is "hardcoded" (06-09-15).
I want this value to be the first next sunday. But since this is in the loop it showed every Sunday for 11 times.
How can I set this Sunday once?
Hope it makes sense!!!
You can keep additional variable for it:
$j = 0;
for ($i=0; $i<=11; $i++){
$date_modified = $datum_gisteren->modify('next sunday')->format('d-m-y');
$datum = get_the_author_meta( $date_modified, $current_user->ID );
$time = '23:59';
if(date('l') === 'Thursday' && date('H:i') >= $time && $j === 0) {
$j++;
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
'.(($datum == '1') ? '<option value="1" selected disabled="disabled">Nee</option>' : '').'
'.(($datum == '2') ? '<option value="2" selected disabled="disabled">Ja</option>' : '').'
</select></td>';
}else{
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
<option value="1" '.(($datum == '1') ? 'selected' : '').' >Nee</option>
<option value="2" '.(($datum == '2') ? 'selected' : '').' >Ja</option>
</select></td>';
}
}
but will be good to segregate php logic from front.
Thanks.
Related
Below is the code where only two options are there in dropdown list i.e. 2016 and 2017. I want to keep selected value after submit. But i m getting 2016 after selecting 2017. What changes need to be done?
<?php
$yearArray = range(2016, 2017);
?>
<select name="year" id="select2">
<option value="0">Select Year</option>
<?php
foreach ($yearArray as $year)
{
$selected = ( $year == 2016) ? 'selected' : '';
echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>
The reason why 2016 is selected always is because you are setting it to 2016.
<?php
foreach ($yearArray as $year)
{
$selected = ( $year == 2016) ? 'selected' : ''; // Here, you are setting it
echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
Instead do this,
<?php
foreach ($yearArray as $year)
{
$selected = ((isset($_POST['year']) && $_POST['year'] == $year) || ($year == '2016')) ? 'selected' : '';
echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
By default 2016 is selected, if you have submitted any other value, that will be selected.
Try this:
if((isset($_POST['year']) && $year == $_POST['year'])){
$selected = 'selected';
}else if($year == 2016){
$selected = 'selected';
}
<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 have a select box that when the page loads it sets a value selected based upon a variable. I also have 2 IF statements that checks the variable and returns the results. Here is my code I am trying to do:
<select id="satbreak1" name="satbreak1">
<?php if ($trimmed_title[0] == "Guest")
{
echo ('<option selected value="#">Select One</option>');
echo ('<option value="y">Yes</option>');
echo ('<option value="n">No</option>');
}
if ($trimmed_title[0] != "Guest")
{
echo ('<option <?php if($trimmed_satBreakfast[0] == "") echo ("selected ") ; ?> value="#">Select One</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "y") echo ("selected ") ; ?> value="y">Yes</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "n") echo ("selected ") ; ?> value="n">No</option>');
}
?>
</select>
What I need to do is if $trimmed_title[0] == "Guest" then echo out the first part, but if $trimmed_title[0] != "Guest") then echo out the second part and also check what the status of $trimmed_satBreakfast[0] is and set the selected value.
What is the best / correct way to handle this?
The first part works fine but having an php script in a echo seems to fail.
I think this might do the trick - not tested but theoretically looks ok
<select id="satbreak1" name="satbreak1">
<?php
if ( $trimmed_title[0] == "Guest" ) {
echo '
<option selected value="#">Select One</option>
<option value="y">Yes</option>
<option value="n">No</option>';
} else {
$tsb=$trimmed_satBreakfast[0];
echo '
<option selected value="#" '.( $tsb=='' ? 'selected' : '' ).'>Select One</option>
<option value="y" '.( $tsb=='y' ? 'selected' : '' ) .'>Yes</option>
<option value="n"'.( $tsb=='n' ? 'selected' : '' ).'>No</option>';
}
?>
</select>
Something like this:
<?php
$options = array(
'values' => array('', 'y', 'n'),
'titles' => array('Select One', 'Yes', 'No')
)
$index = $trimmed_title[0] == 'Guest' ? 0 : array_search($trimmed_satBreakfast[0], $options['values']);
//if ($index === false) $index = 0;
$optHtml = array();
foreach($options['values'] as $i => $val) {
$selected = $index == $i ? 'selected' : '';
$optHtml[] = "<option $selected value=\"$val\">{$options['titles'][$i]}</option>";
}
$optHtml = implode("\n", $optHtml);
?>
<select id="id" name="name">
<?php echo $optHtml?>
</select>
or any other style to split html from php:
//if ($index === false) $index = 0;
...
?>
<select id="id" name="name">
<?php foreach($options['values'] as $i => $val) {?>
<option <?=$index==$i?'selected':''?> value="<?=$val?>">
<?=$options['titles'][$i]?>
</option>
<?php } ?>
</select>
In this case HTML not used in the common php code, but php used in HTML (only general template features: loop through data and echo it).
Common purpose of this is possibility to extract template part to separate file.
It is already PHP so you have to remove the php tags
echo ('<option '.($trimmed_satBreakfast[0] == "") ? : '"selected"' : ''.' value="#">Select One</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "y") ? : '"selected"' : ''.' value="y">Yes</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "n") ? : '"selected"' : ''.' value="n">No</option>');
I'm pretty new to php and I am trying to get the value of user's selected value from 3 different fields in dropdown (day, month, year) of their birthday into unix format. I know there is an easier way of using datepicker, but I would like to know how this way works. So far the following are my coding
Register.php
<form name="registrationForm" method="post" id="registrationForm" class="registrationForm" action="processRegister.php">
<tr class="birthday-select">
<td> </td>
<td class='input1'>
<?php
if($user_profile['birthday']){
$month = substr($user_profile['birthday'], 0, 2);
$day = substr($user_profile['birthday'], 3, 2);
$year = substr($user_profile['birthday'], 6);
$date = DateTime::createFromFormat('m/d/Y', $user_profile["birthday"]);
$birthday = floatval($date->format('U')) * 1000;
$string_time = sprintf('%.0f', $birthday);
}
?>
<input type="hidden" name="birthday" id="birthday" value="<?php echo $birthday; ?>">
<select name="birthday_month" class="month" id="bdayMonth">
<option value="1" <?php if($month == "01"){ echo 'selected'; } ?>>January</option>
<option value="2" <?php if($month == "02"){ echo 'selected'; } ?>>February</option>
<option value="3" <?php if($month == "03"){ echo 'selected'; } ?>>March</option>
<option value="4" <?php if($month == "04"){ echo 'selected'; } ?>>April</option>
<option value="5" <?php if($month == "05"){ echo 'selected'; } ?>>May</option>
<option value="6" <?php if($month == "06"){ echo 'selected'; } ?>>June</option>
<option value="7" <?php if($month == "07"){ echo 'selected'; } ?>>July</option>
<option value="8" <?php if($month == "08"){ echo 'selected'; } ?>>August</option>
<option value="9" <?php if($month == "09"){ echo 'selected'; } ?>>September</option>
<option value="10" <?php if($month == "10"){ echo 'selected'; } ?>>October</option>
<option value="11" <?php if($month == "11"){ echo 'selected'; } ?>>November</option>
<option value="12" <?php if($month == "12"){ echo 'selected'; } ?>>December</option>
</select> </td>
<td class='input1'>
<select name="birthday_day" class="day" id="bdayDay"><?php
for($i = 1; $i <= 31; $i++){
if(intval($day) == $i){
echo '<option value="' . $i. '" selected>' . $i . '</option>';
}else{
echo '<option value="' . $i. '">' . $i . '</option>';
}
}
?></select>
<select name="birthday_year" class="year" id="bdayYear"><?php
for($i = intval(date("Y")); $i > 1950; $i--){
if(intval($year) == $i){
echo '<option value="' . $i. '" selected>' . $i . '</option>';
}else{
echo '<option value="' . $i. '">' . $i . '</option>';
}
}
?></select>
</td>
</tr>
<button class="greybtn" type="submit" id="submitButton">Submit</button>
processRegister.php
<?php
session_start ();
include_once ('ajax-helper/functions.php');
include ('ajax-helper/php-header.php');
$fields = array (
'emailAddress' => $_POST["emailAddress"],
'projectId' => $PROJECT_ID,
'password' => $_POST["regPassword"],
'firstName' => $_POST["fname"],
'lastName' => $_POST["lname"],
'dob' => (float)$_POST["date"],
'gender' => $_POST["gender"]
);
print_r($fields);
?>
try this, concatenate the date, month and year, then convert to timestamp using strtotime().
$date = $_POST['bdayDay']."-" .$_POST['bdayMonth']."-" .$_POST['bdayYear'];
echo strtotime($date);
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>