Change currency icon block to drop-down list - php

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!

Related

foreach loop doesn't populate select options

I've been struggling with this for at least an hour to no avail. I'm sure the answer is simple, but I can't figure it out. The foreach loop is what I'm trying to accomplish, but it doesn't work. The for loop works and renders fine. Please help! - Jon
<div class="td"><select name="vehYear">
<?php
foreach ($veh_year_array as $item)
{?>
<option value="<?php echo $item;?>"><?php echo $item;?></option>
<?php}
?>
<?php
for ($i = date('Y'); $i > 1950; $i--) {
echo "<option>$i</option>";
}
?>
</select></div>
I tested your code and it seems your error is the curly bracket }. In my environment I received an
invalid argument error
when pasting your original code. You want to be very careful in PHP since it is a sensitive language.
So exchange this
<?php}?>
to this
<?php }?>
Your code should then execute
<?php
$veh_year_array =array("Tomato", "Apple", "Orange");
foreach ($veh_year_array as $item)
{?>
<option value="<?php echo $item;?>"><?php echo $item;?></option>
<?php }?>
<?php
for ($i = date('Y'); $i > 1950; $i--) {
echo "<option>$i</option>";
}
?>

How to merge two PHP for-loop

How do I merge two (for) in one code?
e.g first code
for ($i = 0, $n = count($options); $i < $n; $i ++) {}
and second code
for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++)
here is oll code
<?php if ($display_poll) { ?>
<form action="<?php echo JRoute::_('index.php');?>" method="post" name="poll_vote_<?php echo $poll->id;?>" id="poll_vote_<?php echo $poll->id;?>">
<?php for ($i = 0, $n = count($options); $i < $n; $i ++) { ?>
<label for="mod_voteid<?php echo $options[$i]->id;?>" class="<?php echo $tabclass_arr[$tabcnt].$params->get('moduleclass_sfx'); ?>" style="display:block; padding:2px;">
<input type="radio" name="voteid" id="mod_voteid<?php echo $options[$i]->id;?>" value="<?php echo $options[$i]->id;?>" alt="<?php echo $options[$i]->id;?>" <?php echo $disabled; ?> />
<?php echo $options[$i]->text; ?> <?php for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++) { ?>
<a href="<?php echo $params->get($u); ?>" onClick="return popup(this, 'notes')">
<img src="/images/stories/add.png" alt="play"></a>
<?php
} ?
I want in one poll show onle play button
now shows this
here is my xml code:
You don't really need that second for. You already have an index with $i from your first loop. (Except that it starts from 0 instead of 1, but you can easily get it working)
Remove the second for, and try this :
// echo $params->get($u);
echo $params->get('rel_article' . ($i + 1));

increament by 5 for loop in php

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?

html select box - load image for default selected option

Today I faced with a problem about html select
here is the code :
<select id="optdon" onchange="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { ?>
<option value="<?php echo $donname['don_id']; ?>"><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
function showimage(ids){
clearall();
if(ids.value != ''){
jQuery('#'+ids.value).show();
}
}
What is the project ? I need to select first option !!!
It is easy but when i set the first option selected it's image doesn't load in the page first load . option's image appears on changing options.
I have these two answers for selecting first option (when thre is just one option )
suggestion 1
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['don_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<select id="optdon" onload="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php $z = 0; $selected='selected' ?>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { $z = $z + 1; ?>
<option <?php if($z==1 && $x == 1)echo $selected; ?> value="<?php echo $donname['don_id']; ?>" ><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
</div>
suggestion 2 :
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['donation_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<?php if($x == 1) { ?>
??????
<?php } else { ?>
thank you
Add this JavaScript:
var optdon = document.getElementById('optdon');
optdon.onchange.call(optdon);
This will trigger the onChange event handler, allowing the initial option to have its effect.

Drop Down list in php (help)

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.

Categories