I have an array $faecher to create an SELECT Field with options. My problem is that I want to pre-select an option which is saved in variable f_name1.
<select>
<?php
for ($i = 0; $i <= count($faecher); $i++)
echo "<option" if ($faecher[$i] == f_name1) echo 'selected="selected"'; ">".$faecher[$i]."</option>";
?>
</select>
When you'll learn to indent you code, it will be easier:
echo "<option";
if ($faecher[$i] == f_name1) {
echo ' selected="selected"';
}
echo ">".$faecher[$i]."</option>";
Try:
echo "<option".($faecher[$i] == $f_name1 ? ' selected="selected"' : null).">".$faecher[$i]."</option>";
or
echo "<option".($faecher[$i] == $f_name1 ? ' selected="selected"' : '').">".$faecher[$i]."</option>";
What is f_name1 here? You should use direct string here. Like below
<select>
<?php for ($i = 0; $i <= count($faecher); $i++) { ?>
<option <?php echo $faecher[$i] == "Matching String" ? "selected" : ""; ?>><?php echo $faecher[$i]; ?></option>
<?php } ?>
</select>
Related
I want to put a for loop inside the echo but I can´t, is this possible?
<?php
$limite_may=2014;
$limite_inf=1914;
echo"
<select name='anio_a1' class='select' id='anio_a1'>
<option selected='selected' value='--'>year</option>".
for($i=$limite_may; $i>$limite_inf; $i--){
echo $i;
}.
"</select>";
?>
You want to echo inside a for loop, not put a for loop in the echo. We go though and echo out each item as an <option> with the value $i.
...
echo "<select name='anio_a1' class='select' id='anio_a1'>";
for($i = $limite_may; $i>$limite_inf; $i--) {
echo "<option value=".$i.">".$i."</option>";
}
?>
echo "</select>";
...
<?php
$limite_may=2014;
$limite_inf=1914;
echo "<select name='anio_a1' class='select' id='anio_a1'>";
for($i=$limite_may; $i>$limite_inf; $i--){
echo "<option value='--'>".$i."</option>";
}
echo "</select>";
?>
Remove your HTML from your PHP code blocks entirely, like this:
<?php
$limite_may=2014;
$limite_inf=1914;
?>
<select name='anio_a1' class='select' id='anio_a1'>
<?php
for($i=$limite_may; $i>$limite_inf; $i--){
?>
<option value='<?php echo $i; ?>'><?php echo $i; ?></option>
<?php
}
?>
</select>
If your selected value is stored in a variable named $selected (for example) you can use it like this (HTML5):
<option<?php echo $i==$selected ? ' selected' : ''; ?> value='<?php echo $i; ?>'><?php echo $i; ?></option>
or like this (XHTML):
<option<?php echo $i==$selected ? ' selected="selected"' : ''; ?> value='<?php echo $i; ?>'><?php echo $i; ?></option>
I want to display some number in a dropdown options with the increment 5 which will be started to 5 and ended at 1005. The result should be 5, 10, 15, .... 1000, 1005.
So I wrote the loop as
<?php for ($i=5; $i < 1005; $i++) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
But it gives me as 5,6,7,.... . How can I resolve it?
<?php for ($i=5; $i < 1005; $i+=5) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
$i+=5 <-- This is the only change.
How about this?
I am trying to create a dropdown list for display time. My Option tag should be something like this
<option value="1">01.00AM</option>
<option value="2">02.00AM</option>
<option value="3">03.00AM</option> and so on
So Can I know is there a quick way to create the array instead of typing each hours in option tag?
NOTE: AM and PM should be display according to the time.
I tried it with this code, but it doesn't work for me..
<select>
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?= $i; ?>"><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endfor ?>
</select>
A slight variant on Fresh Prince's deleted answer
I give Fresh Prince of SO most of the credit for this one, but since he deleted his original answer I'm posting a variant of it without the mixed echo tags and weird concatenations.
<select id='time'>
<?php for($i = 1; $i <= 24; $i++): ?>
<option value="<?= $i; ?>"><?= date("h.iA", strtotime("$i:00")); ?></option>
<?php endfor; ?>
</select>
Note: I am using short echo tags <?= because the original post used them. I'd recommend replacing these with <?php echo if you're writing portable code that needs to support older versions of PHP.
Output format
The output format from this script is:
<select id='time'>
<option value="1">01.00AM</option>
<option value="2">02.00AM</option>
...
<option value="23">11.00PM</option>
<option value="24">12.00AM</option>
</select>
This is a live demo.
Why not just use strtotime and date?
date("h.iA", strtotime($i . ":00"))
See a demo
Working it into your example,
<?php
for($i = 0; $i < 24; $i++):
echo "<option value=\"$i\">" . date("h.iA", strtotime($i . ":00")) . "</option>\n";
endfor;
?>
Alternatively,
<?php
for($i = 0; $i < 24; $i++, $d = date("h.iA", strtotime($i . ":00:00"))) {
echo "<option value=\"$i\">$d</option>\n";
}
?>
Lastly,
for($i = 0; $i < 24; print "<option value=\"$i\">" . date("h.iA", strtotime($i . ":00:00")) . "</option>\n", $i++);
See a demo
Checkout the following demo: http://phpfiddle.org/main/code/8y7-hut In-which sprintf formating features are used to fill preleading 0.
The following is the code used:
<select>
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?php echo $i+1; ?>"><?php printf('%1$02d.00',(($i+1) > 12)? ($i+1)-12 : $i+1)?><?php echo (($i < 12)? 'AM' : 'PM'); ?></option>
<?php endfor ?>
</select>
No need for all those messy php tags.
for ($i=0; $i<24; ++$i) {
$t = date("H.iA", strtotime($i.":00:00"));
echo '<option value="'.$i.'">'.$t.'</option>';
}
Try this:
$start = '12:00AM';
$end = '11:59PM';
$interval = '+1 hour';
// $interval = '+30 minutes';
// $interval = '+15 minutes';
$start_str = strtotime($start);
$end_str = strtotime($end);
$now_str = $start_str;
echo '<select>';
while($now_str <= $end_str){
echo '<option value="' . date('h:i A', $now_str) . '">' . date('h:i A', $now_str) . '</option>';
$now_str = strtotime($interval, $now_str);
}
echo '</select>';
Here's a working example.
Well, this is my first question in stackoverflow. I hope that you can help me!
I would like to make a select box or a drop down list, if you prefer
So, i have this php code:
<?php $i = 0; ?>
<?php $orders = osc_list_orders();
foreach($orders as $label => $params) {
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1'; ?>
<?php if(osc_search_order() == $params['sOrder'] && osc_search_order_type() == $orderType) { ?>
<a class="current" href="<?php echo osc_update_search_url($params); ?>"><?php echo $label; ?></a>
<?php } else { ?>
<?php echo $label; ?>
<?php } ?>
<?php if ($i != count($orders)-1) { ?>
<span>|</span>
<?php } ?>
<?php $i++; ?>
<?php } ?>
I have tried to do something like this:
function osc_list_orders_drop() {
$orders = osc_list_orders();
echo '<select name="order" id="order" ONCHANGE="location = this.options[this.selectedIndex].value;">';
foreach($orders as $label => $params){
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1';
echo '<option value="'.osc_update_search_url($params).'">';
echo $label;
echo'</option>';
}
echo'</select>';
}
But the first option is not stored, so when i select one option and then try to select the first one, the first one never works.
Can you help me?
Thx
Try adding a blank option tag in the select, it will allow the change to trigger.
function osc_list_orders_drop() {
$orders = osc_list_orders();
echo '<select name="order" id="order" ONCHANGE="location = this.options[this.selectedIndex].value;">';
echo '<option></option>';
foreach($orders as $label => $params){
$orderType = ($params['iOrderType'] == 'asc') ? '0' : '1';
echo '<option value="'.osc_update_search_url($params).'">';
echo $label;
echo'</option>';
}
echo'</select>';
}
A better option would be to add a selected="selected" to the current filter's option tag.
My client would prefer a currency drop-down list to the currency icon block installed on a 1.5.1 OpenCart theme. I've tried coding it but get the following error:
Parse error: syntax error, unexpected T_STRING, expecting ';' in /...file path......
I've pasted the code and used before and after the offending line.
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name=”curselect” onchange=”$(‘input[name=\'currency_code\']‘).attr(‘value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();”>
***<?php for ($z = 0; $z <= $a – 1; $z++) { ?>***
<?php if ($thisCurCode[$z] == $currency_code) { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>” selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } else { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>”><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
Any help would be most appreciated.
$a – 1 contains something like an m-dash, or whatever it's called. Not a - minus sign. This most likely happened because you copy-pasted code that had been through an auto-formatter like some mail programs or word processors.
after 2 hours of work here is my solution
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" name="formcur" id="formcur">
<div id="currency">
<?php echo $text_currency; ?>
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name="curselect" id="curselect" onchange="$('input[name=\'currency_code\']').attr('value', document.getElementById('curselect').value); document.forms['formcur'].submit();">
<?php
for ($z = 0; $z <= $a - 1; $z++) {
if ($thisCurCode[$z] == $currency_code) { ?>
<option value="<?php echo $thisCurCode[$z]; ?>" selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php
} else {
?>
<option value="<?php echo $thisCurCode[$z]; ?>"><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
</select>
<input type="hidden" name="currency_code" value="" />
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</div>
</form>
I think your problem is that the value returned in your if block is a String?
if ($currency['symbol_left']) {
Had to clean up your code to be able to make any sense of it. You had a load of odd ` chars stick to " and ' and try to keep inside PHP as much as possible.... this is not an answer yet.... I suggest you upload this and identify to us exaclty which line is at fault, I suspect its in the first echo
<?php
$a = 0;
foreach ($currencies as $currency)
{
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left'])
{
$thisCurSymb[$a] = $currency['symbol_left'];
}
else
{
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
echo "<select name='curselect' onchange='$('input[name=\'currency_code\']').attr('value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();'>";
for ($z = 0; $z <= $a – 1; $z++)
{
if ($thisCurCode[$z] == $currency_code)
{
echo "<option value='".$thisCurCode[$z]."' selected>".$thisCurTitle[$z]." ".$thisCurSymb[$z]."</option>";
}
else
{
echo "<option value='".$thisCurCode[$z]."'>".$thisCurTitle[$z]." & nbsp;".$thisCurSymb[$z]."</option>";
}
}
?>
found the problem.....
You have placed a calculation inside the for() statement...
$a =10; // this is to mimic your counter
$a=$a-1; // do you subtract here
//for ($z = 0; $z <= $a – 1; $z++) // this is the issue.
//for ($z = 0; $z <= ($a – 1); $z++) // doesn't work either.
for($z=0; $z <= $a; $z++)
{
echo $z." hello</br />";
}
http://php.net/manual/en/control-structures.for.php
cant see any reason why you wouldnt be able to do it the way you tried, my PHP errors in the same way, even encapsulating the subtract in ()... fixes the issue but still bothers me. Rarely use for()...
I hope that fixes your trouble!