How to make form field a hyperlink in CakePHP? - php

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));

Related

how to show the selcted value selected in CakePHP selectbox

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".

Displaying one string from a list of multiple strings

I'm trying to grab data from this array, but when I do var_dump($ArrayedLevels['Attack']); it returns NULL, I know for a fact that it was able to grab the data from the SQL database, I think it has something to do with my array. Any help would be greatly appreciated.
include("highscoresconfig.php");
$GrabXP = $database2->prepare("SELECT * FROM `skills` WHERE `playerName` = ?");
$GrabXP->execute(array($playerName));
$MainResult = $GrabXP->fetchAll();
$ArrayedLevels = $array = [
"Attack" => $MainResult['Attacklvl'],
"Defence" => $MainResult['Defencelvl'],
"Strength" => $MainResult['Strengthlvl'],
"Hitpoints" => $MainResult['Hitpointslvl'],
"Ranged" => $MainResult['Rangelvl'],
"Prayer" => $MainResult['Prayerlvl'],
"Magic" => $MainResult['Magiclvl'],
"Cooking" => $MainResult['Cookinglvl'],
"Woodcutting" => $MainResult['Woodcuttinglvl'],
"Fletching" => $MainResult['Fletchinglvl'],
"Fishing" => $MainResult['Fishinglvl'],
"Firemaking" => $MainResult['Firemakinglvl'],
"Crafting" => $MainResult['Craftinglvl'],
"Smithing" => $MainResult['Smithinglvl'],
"Mining" => $MainResult['Mininglvl'],
"Herblore" => $MainResult['Herblorelvl'],
"Agility" => $MainResult['Agilitylvl'],
"Thieving" => $MainResult['Thievinglvl'],
"Slayer" => $MainResult['Slayerlvl'],
"Farming" => $MainResult['Farminglvl'],
"Runecrafting" => $MainResult['Runecraftlvl'],
"Hunter" => $MainResult['Hunterlvl'],
"Construction" => $MainResult['Constructionlvl'],
"Summoning" => $MainResult['Summoninglvl'],
"Dungeoneering" => $MainResult['Dungeoneeringlvl'],
];
var_dump($ArrayedLevels["Attack"]);
Problem might be because the fetchAll() returns all the rows of your query.
if($GrabXP->execute(array($playerName))){
//Success
$MainResult = $GrabXP->fetchAll();
/*
This will give you all the rows. Use a foreach loop to iterate through all the rows.
If you want only the first row, add this-
*/
$MainResult = $MainResult[0];
//The rest of your code.
$ArrayedLevels = $array = Array(
"Attack" => $MainResult['Attacklvl'],
...
);
var_dump($ArrayedLevels["Attack"]);
}
else{
//Failure
}
The brackets don't work in PHP when creating a new array, use array() to create one.
Actually: as of PHP 5.4 they do work, see here: http://nl3.php.net/manual/en/language.types.array.php
Try var_dump($MainResult);
if all good, try create your array like this:
$ArrayedLevels = array(
"Attack" => $MainResult['Attacklvl'],
"Defence" => $MainResult['Defencelvl'],
"Strength" => $MainResult['Strengthlvl'],
"Hitpoints" => $MainResult['Hitpointslvl'],
"Ranged" => $MainResult['Rangelvl'],
"Prayer" => $MainResult['Prayerlvl'],
"Magic" => $MainResult['Magiclvl'],
"Cooking" => $MainResult['Cookinglvl'],
"Woodcutting" => $MainResult['Woodcuttinglvl'],
"Fletching" => $MainResult['Fletchinglvl'],
"Fishing" => $MainResult['Fishinglvl'],
"Firemaking" => $MainResult['Firemakinglvl'],
"Crafting" => $MainResult['Craftinglvl'],
"Smithing" => $MainResult['Smithinglvl'],
"Mining" => $MainResult['Mininglvl'],
"Herblore" => $MainResult['Herblorelvl'],
"Agility" => $MainResult['Agilitylvl'],
"Thieving" => $MainResult['Thievinglvl'],
"Slayer" => $MainResult['Slayerlvl'],
"Farming" => $MainResult['Farminglvl'],
"Runecrafting" => $MainResult['Runecraftlvl'],
"Hunter" => $MainResult['Hunterlvl'],
"Construction" => $MainResult['Constructionlvl'],
"Summoning" => $MainResult['Summoninglvl'],
"Dungeoneering" => $MainResult['Dungeoneeringlvl'],
);
first of all try to print the structure of the variable $MainResult:
var_dump($MainResult);
FetchAll return an array with all restuls like
resutl = [
[0] => ['first row'],
[1] => ['sec. row']
]
you will See The variable $MainResult looks like:
MailResult => [
[0] => ['Attacklvl' => 'foo'],
[1] => ['Defencelvl' => 'bar']
]

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/

CakePHP saveAll() function problems

Im trying to save a record to a table aswell as an accociated record. The main table is called quotes and it has a hasmany link to the table quote_items. quote_items belongsto quote
When i try and save it saves the record in quote but does not save the record in quote_items
.
Below is my quote add function
function add() {
if (!empty($this->data)) {
$this->Quote->create();
if ($this->Quote->saveAll($this->data)) {
$this->Session->setFlash(__('The Quote has been saved', true));
//$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Quote could not be saved. Please, try again.', true));
}
}
$this->Quote->recursive = 2;
$statuses = $this->Quote->Status->find('list');
$contacts = $this->Quote->Contact->find('list');
$this->set(compact('statuses', 'contacts'));
}
Quote view / form setup
<?php echo $form->create('Quote', array('action' => 'add'));?>
<fieldset>
<legend><?php __('Add Quote');?></legend>
<?php
echo $form->input('Quote.name');
echo $form->input('Quote.revision');
echo $form->input('Quote.status_id');
echo $form->input('Quote.contact_id');
echo $form->input('quote_item.product_id');
echo $form->input('quote_item.name');
echo $form->input('quote_item.price');
echo $form->input('quote_item.description');
echo $form->input('Quote.totalcost');
?>
</fieldset>
<?php echo $form->end('Submit');?>
Array that is returned when the form is submitted
Array (
[quote] => Array (
[name] => Test
[revision] => 1
[status_id] => 1
[contact_id] => 1
[totalcost] => 123
)
[quote_item] => Array (
[product_id] => 1
[name] => test
[price] => 123
[description] => tes 1234
)
)
This seems to follow exactly what is listed in the cakephp documentation so i cant work out why its not working - http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo
Thanks in advance
The correct way to build the form would be:
echo $form->input('Quote.name');
...
echo $form->input('QuoteItem.0.product_id');
echo $form->input('QuoteItem.0.name');
...
echo $form->input('QuoteItem.1.product_id');
echo $form->input('QuoteItem.1.name');
The resulting array should look like this:
array(
'Quote' => array(
'name' => 'Test'
....
),
'QuoteItem' => array(
0 => array(
'product_id' => 1
'name' => 'test'
...
)
1 => array(
'product_id' => 2
'name' => 'test'
...
)
)
)
According to naming conventions, model names are camelized (ModelName, not model_name). Also, since Quote hasMany QuoteItems, the QuoteItem array needs to consist of many QuoteItem arrays. Hope that makes sense. :)

Categories