Cannot autocomplete other textfields using CJuiAutoComplete - php

I have a model that has fields id_peg, nama_peg, and jabatan_peg. So here's what I wanna do:
Autosuggestion that shows the list ofid_peg as I type in the text field id_peg
When I select the suggested id_peg, the fields nama_peg and jabatan_peg are autocompleted based on that corresponding selected value (retrieved from database)
I've been trying to do the first one and I did it. But I'm stuck at the second one.
This is what I did:
view/melaporkan.php:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'test1',
'source' => $this->createUrl('search'),
// additional javascript options for the autocomplete plugin
'options' => array(
'showAnim' => 'fold',
'select' => 'js:function(event, ui) {
$('#nama_peg').val(ui.item.nama_peg);
$('#jabatan_peg').val(ui.item.jabatan_peg);
}',
),
));
controllers/sitecontroller.php
public function actionSearch() {
$res = array(0 => array('id' => 1, 'value' => "id_peg"), 1 => array('id' => 2, 'value' => "jabatan_peg"), 2 => array('id' => 2, 'value' => "nama_peg"));
if (isset($_GET['term'])) {
$qtxt = "SELECT id_peg FROM pegawai WHERE id_peg LIKE :id_peg";
$command = Yii::app()->db->createCommand($qtxt);
$command->bindValue(":id_peg", '%' . $_GET['term'] . '%', PDO::PARAM_STR);
$res = $command->queryColumn();
}
echo CJSON::encode($res);
Yii::app()->end();
}
All it did was autosuggest for id_peg. When clicked some random id_peg, nama_peg and jabatan_peg were still empty.
What did I do wrong?

Actually the problem is controllers/sitecontroller.php page.
You are sending only id_peg as JSON format but trying to get as ui.item.jabatan_peg.
It is always better to send your value as
[{"id":"Caprimulgus europaeus","value":"European Nightjar"}] pattern
and get the value ui.item.value at your select function.

Related

tableselect make specific checkbox checked

foreach ($courses as $user) {
$options[$user['uid']] = [
'coursecode' => $user['coursecode'],
'coursename' => $user['coursename'],
'credithours' => $user['credithours'],
'coursetype' => $user['coursetype'],
'building' => $user['building'],
'place' => $user['place'],
'day' => $user['day'],
'time' => $user['time'],
];
}
$form['table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No Courses found'),
'#js_select' => false,
'#attributes' => ['checked' => 'checked'],
];
this code generates all courses I can register is there any way to check specific values as default, I had a problem with making a specific checkbox checked
so anyhelp ?
I'm using drupal 8
//Inside your foreach loop
foreach($courses as $user) {
$default_value[$user['uid']] = 1; // use 1 or 0 as appropriate
}
// now with your table select
$form['table']['#default_value'] = $default_value;
// default_value is not provided with each options, rather an array of default values is provided in the tableselect main array.
Hope this solves your problem.

Auto populate dropDownListRow form field in yii from database

I have a form having some multi select dropdowns in yii. I have to auto populate the selected values in that multi select box when comes to edit the data. I am fetching the data and passing it inot view file, but am not sure how can i populate the values in the dropdown. please help me
Here is the code for the multi select drop down
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;'));
For multiple select you need to use 'options' parameter:
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;', 'options' => array(
1 => array('selected' => 'selected'),
2 => array('selected' => 'selected'),
)));
Where 1 and 2 are domain ids taken from the $_POST;
You can do this in the action:
$post = $this->getRequest()->getPost('ModelName');
$selectedDomains = $post['domain_id']; // this should be an array or selected values
$selected = array_fill(0, count($selectedDomains), array('selected' => 'selected')); // this constructs the 'options' value from view
$selectedOptions = array_combine($selectedDomains, $selected) // this is the 'options' array with selected values as keys and htmlOptions as values
Also, the code is untested and you need to do your validations and other logic stuff yourself.

CakePHP: How to retrieve data by multiple condition

Please help me to retrieve data from a table by multiple condition in Cakephp
I have one table name: article; I have tried to retrieve data with the code below
I want to get specific id as given in the parameter; article_price > 0 and article_status > 1
public function getArticle($artID = ''){
return $this->find('all', array(
'condition' => array(
'article_id =' => $artID,
'article_price' => '> 0',
'article_status = ' => '1'),
'order' => 'article_id DESC'
));
}
// the out put was selected all data without condition that I want.
What was the problem with my code?
What I found out is I print: echo $this->element ('sql_dump'); and I got the following sql statement:
SELECT `article`.`article_id`, `article`.`name`, `article`.`article_price`, `article`.`article_status` FROM `db_1stcakephp`.`article` AS `article` WHERE 1 = 1 ORDER BY `article_id` DESC
Please help me.
Thank!
If your model name is Article:
public function getArticle($art_id) {
return $this->find('first', array(
'conditions' => array(
'Article.article_id' => $art_id,
'Article.article_price >' => 0,
'Article.article_status >' => 1,
),
));
}
Using 'Model.field' syntax is optional, until your models have relationship and have the same names - for example Article.status and Author.status.
Moving comparison sign into array's key part allows you to do:
'Article.price >' => $minPrice,
'Article.price <=' => $maxPrice,
And I didn't really notice typo in 'conditions'.

How to search in fulltext index using php in mongodb

I'm using mongodb 2.4 and added fulltext index to the "title" field in one the collection. How should I search something in that field using php?
This is the code I use right now:
$params = array(
'_id' => array(
'$gt' => (int)$gt
)
);
$r = $this->collection->find( $params )->limit($limit);
This seem to be the answer to my question:
<?php
$result = $db->command(
array(
'text' => 'bar', //this is the name of the collection where we are searching
'search' => 'hotel', //the string to search
'limit' => 5, //the number of results, by default is 1000
'project' => Array( //the fields to retrieve from db
'title' => 1
)
)
);
http://www.php.net/manual/en/mongodb.command.php#111891

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/

Categories