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>';
Related
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 ?
I'm trying to imitate how the Windows "Map Network Drive" dropdown works. My dropdown options are the letters A-Z. Based on the results from my SQL SELECT statement, I need to show but disable those letters that are already in use. I'm able to get the letters in the dropdown, or get my SQL results in the dropdown but not the correct combination of both.
How do I display all letters of the alphabet in a dropdown and disable those that come back in my SQSL SELECT statement?
Example DB
ID | Desc
-----------
A | Desc A
D | Desc D
F | Desc F
J | Desc J
Z | Desc Z
So for example, using the example DB above, the letters 'A, D, F, J, Z' would be displayed but disabled whereas all other letters in the alphabet would be selectable.
This is the code I currently have. It's very close to being correct but somehow out of order. It does display every letter of the alphabet and my database results (with those results disabled) BUT every letter of the alphabet is displayed for every database result. Again using the Example DB above, the code below would display the letters 'A' through 'Z' for each result (5 times) and each time disable that result only.
<select>
<?php
// SQL select from Example DB table and connection
while ($row = odbc_fetch_array($db)) {
foreach (range('A', 'Z') as $value) {
if ($row['id'] == $value) {
$result = $value.' -- '.$row['desc'];
$dis = "disabled";
} else {
$result = $value;
$dis = "";
}
?>
<option <?php echo $dis; ?>><?php echo $result; ?></option>
<?php
}
}
?>
</select>
I've tried moving the 'while' and 'foreach' around but haven't seemed to get them in the correct order.
You need to first query your database for letters in use, and then create a looping statement that goes from A to Z, and either marks the <option> as disabled or not.
<?php
$inUse = array();
$sql = "SELECT UNIQUE(`ID`) FROM `table` ORDER BY `ID` ASC";
$res = $db->query($sql);
foreach ($drive as $res->fetchObject()) {
$inUse[] = $drive['ID'];
}
// create select list
echo '<select name="drive">';
foreach (range('A', 'Z') as $letter) {
$disabled = (in_array($letter, $inUse)) ? ' disabled="disabled"' : '';
echo '<option value="' . $letter . '"' . $disabled . '>' . $letter . '</option>';
}
echo '</select>';
Untested, but you could try this:
<select>
<?php
$range = range('A', 'Z');
$used = array();
// Fill in used drives along with their descriptions
while ($row = odbc_fetch_array($db)) {
$used[$row['ID']] = $row['desc']);
}
foreach ($range as $value) {
$result = $value;
$dis = "";
if (in_array($value, $used) {
$result .= ' -- '.$used[$value];
$dis = 'disabled="disabled"';
}
?>
<option <?php echo $dis; ?>><?php echo $result; ?></option>
<?php
}
?>
</select>
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;
?>
I have a date of birth, and when the user is over Feb (02), the days should go only to 29. As you can see I'm using $month="1" just to test it. I'm supposed to use PHP only, no JavaScript or anything else. How would i go about making that?
<?php
$month="1"; // <-- currently using this to make it 29,30 or 31 days
print "<select name='day'>";
if ($month==1){
for ($i=0; $i<=28; $i++)
{
$day = 1 + $i;
print "<option value = $day>" . $day ."</option>";
}
}
if ($month==2){
for ($i=0; $i<=29; $i++)
{
$day = 1 + $i;
print "<option value = $day>" . $day ."</option>";
}
}
print "</select>";
print "<select name='month'>";
for ($i=0; $i<=11; $i++)
{
$month = 1 + $i;
print "<option value = $month>" .$month ."</option>";
}
print "</select>";
?>
Once PHP sends stuff to the browser, it is done. It cannot affect the page in any way.
JavaScript, on the other hand, takes over when the browser gets the page. It CAN change the page, and it can even ask the server to do something (in which case PHP may get involved).
In other words, you cannot do what you are asking without JavaScript.
Without Javascript it is just impossible to do.
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.