how to show the selcted value selected in CakePHP selectbox - php

following is the code that i have used to show the values in selectbox . but how to show the value selected the below code is not working for me..
$currencyName = Array
(
[$] => US Dollar
[$] => rdcyhgv976
[€] => Euro
[£] => UK Pound
[¥] => Japanese yen
[a] => a
[b] => test
)
$selected=Array
(
[Currency] => €
)
<?php
echo $this->Form->input('SystemUser.Currency', array('type'=>'select','options'=> $currencyName, 'selected'=>$selected['Currency'], 'label' => false, 'div' => false, ));
?>
can anybody tell where I am wrong?

Have you tried
<?php
echo $this->Form->input('SystemUser.Currency', array('type'=>'select','options'=> $currencyName, 'default'=>$selected, 'label' => false, 'div' => false, ));
?>
Change "selected" to "default".

Related

Cast double to int inside Smarty

I am trying to display double variable inside an Smarty environment converting it into int.
The File configuring the variables has the following code.
The Variable PRODUCTS_Quantity_x is the viewed Variable which I added. It is giving me a comma separated value with 3 fractional digits. The {$module_data.PRODUCTS_Quantity} is already be able to get displayed in through the html.
I have already tryed things like count() etc. I mostly got an empty variable or smarty errors ...
$t_data_array = array ( 'PRODUCTS_NAME' => htmlspecialchars_wrapper($array['products_name']),
'COUNT' => $array['ID'],
'PRODUCTS_ID' => $array['products_id'],
'PRODUCTS_VPE' => $this->getVPEtext($array, $products_price['plain']),
'PRODUCTS_IMAGE' => $this->productImage($array['products_image'], $image),
'PRODUCTS_IMAGE_W' => $array['products_image_w'],
'PRODUCTS_IMAGE_H' => $array['products_image_h'],
'PRODUCTS_IMAGE_WIDTH' => PRODUCT_IMAGE_THUMBNAIL_WIDTH,
'PRODUCTS_IMAGE_PADDING' => ((PRODUCT_IMAGE_THUMBNAIL_HEIGHT + 8) - $array['products_image_h'])/2,
'PRODUCTS_IMAGE_ALT' => $array['gm_alt_text'],
'PRODUCTS_LINK' => $gm_product_link,
'PRODUCTS_PRICE' => $products_price['formated'],
'PRODUCTS_TAX_INFO' => $gm_tax_info,
'PRODUCTS_SHIPPING_LINK' => $gm_shipping_link,
'PRODUCTS_BUTTON_BUY_NOW' => $buy_now,
'GM_PRODUCTS_BUTTON_BUY_NOW_URL' => $gm_buy_now_url,
'GM_PRODUCTS_BUTTON_BUY_NOW' => $gm_buy_now,
'PRODUCTS_SHIPPING_NAME' => $shipping_status_name,
'PRODUCTS_SHIPPING_IMAGE' => $shipping_status_image,
'PRODUCTS_DESCRIPTION' => $array['products_description'],
'PRODUCTS_EXPIRES' => $array['expires_date'],
'PRODUCTS_CATEGORY_URL' => $array['cat_url'],
'PRODUCTS_SHORT_DESCRIPTION' => $array['products_short_description'],
'PRODUCTS_FSK18' => $array['products_fsk18'],
'GM_FORM_ACTION' => xtc_draw_form('gm_add_to_cart_'.$array['products_id'], xtc_href_link('index.php', 'action=buy_now&BUYproducts_id=' . $array['products_id'] . $gm_cat_search, 'NONSSL', true, true, true), 'post', 'onsubmit="return gm_quantity_check_listing(\'' . $array['products_id'] . '\')"'),
'FORM_DATA' => $t_form_array,
'QTY_DATA' => $t_qty_array,
'PRODUCTS_Quantity_x' => $array['products_quantity'],
'GM_FORM_END' => '</form>',
'GM_PRODUCTS_QTY' => $gm_qty,
'GM_PRODUCTS_STOCK' => $gm_products_stock,
'PRODUCTS_META_DESCRIPTION' => $array['products_meta_description'],
'PRODUCTS_WEIGHT' => gm_prepare_number((double)$array['products_weight'], $xtPrice->currencies[$xtPrice->actualCurr]['decimal_point']),
'SHOW_PRODUCTS_WEIGHT' => $array['gm_show_weight']);
return $t_data_array;
// EOF GM_MOD
Inside the html the following code is given
<span class="label">{$module_data.PRODUCTS_Quantity_x}<span>
I hope you can help me out here
thanks in advance
You can do this 2 way:
1) First way:
You simple use in Smarty the following code with added string_format:
<span class="label">{$module_data.PRODUCTS_Quantity_x|string_format:"%d"}<span>
2) Second way:
You do whatever you need in PHP and assign desired value in PHP.
So in PHP you can change line:
'PRODUCTS_Quantity_x'=> $array['products_quantity'],
into
'PRODUCTS_Quantity_x'=> (int) $array['products_quantity'],
and then in Smarty you use the same code as you used earlier (without string_format):
<span class="label">{$module_data.PRODUCTS_Quantity_x}<span>

how to disable to select one or many option from dropdown list in cakephp?

I want to disable some options from dropdown list, i have an array like that
array(
'all' => 'ALL',
'skip1' => 'User Define Groups:',
(int) 43 => '--Usii Group2',
(int) 105 => '--Usii Mailing [ mailing list]',
(int) 106 => '--test [ mailing list]',
'skip2' => 'Dynamic Define Groups:'
i want to disable value of skip1 and skip2, if user click on skip1 and skip2 value it can't be select in dropdown list, this is my view file
echo $this->FormManager->input('view',array('label'=>'View ','type'=>'select','options'=>$viewGroup,'default'=>$default));
any one can help to do this, it will be appreciated, thanks in advance.
I think you should disable options from client side, i.e from Jquery something like this
HTML
<select>
<option value="all">ALL/option>
<option value="skip1">User Define Groups:</option>
<option value="43 ">--Usii Group2</option>
<option value="105">--Usii Mailing [ mailing list]</option>
<option value="106">--test [ mailing list]</option>
<option value="skip2">'Dynamic Define Groups:</option>
</select>
JQuery
$('option[value=skip1]').prop('disabled', true);
$('option[value=skip2]').prop('disabled', true);
To complement an answer from Moyed Ansari:
You can use .attr jquery function.
$('option[value=skip1]').attr('disabled', true);
$('option[value=skip2]').attr('disabled', true);
Use an array of arrays.
$values = array(
'all' => 'all',
'skip1' => array(
5 => 'ex',
6 => 'ex',
7 => 'ex',
),
'skip2' => array(
5 => 'ex',
6 => 'ex',
7 => 'ex',
)
)
See here: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
Try with re-arrange the array by following way:
array(
'all' => 'ALL',
'skip1' => array(
'name' => 'User Define Groups:',
'value' => 'skip1',
'disabled' => true
)
(int) 43 => '--Usii Group2',
(int) 105 => '--Usii Mailing [ mailing list]',
(int) 106 => '--test [ mailing list]',
'skip2' => (
'name' => 'Dynamic Define Groups:'
'value' => 'skip2',
'disabled' => true
)
)
Or you can simply try this on your view :
echo $this->FormManager->input('view',array('label'=>'View ','type'=>'select','options'=>$viewGroup,'default'=>$default, 'disabled'=>array('skip1','skip2')));
Both of those do not require any JavaScript or jQuery.

Get value from custom category attribute

I am trying to get the value from a custom category attribute in Magento. The attribute is a select field and is been made with the install script below:
$this->startSetup();
$this->addAttribute('catalog_category', 'category_categorycolor', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'varchar',
'label' => 'Categorie kleur',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'option' => array (
'value' => array('yellow' => array('Geel'),
'purple' => array('Paars'),
'blue' => array('Blauw'),
'red' => array('Rood'),
'orange' => array('Oranje'),
'green' => array('Groen'),
'darkblue' => array('Donkerblauw'),
'lightgreen' => array('Lichtgroen'),
)
),
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Unfortunately only getting numbers and not text value. I use this line to retrieve the value:
<?php $_category_categorycolor = $_category->getData('category_categorycolor'); if($_category_categorycolor): ?> <?php echo $_category_categorycolor; ?> <?php endif; ?>
Can someone help me?
Something like this:
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
The sollution is pretty messy (the only one I know of).
$opt = array(); // will contain all options in a $key => $value manner
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_categorycolor');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach ($options as $o) {
$opt[$o['value']] = $o['label'];
}
}
$categoryColorId = $_category->getData('category_categorycolor');
$categoryColorLabel = $opt[$categoryColorId];
// if you have problems, do a Zend_Debug::dump($opt);
// - it should contain an array of all the options you added
Didn't test it out, let me know if it works or not.
PS: can't reply to your comment, not sure why. What does $opt contain ?
The numbers you are getting back are the id's of each value in the dropdown. You have to load the dropdown values too.
See the following page. It helped me understand this.
http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/

How to make form field a hyperlink in CakePHP?

I have an options array:
$allAmazonMatches = Array ( [1] => B002I0HJZO [2] => B002I0HJzz [3] => B002I0HJccccccccc )
I display them using a Form helper:
<?php
echo $this->Form->create('AmazonMatches', array('action' => 'selectMatches'));
echo $this->Form->input('option_id', array('options' => $allAmazonMatches, 'type' => 'radio'));
echo $this->Form->end(__('Submit', true));
?>
What I want to show is:
option1 http://somewebsite/B002I0HJZO (hyper link that opens in new tab)
option2 http://somewebsite/B002I0HJzz (hyper link that opens in new tab)
How can I style my options array to do that?
You can do it, but I'm not sure it's a recommend practice. Clicking the label should really check the radio box - but if you really have to:
$allAmazonMatches = array(
'B002I0HJZO' => 'http://somewebsite/B002I0HJZO',
'B002I0HJzz' => 'http://somewebsite/B002I0HJzz',
'B002I0HJccccccccc' => 'http://somewebsite/B002I0HJccccccccc'
);
Have you tried something like this:
$label1 = $this->Html->link('blabla1', 'http://www.google.de', array('target' => '_blank'));
$label2 = $this->Html->link('blabla2', 'http://www.google.de', array('target' => '_blank'));
$options = array(0 => $label1, 1 => $label2);
echo $this->Form->input('accept', array('type' => 'radio', 'options' => $options));

How can I create a new array in PHP using elements from a JSON return

Hello all having some trouble accessing elements from a returned JSON from Twitter. I'm trying to retrieve about 18 tweets from 6 different account and display them in chronological order, but I can't figure out how to access the elements in the array. I've tried using json_decode(), tried foreach loop and a for loop. Basically I out of all of the following, all I need is the following elements: text, user->screen_name, user->profile_image_url, and created_at. Reason being when I upload the php to a website, the app I'm building times out and doesn't receive the data, so I'm trying to make the return a lot smaller by only getting back what is needed. Anywho, here is a sample of 1 tweet I'm receiving back...
[{"in_reply_to_status_id":null,"created_at":"Thu Aug 25 01:50:07 +0000 2011","geo":null,"user":{"profile_background_tile":true,"protected":false,"url":"http:\/\/www.backpocketmemory.com","name":"Back Pocket Memory","default_profile":false,"friends_count":1115,"profile_link_color":"0084B4","default_profile_image":false,"show_all_inline_media":false,"contributors_enabled":false,"following":false,"geo_enabled":false,"utc_offset":-28800,"profile_sidebar_border_color":"C0DEED","description":"We're a hard rock\/pop band from Burbank, CA. We love you. facebook.com\/backpocketmemory #bpmchris #bpmguitar #bpmsixstring #bpmbass #bpmdrummer\r\n","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","time_zone":"Pacific Time (US & Canada)","created_at":"Mon Jul 27 20:58:35 +0000 2009","profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","is_translator":false,"profile_use_background_image":true,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","follow_request_sent":false,"statuses_count":2238,"profile_background_color":"C0DEED","screen_name":"bckpcktmmry","profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","favourites_count":6,"id_str":"60708421","lang":"en","verified":false,"profile_text_color":"333333","location":"Burbank, CA","id":60708421,"listed_count":19,"notifications":false,"profile_sidebar_fill_color":"DDEEF6","followers_count":683},"in_reply_to_user_id":null,"truncated":false,"coordinates":null,"favorited":false,"possibly_sensitive":false,"in_reply_to_status_id_str":null,"in_reply_to_screen_name":null,"source":"\u003Ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003EFacebook\u003C\/a\u003E","in_reply_to_user_id_str":null,"id_str":"106543849266098176","id":106543849266098176,"contributors":null,"place":null,"retweeted":false,"retweet_count":2,"text":"Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http:\/\/t.co\/oNvw5I6"}]
Any help is appreciated!
Your JSON decodes fine:
array (
0 =>
array (
'in_reply_to_status_id' => NULL,
'created_at' => 'Thu Aug 25 01:50:07 +0000 2011',
'geo' => NULL,
'user' =>
array (
'profile_background_tile' => true,
'protected' => false,
'url' => 'http://www.backpocketmemory.com',
'name' => 'Back Pocket Memory',
'default_profile' => false,
'friends_count' => 1115,
'profile_link_color' => '0084B4',
'default_profile_image' => false,
'show_all_inline_media' => false,
'contributors_enabled' => false,
'following' => false,
'geo_enabled' => false,
'utc_offset' => -28800,
'profile_sidebar_border_color' => 'C0DEED',
'description' => 'We're a hard rock/pop band from Burbank, CA. We love you. facebook.com/backpocketmemory #bpmchris #bpmguitar #bpmsixstring #bpmbass #bpmdrummer
',
'profile_background_image_url_https' => 'https://si0.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg',
'time_zone' => 'Pacific Time (US & Canada)',
'created_at' => 'Mon Jul 27 20:58:35 +0000 2009',
'profile_image_url' => 'http://a2.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg',
'is_translator' => false,
'profile_use_background_image' => true,
'profile_image_url_https' => 'https://si0.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg',
'follow_request_sent' => false,
'statuses_count' => 2238,
'profile_background_color' => 'C0DEED',
'screen_name' => 'bckpcktmmry',
'profile_background_image_url' => 'http://a3.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg',
'favourites_count' => 6,
'id_str' => '60708421',
'lang' => 'en',
'verified' => false,
'profile_text_color' => '333333',
'location' => 'Burbank, CA',
'id' => 60708421,
'listed_count' => 19,
'notifications' => false,
'profile_sidebar_fill_color' => 'DDEEF6',
'followers_count' => 683,
),
'in_reply_to_user_id' => NULL,
'truncated' => false,
'coordinates' => NULL,
'favorited' => false,
'possibly_sensitive' => false,
'in_reply_to_status_id_str' => NULL,
'in_reply_to_screen_name' => NULL,
'source' => 'Facebook',
'in_reply_to_user_id_str' => NULL,
'id_str' => '106543849266098176',
'id' => 106543849266098176,
'contributors' => NULL,
'place' => NULL,
'retweeted' => false,
'retweet_count' => 2,
'text' => 'Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http://t.co/oNvw5I6',
),
)
Echo all the user's avatars, names, and a date when they joined:
$data = json_decode($twitter_response);
foreach($data as $d)
{
echo '<img src="' . $d->user->profile_image_url . '" /> ' . $d->user->screen_name . ' (since ' . $d->user->created_at . ')<br />';
}
If you are using an associative array as your json_decode output, make sure you reference the appropriate keys, like so:
foreach($data as $d)
{
echo '<img src="' . $d['user']['profile_image_url'] . '" /> ' . $d['user']['screen_name'] . ' (since ' . $d['user']['created_at'] . ')<br />';
}
Working code: http://pastie.org/2436307 (removed single quotes in some values - just escape them or convert them when you decode/reference)
Use http://json.parser.online.fr/ to verify that the JSON you are trying to json_decode is valid. That's the main function you need to use and it will return an array that you can use normally. Most likely your JSON is invalid and you'll need to get that cleaned up first.
There's no closing bracket, and thus it's not valid JSON data. Put ] at the end of the string and it parses just fine with json_decode();

Categories