Selected in select option (PHP) - php

I've some pb with a piece of code in PHP Laravel.
<select id="month-select">
<option value="0">select month</option>
#for ($i =0; $i <=12; $i++)
<option value="{{today()->subMonth($i)->format("Y-m")}}">{{today()->subMonth($i)-format("Y-m")}}</option>
#endfor
</select>
I'd like to add a selected and verify if my selected date is the same as the date in the parameter ($date).
I thought of something like this:
<option value="{{today()->subMonth($i)->format("Y-m")}}"{{(today()->subMonth($i)->format("Y-m")) && (!is_null(today()->subMonth($i)->format("Y-m"))) == $date ? 'selected':''}}>{{today()->subMonth($i)->format("Y-m")}}</option>
It is not optimised yet, and it doesn't work. Only the last value is selected. What's wrong?

<select id="month-select">
<option value="0">select month</option>
#for ($i =0; $i <= 12; $i++)
<option value="{{today()->subMonth($i)->format('Y-m')}}"
{{today()->subMonth($i)->eq($date) ? 'selected': ''}}>
{{today()->subMonth($i)->format("Y-m")}}
</option>
#endfor
</select>

Related

set default selected db value in select element php laravel

<td>
<select name="setR" class="form-control">
<option value="">--Select--</option>
#if($ap_detail['pr'])
<option selected="selected" value="{{$ap_detail['pr']->setR}}">
#endif
#for($i=1; $i<=31; $i++)
{{$ap_detail['pr']->setR}} Days Later</option>
<option value="{{$i}}">{{$i}} Days Later</option>
#endfor
</select>
</td>
if value exist in databse it select by default that value
I tried above code its working fine but it select the db selected value but also show in looping list I mean if 4 comes from database it shows two 4 values it should be one 4 value
I think this might help you. So in loop if your database value occurs then add selected="selected" to that option instead of printing that option differently.
<td>
<select name="setR" class="form-control">
<option value="">--Select--</option>
#for($i=1; $i<=31; $i++)
#if($ap_detail['pr'] && $ap_detail['pr']->setR == $i)
<option selected="selected" value="{{$ap_detail['pr']->setR}}">{{$ap_detail['pr']->setR}} Days Later</option>
#else
<option value="{{$i}}">{{$i}} Days Later</option>
#endif
#endfor
</select>
</td>

From and To date search in database PHP

Good day! I am trying to create a "search by date" option in my website that will retrieve the records in the database whose dates matched the users' input. I have these select boxes:
<!-- FROM -->
<select name="from_month" id="from_month">
<option selected>-- MONTH --</option>
<option value="january">January</option>
...
<option value="december">December</option>
</select>
<select name="from_day" id="from_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="from_year" id="from_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- FROM (END) -->
<!-- TO -->
<select name="to_month" id="to_month">
<option selected>-- MONTH --</option>
<option value="1">January</option>
...
<option value="12">December</option>
</select>
<select name="to_day" id="to_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="to_year" id="to_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- TO (END) -->
What I originally plan to do is get all the "from_" values and store it into a variable '$from'. The same goes for the "to_". Because my database for dates looks like this:
id | title(text) | date_submitted(date) |
1 | Lorem Ipsum | 2015-01-20 |
Any help would be appreciated on how I would accomplish this. Thank you in advance!
Since you know every part of the date, MySQL's BETWEEN would do the trick:
SELECT * FROM db WHERE mydate BETWEEN 'from_date' AND 'to_date'
from_date and to_date are your variables, get through the form.
As #AnoopS stated, use numbers to value your month boxes.
And remember to prevent injections using mysqli or PDO with prepared statements.

How to calculate unknown array values in php

I have this form but would like to get the total.
Please how can I calculate this array?
...While(ticket table row)
{
<input type="text" name="ticket[][price]" id="ticket[][price]" value="12">
<input type="text" name="ticket[][fee]" id="ticket[][fee]" value="3462">
<select name="ticket[][qty]">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
</select>
}
Eg. $total = ticket[][price] + ticket[][fee] * ticket[][qty];
Maybe something like this?
$total = 0;
foreach ($_POST['ticket'] as $ticket) {
$total += $ticket['price'] + $ticket['fee'] * $ticket['qty'];
}
print $total;

Get date, explode, use data to select options in <select>

I have three variables set, $date_legible (in the format of F jS, Y, i.e. January 1st, 2011), $date_timestamp (self-explanatory), and $date_normal (in the format of YYYY-MM-DD)
I have three select fields:
<select name="date_mo">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="date_dy">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="date_yr">
<?php for($year=$curryear;$year<$curryear+51;$year++){ ?>
<option value="<?php echo $year; ?>"><?php echo $year; ?></option>
<?php } ?>
</select>
I know I want to convert the timestamp, but I will ultimately need to explode the output for the $month, $day, and $year. The problem is I don't know how to check the selects to the values and automatically select the correct option.
Can anyone help? Thanks!
UPDATE
I now have one more variable, because I'm having users submit from the front end, and Gravity Forms submits as YYYY-MM-DD.
Prior to diving into the solutions already posted (thank you guys [and gals] by the way), here's a little more information:
I'm running WordPress and have three custom fields as described above. When a user submits a post from the front end, the only custom field that's filled out is $date_normal. This is okay, because a post has to be moderated (i.e. published) from the backend. What I NOW need is on load to set the YYYY-MM-DD to set the options on the select. I'm not asking you guys to alter your answers, I'm sure I can figure it out (though if you can help with this new problem, it would be greatly appreciated).
If I understand your question correctly, you have a timestamp and you need to use the timestamp to select the appropriate select options when you render the page.
As a forewarning - this will make it possible to select invalid dates, for example February 31st (does not exist). You may want to consider re-drawing the year drop-down when the month is changed using javascript. Since you didn't specify any javascript library (and it is not clear how important the possibility of an invalid date is), I will not venture to show code for that, but it would not be terribly difficult.
// using the current time for the sake of the example, your timestamp would take the place of this
$timestamp = time();
// determine the selected month, day, and year
$selected_month = date('n', $timestamp);
$selected_day = date('j', $timestamp);
$selected_year = date('Y', $timestamp);
// now, create the drop-down for months
$month_html = '<select name="date_mo">';
for ($x = 1; $x < 13; $x++) {
$month_html .= '<option value='.$x.($selected_month == $x ? ' selected=true' : '' ).'>'.date("F", mktime(0, 0, 0, $x, 1, $selected_year)).'</option>';
}
$month_html .= '</select>';
// output
print $month_html;
// create the day drop-down
$day_html = '<select name="date_day">';
for ($x = 1; $x < 32; $x++) {
$day_html .= '<option value='.$x.($selected_day == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$day_html .= '</select>';
// output
print $day_html;
// create the year drop-down
$year_html = '<select name="date_year">';
$start_year = date('Y', time());
$max_year = $start_year + 51;
for ($x = $start_year; $x < $max_year; $x++) {
$year_html .= '<option value='.$x.($selected_year == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$year_html .= '</select>';
// output
print $year_html;
Output:
EDIT
If your source data will be in the format of YYYY-MM-DD. In order to translate that into a timestamp, and thus work with the example code I've posted, is to call strtotime on that formatted date value. Thus, the only line in the code above that would need to be changed is $timestamp = strtotime($the_formatted_date);
<?php
// $months = array(1 => 'January', 2 => 'February', ... etc
?>
<select name="date_mo">
<? for ($i = 1, $current = (int) date('n'); $i <= 12; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$months[$i];?></option>
<? } ?>
</select>
<select name="date_dy">
<? for ($i = 1, $current = (int) date('j'); $i <= 31; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
<select name="date_yr">
<? for ($i = $current = (int) date('Y'), $until = $i + 50; $i <= $until; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
</select>
I would personally convert the 3 fields to a more user-friendly date selector (using for example jQuery UI), but to have the correct option selected when you open the page, you need to add a check to each option.
Example for the year:
<?php
$selected_year = date("Y", $date_timestamp); // get the selected year
for($year=$curryear;$year<$curryear+51;$year++)
{
?>
<option value="<?php echo $year; ?>" <?php echo ($selected_year == $year) ? 'selected' : ''; ?>><?php echo $year; ?></option>
<?php
}
?>

Elegant way to add "selected" into a dropdrown form using PHP

I have a form that looks like this:
$guests = 2; // Just for testing
$form = 'Guests<br /><select name="guests">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>';
But the list of guests can be up to 100, so I probably shouldn't do a check for which value is selected manually.
What would be a good way for me to make the selected value become the selected value when the user sees this form?
Thanks!
What I normally do with dynamic select menus is use a foreach loop over an array, and just check each item.
Something like:
$dropdownName = "guests"; //Name of dropdown
$defaultValue = "0"; //value to select if there isn't one already set
$items = array('name'=>'value', 'anotherName'=>'anothervalue'); //items, name=>value
echo '<select name="'.$dropdownName.'">'; //Start Select
$selectedItem = (isset($_POST[$dropdownName])?$_POST[$dropdownName]:$defaultValue); //If a value is set, use it, otherwise use the default
foreach ($items as $name=>$value) //build select
{
echo '<option value="'.$value.'"'.(($selectedItem == $value)?' selected="selected"':'').'>'.$name.'</option>';
}
echo '</select>';
$guests = 2; // Just for testing
$form = 'Guests<br /><select name="guests">';
for($i = 0; $i <= 100; $i++)
$form .= "<option value='$i'{$guest == $i ? ' selected=\'selected\'' : ''}>$i</option>";
$form .= '</select>';
EDIT: $i < 101 was pretty lame...
You can store the value this value in a hidden field and on document.ready(), you can assign the selected value of drop down top the value of hidden value.
A solution that works but may not be efficient for some users...

Categories