Pull-down select menu works, but won't pre-select - php

I have a function that take 2 parameters and outputs a select pulldown menu. The first parameter is simply the name, and the second is a parameter ($sel) that holds the selected element (the data is first populated from a db). This works fine, except the if statement (that determine is used to pre-select an option from the database) doesn't seem to work. I've verified there should be a match, and that both $sel and date('g:i ', $i) are both strings..so in principle this should work..but it doesn't.
Can you please take a look and see if there is something I'm doing wrong?
function selectTime_Hour($timeName,$sel){
$start = strtotime('1:00am');
$end = strtotime('12:00pm');
$menu = '<select name="'.$timeName.'">';
for ($i = $start; $i <= $end; $i += 1800)
{
if ($sel==date('g:i ', $i)){
$menu .= '<option selected="selected">' . date('g:i ', $i) . '</option>';
}
else {
$menu .= '<option value="'.date('g:i',$i).'">' . date('g:i ', $i) . '</option>';
}
}
$menu .= '</select>';
return $menu;
}

I'm guessing it's the extra space you're adding in some of those date() calls.
You are populating your <option> tags with values of date('g:i',$i), which I presume you are then storing to the database. However, in your if statement, you are comparing this against date('g:i ',$i), with an extra space after the g:i.

Related

Outputting dates in to a table padding missing dates in order

My array from a database looks like this:
$client[0]['Name'] = 'Foo';
$client[0]['Claim'][0]['year'] = 2013;
$client[0]['Claim'][1]['year'] = 2014;
$client[0]['Claim'][2]['year'] = 2015;
$client[1]['Name'] = 'Bar';
// no 2013!
$client[1]['Claim'][0]['year'] = 2014;
$client[1]['Claim'][1]['year'] = 2015;
// table headers are name, 2013, 2014, 2015...
foreach($client as $c) {
echo '<tr>';
echo '<td>' . $c['Client']['name'] . '</td>';
foreach($c['Claim'] as $claim) :
echo '<td>' . $claim['year'] . '</td>';
endforeach;
echo '</tr>';
}
Which works fine except when a $client is missing a year then the <td> are not balanced; causing my table to be incorrect.
My goal is to have each $claim['Year'] line up with the appropriate table heading.
I figured I could just add empty keys to each array and re-order them but that seems a bit unnecessary and wondered if there was a better way of doing it.
So an array might look
this would be ideal:
$years[null, 2014, 2015]
whereas currently it is
$years[2014,2015]
Hope this makes sense.
I am assuming you have a start and an end year that you want to display the claims for. Then if you replace the inner for loop with this I think it should work:
$x = 0; //Variable to keep track of what claim we are on.
//Loop through the years.
for($i=$start; $i<=$stop; $i++) {
//If there is a claim for this year, echo it and move on to the next claim.
if($c['Claim'][$x]['year'] == $i) {
echo '<td>' . $c['Claim'][$x]['year'] . '</td>';
$x++;
}
//If not, just echo an empty table cell.
else {
echo '<td></td>';
}
}
This only works if the claims are sorted by year. Please note that I have not tested the code.

Multilanguage and select option

I'm working with a multi-language site, and there is an HTML select element with 100 choices. The code looks like this:
<option value="1"><? echo $lang['CARGOTYPE_1']; ?></option>
So system insert into mysql table "type" number "1", or number "2" pr...
But when I select number from mysql, I need to change number to word.
With if, else I can change its meanings:
If($type == 1) { echo $lang['CARGOTYPE_1']; } elseif($type == 2){ echo $lang...
But the problem is, that code will be very long...
Any smart solutions for my problem?
If you know number of choices, use for cycle for that.
for ($i = 1; $i <= 30; $i++) {
echo '<option value="' . $i . '">' . $lang['CARGOTYPE_' . $i] . '</option>';
}
I am not sure what you want... If you want to "echo" something with arguments, you can do something like :
echo $lang['CARGOTYPE_'.$type];
But, is that you need ?

Default Value in PHP For Loop

I have a HTML select form to select the day/month/year, and this is generated with PHP. My for loop, for example with the month, looks like this:
$html="<select name=\"".$name."month\">";
for($i=1;$i<=12;$i++)
{$html.="<option value='$i'>$months[$i]</option>";}
$html.="</select> ";
I can't seem to figure out how to set a "default" value to the current month while retaining all of the prior months in the dropdown (I can just set $i=date("n"), but then I lose the ability to select any prior month).
Does anybody know a simple way of setting the default value to be the current day? Thank you very much!
Try this:
<?php
$currentMonth = date("m");
$html = "<select name=\"" . $name . "month\">";
for ($i = 1; $i <= 12; $i++) {
if ($i == $currentMonth) {
$html .= "<option selected='selected' value='$i'>$months[$i]</option>";
} else {
$html .= "<option value='$i'>$months[$i]</option>";
}
}
$html .= "</select> ";
echo $html;
?>

cakePHP: How to change the ID of a form element (using the form helper)?

I've created a form which has a loop to display a number of rows. I have a select on each row using the form helper. The IDs it creates are all the same, is there a way to add a counter, or some defining info to the ID?
I'm using $this->Form->input('city_id') to output a select of the cities from my city model. All the IDs are ModelCityId. I'd like to get something like ModelCityId1, ModelCityId2, ModelCityId3, etc. Is this possible? Or is there a better way to go about displaying options in a loop?
Thanks for any suggestions you might have.
Here is the relevant part of the code.
while ($current_date != $departure_date) {
$current_date = date("d-M-y", strtotime($current_date . " +1 day"));
$output .= '<tr>';
$output .= '<td>'.$current_date.'</td>';
// irrelevant other columns
$output .= '<td>'.$this->Form->input('city_id', array('label' => '', 'empty' => true)).'</td>';
$output .= '</tr>';
}
As itchy points out, simply use a counter in your while loop to get a unique number.
Then all you have to do is assign it to your ID field
$this->Form->input('city_id', array('id' => 'somvalue' . $i));
Assuming $i is defined in your loop.
If the ids are the same, the name is the same as well. That'll mess up your data when you submit it. You're looking for this syntax:
$this->Form->input("ModelName.$i.city_id", array(...))
Use the ModelName that you're making the form for (i.e. the same as in $this->Form->create('ModelName')).
Edit: why can't you do something like this:
$counter = 0;
while ($current_date != $departure_date) {
$current_date = date("d-M-y", strtotime(date("Y-m-d", strtotime($current_date)) . " +1 day"));
$output .= '<tr>';
$output .= '<td>'.$current_date.'</td>';
// irrelevant other columns
$output .= '<td>'.$this->Form->input('city_id' . $counter, array('label' => '', 'empty' => true)).'</td>';
$output .= '</tr>';
$counter++;
}

why isn't this php loop working?

I have this code:
$thisTime = gmmktime(0, 0, 0);
for($i=0; $i<=95; $i++)
{
$perfTimeNumber = ($i+1);
$perfTimestamp = $thisTime;
$perfTime = date("H:i", $perfTimestamp);
echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';
$thisTime = $thisTime+(15*60);
}
This works fine to generate a select input with options from 01:00 through to 24:45 at 15 minute intervals.
However, if I change the code and add an if statement I get some odd results...
$thisTime = gmmktime(0, 0, 0);
for($i=0; $i<=95; $i++)
{
$perfTimeNumber = ($i+1);
$perfTimestamp = $thisTime;
$perfTime = date("H:i", $perfTimestamp);
if ($perfTime == '19:30') {
$sel = "selected";
}
echo '<option value="'. $perfTimeNumber .'" selected="'.$sel.'">' .$perfTime .'</option>';
$thisTime = $thisTime+(15*60);
}
The idea is to (arbitrarily!) make the select input default to 19.30. The code above adds
selected = "selected" to every option after 19:30, not just the 19:30 option. If I change the if statement slightly to be if ($perfTime = '19:30') { ... i.e., having a single = instead of == it creates a set of options all with the value of 19:30. What am I doing wrong?
Short answer: Because every single echo operation uses the current value of $sel. I assume it's initially blank, so the first N echos contain selected=''. If test succeeds, $sel is set to "selected", and every later print includes selected='selected'. If you use $perfTime = '19:30', it's an assignment, so the test always succeeds, and $sel is always 'selected'.
Quick fix: Add an else clause that sets $sel = ''. However, there are other oddities that make me think this is only a code snippit (i.e. always using $thisTime for $perfTimestamp , rather than something loop indexed, so it always prints the same time?).
This is because you never reset $sel.
Try this instead:
$sel = $perfTime == '19:30' ? 'selected' : '';
$sel isn't explicitly intitialised anywhere, so it's maintaining its 'selected' value for each run through the loop.
Try $sel = ""; as the first line in your loop as a quick fix.
Hm, might be that you should do this:
...
if ($perfTime == '19:30') {
$sel = 'selected="selected"';
}else{
$sel = "";
}
...
I think just having the 'selected' attribute present makes it selected.
Oops, I forgot: And
echo '<option value="'. $perfTimeNumber .'" '.$sel.'>' .$perfTime .'</option>';

Categories