How to get timezone value - magento

how to get the timezone drop down menu in frontend magento?

Here is the code to get all timezones:-
$timezones = Mage::getModel('core/locale')->getOptionTimezones();
echo "<pre>";
print_r($timezones);
echo "</pre>";
You will be getting an array. You can loop through the array and populate the selection/dropdown list like below:-
<select>
<?php
foreach($timezones as $timezone) {
?>
<option value="<?php echo $timezone['value']; ?>"><?php echo $timezone['label']; ?></option>
<?php
}
?>
</select>
Thanks.

Related

select shows the top record in the db and not the selected

I have a select on a form that updates the backend db ok. My problem is that when the form is displayed, it always shows the top record in the db and not the selected.
I've googled and read answers on stackoverflow among others but everything I try either doesn't work or breaks functionality.
$registrants points to a select on a registrants table.
$affiliates points to a select on an affiliates table
Here is the select:
<select name="affiliate_id" class="form-control">
<?php foreach($affiliates as $display => $value) { ?>
<option value='<?= $value['affiliate_id'] ?>' <?php if($registrants['affiliate_id'] == $value['affiliate_id']) { ?>selected='selected'<?php } ?>><?= $value['affiliate_name'] ?>
</option>
<?php } ?>
</select>
Thanks for looking it over.
You could clean up your code a bit and create a variable $selected, which either is empty or a string 'selected':
<select name="affiliate_id" class="form-control">
<?php
foreach($affiliates as $value):
$selected=$registrants['affiliate_id'] == $value['affiliate_id']?'selected':'';
?>
<option value="<?=$value['affiliate_id']?>" <?=$selected?> >
<?= $value['affiliate_name'] ?>
</option>
<?php
endforeach;
?>
</select>
or you could use the CI function form_dropdown(), for that don't forget to load the form helper. This method simplifies a bit the use of drop down lists:
$this->load->helper('form');
$data=array('class'=>'$affiliates');
// get the selected item
<?php
foreach($affiliates as $value){
$selected=$registrants['affiliate_id'] == $value['affiliate_id']?$value['affiliate_id']:'';
if($selected){break;}
}
?>
<?=form_dropdown('affiliate_id', $affiliates, $selected, $data);?>

making a select dropdown from database in codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In your controller you are passing value like
$data['red']=$this->mymodel->get_sec_info();
and in you view you are taking value like
<?php foreach($rec as $row) {?>
So change the vairiable $rec to $red in your view like
<?php foreach($red as $row) {?>
Your option tag is badly formed. The opening option tag is missing its closing bracket, and the closing quotation mark for the option tag's value is in the wrong place.
Corrected:
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>
You should use CI form helper function instead of using html code
In model
function get_sec_info()
{
$records=$this->db->query('SELECT sec_id,name FROM section');
$ret_arr = array();
if($records->num_rows() > 0)
{
while($records->result() as $r)
$ret_arr[$r->sec_id] = $r->name;
}
return $records->result();
}
In your view
echo form_dropdown("section",$rec);
Remove your code below in view:
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
Make sure you have loaded form helper.

populate dropdown in codeigniter form using set_value

i have a form that uses a drop down and im using codeigniter , form helper and form validation
so when i get a validation error in my form , all the correctly entered fields are populated using set_value of codeigniter , however this doesnt work for dropdown
im doing :
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
when get error in form , always the first value of dropdown is selected and not previously set
Any ideas , what am i doing wrong?
The correct method to use set_select() is
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>
So Andras Ratz method/answer won't work.
Your method is correct.
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
However remember that set_value() is only going to return the value if you have setup a validation rule for it, in your case for mid. You could try as below -
<?php echo form_dropdown('mid', $id_map, (isset($_POST['mid']) ? $_POST['mid'] : ''), 'id="mid"') ?>
or just set a validation rule for mid too
It's because you have to use the set_select() function , not the set_value();
You can check that here:
http://codeigniter.com/user_guide/helpers/form_helper.html
And the right syntax:
EDIT :
<?php echo form_dropdown('mid', $id_map, set_select('mid'), 'id="mid"'); ?>
If you have a default value, that should go in the 2. parameter for set_select.
I hope this will work.
<?php // Change the values in this array to populate your dropdown as required
$options = array(
'active' => 'Active',
'inactive' => 'Inactive',
);
echo form_dropdown('mid', $options,$id_map, ' id="mid"');
?>
What about if the the form select content is selected from a foreach loop like this
<?php foreach ($roles as $role) {
echo "<option value=" . $role->id . ">" . $role->role_name . "</option>";
}
?>
If you do not have a validation rule on the actual dropdown field, then
echo form_dropdown('mid',$id_map);
is sufficient.

How to Set the Credit Card payment method as default (selected) when there is more than one payment method in Magento

Is there a way to make the Credit Card payment method always selected and open initially when there are more than one payment method? This is in the Checkout process.
Here's what I ended up doing.
Copy the \website\app\design\frontend\base\default\template\checkout\onepage\payment\methods.phtml file to the equivalent directory in my theme.
Add a new loop counter variable, $i, at the top after the first comment:
$i = 1;
Inside the <?php if( sizeof($this->getMethods()) > 1 ): ?> first if conditional check, add the following condition (basically, it checks if this the first payment method and sets it to checked)
<?php if( $i == 1 ): ?>
<input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]" checked="checked" title="<?php echo $this->htmlEscape($_method->getTitle()) ?>" onclick="payment.switchMethod('<?php echo $_code ?>')" class="radio" />
<?php else: ?>
<input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]" title="<?php echo $this->htmlEscape($_method->getTitle()) ?>" onclick="payment.switchMethod('<?php echo $_code ?>')"<?php if($this->getSelectedMethodCode()==$_code): ?> checked="checked"<?php endif; ?> class="radio" />
<?php endif; ?>
The else condition is the same as before.
Just before the <?php endforeach; ?> I added this line (with <?php around it):
$i = $i + 1;
EDIT:
Actually, the only reliable cross-browser way to keep it open was to copy the file here: \app\design\frontend\base\default\template\payment\form\ccsave.phtml to my theme and then remove the style="display: none;" from the first .
That fixed it so the form was always open and the above code made sure it was selected by default.
you can try this on earlier steps with observer
try {
$quote->getPayment()->setMethod('method_code')->getMethodInstance();
} catch ( Exception $e ) {
Mage::logException($e);
}
but be aware that this payment method has to be active and ready to use before you can set this also and you must check if there is no payment method selected before so you won't change the user selection or force the same payment method for each order and you may have to change some frontend code to open up payment method form.

Magento - How do I get a list of all allowed countries in optionsarray?

I can get countries like this:
$countryCollection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
And they are listed somewhere in the object, but, how do I toOptionsArray them out?
I'm after only the options per website rather than complete listi.e. I want Angola, Antarctica and so on out of the list. (Sorry Angolans and penguins.)
Its actually the obvious answer:
$countryList = Mage::getModel('directory/country')->getResourceCollection()
->loadByStore()
->toOptionArray(true);
also check out http://fishpig.co.uk/magento-tutorials/list-countries-for-drop-down-in-magento for more info on creating drop-down lists and such with the country list.
<?php
$_countries = Mage::getResourceModel('directory/country_collection')
->loadData()
->toOptionArray(false);
$allowed = Mage::getStoreConfig('general/country/allow');
if (count($_countries) > 0) { ?>
<div class="input-box">
<select name="country" id="country" class="validate-select" title="Country" >
<option value="">-- Please Select --</option>
<?php foreach($_countries as $_country){
if(!in_array($_country['value'],explode(',',$allowed))){
continue;
} ?>
<option value="<?php echo $_country['value']; ?>" <?php echo $formData['country'] == $_country['value'] ? ' selected="selected"' : '';?>>
<?php echo $_country['label'] ?>
</option>
<?php
} ?>
</select>
<?php } ?>

Resources