how to edit checkbox values in drupal 7 - php

Initially i am inserting multiple ids through multiple checkboxes. Now i want to open that page again for edit but i want some of the checkbox checked based on id i have inserted priviously.
$courses contains all the nodes which i need to desplay and $checkedarray are the nodes which comes from database[id which got inserted after submission].
here is the code to uderstand the work..
$vocabulary = taxonomy_vocabulary_machine_name_load('xxx list');
$terms = taxonomy_get_tree($vocabulary->vid);
$courses = array();
foreach($terms as $term) {
if($term->parents[0]==0){
$courses[$term->tid] = $term->name."<br />";
}
else{
$parents = taxonomy_get_parents($term->tid);
$parentsName = $parents[$term->parents[0]]->name.' / ';
$courses[$term->tid] = $parentsName.$term->name."<br />";
}
}
$form['addlicense']['categories'] = array(
'#type' => 'checkboxes',
'#title' => t('Series'),
'#options' => $courses,
'#attributes' => array('class' => array('series-list')),
'#required' => TRUE,
);
$checkedarray = array(5,6,7,8,9,10);
Now i have list of node which need to be appered as checked checkboxes which is in $checkedarray array... any help whould be appreciated

Did you tried using the #default_value attribute and setting it to $checkdarray ?

Related

Make a php array load faster

I successfully edited the checkout page of the WooCommerce city field in order to be a dropdown field with a lot of cities in it around 13k values the problem is that on mobile and some low-end devices this field isn't performing well how I can make this array load faster is a DB call worth it in this situation, is there any way stop the dropdown and make the field only searchable is it worth splitting the array or loading from a text file?
The code I made
<?php
// Add custom checkout select fields
add_filter('woocommerce_checkout_fields', 'add_custom_checkout_select_fields');
function add_custom_checkout_select_fields($fields)
{
$arr = array(); // very big array arround 13k values
// Define HERE in the array, your desired cities
$cities = $arr;
// Format in the right way the options array of cities
$options = array(
'' => __('Choose City', 'woocommerce') . '…'
);
foreach ($cities as $city)
{
$options[$city] = $city;
}
// Adding 2 custom select fields
$fields['billing']['billing_city2'] = $fields['shipping']['shipping_city2'] = array(
'type' => 'select',
'required' => true,
'options' => $options,
'autocomplete' => 'address-level2',
'input_class' => array(
'wc-enhanced-select',
)
);
// Copying data from WooCommerce city fields
$fields['billing']['billing_city2']['class'] = array_merge($fields['billing']['billing_city']['class'], array(
'hidden'
));
$fields['shipping']['shipping_city2']['class'] = array_merge($fields['shipping']['shipping_city']['class'], array(
'hidden'
));
$fields['billing']['billing_city2']['label'] = $fields['billing']['billing_city']['label'];
$fields['shipping']['shipping_city2']['label'] = $fields['shipping']['shipping_city']['label'];
$fields['billing']['billing_city2']['priority'] = $fields['billing']['billing_city']['priority'] + 5;
$fields['shipping']['shipping_city2']['priority'] = $fields['shipping']['shipping_city']['priority'] + 5;
wc_enqueue_js("
jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = { minimumResultsForSearch: 1 };
jQuery( this ).select2( select2_args ).addClass( 'enhanced' );
});");
return $fields;
}

How to use OR condition here

I have a table records and another table categories
I want to get get all the records in this categories
$all_categories = '5,6,7,8';
So I am using this code:
$query = $this->Records->find('all',[
'contain' => ['Categories']
]);
if(!empty($search)){
$query->where(['Records.title LIKE' => '%'.$search.'%']);
}
if(!empty($wilaya)){
$query->where(['Records.adresse LIKE' => '%'.$wilaya.'%']);
}
if(!empty($cat)){
$query->where(['Records.category_id =' => $cat]);
} else {
$categories_array = explode(',',$all_categories);
foreach($categories_array as $category) {
$query->where(['Records.category_id =' => $category]);
}
}
When I use this, I'm getting AND-conditions by default.
How can I get OR-conditions instead?
Use IN:
$all_categories = '5,6,7,8';
$categories_array = explode(',',$all_categories);
$query->where(['Records.category_id IN' => $categories_array]);
This should work:
$all_categories = '5,6,7,8';
$array=explode(',',$all_categories);
$query->where(['Records.category_id' => $array], ['Records.category_id' => 'integer[]']);
Note: Edited answer to add information about the column data type. Won't work without this in CakePHP 3.x.
This equals to:
$all_categories = '5,6,7,8';
$array=explode(',',$all_categories);
$query->where(['Records.category_id IN' => $array]);
See Automatically Creating IN Clauses.

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/

Drupal Select option in form

I am trying to create a select list in drupal that is populated from a custom table in my db.
The table consists of a name and an id number. They are both unique.
I used this to collect the data from the db and to populate two arrays.
$query = "SELECT `id`, title` from {svm_mail_esp}";
$result = db_query($query);
$i=0;
while($row = db_fetch_array($result)) {
$listName[$i] = $row['title'];
$listID[$i] = $row['id'];
$i++;
}
Here is the $form array I used:
$form['esp_refferer'] = array(
'#type' => 'select',
'#title' => 'Service Provider',
'#required' => TRUE,
'#options' => $list,
'#cols' => 10,
'#default_value' => '- Choose -', //TODO: This needs to be fixed and the form cannot be processed while this is selected
'#multiple' => FALSE,
);
This is where my problem is coming in, I want to display the name, but when the form is submitted I need the $node->esp_refferer to be the id number and not the name.
How do I do this?
while($row = db_fetch_array($result)) {
$list[$row['id']] = $row['title'];
}
You need to create an array $list, Keys of that array will be ids and value of each will be respective title.

How do I use a lookup table?

I use the code below to get all the categories from the am_category table into a dropdownbox in a form. It works. But when submitting the form I need the category.ID and not the name. How does one usually work with lookup tables like this one?
$category_result = db_query("SELECT id, name FROM {am_category}");
$categories = array();
foreach($category_result as $row)
{
$categories[$row->id] = t($row->name);
}
$form['category_options'] = array(
'#type' => 'value',
'#value' => $categories
);
$form['category']['category'] = array(
'#title' => t('Category'),
'#type' => 'select',
'#description' => t('Please select the category of this achievement.'),
'#options' => $form['category_options']['#value']
);
The value passed through to the $form_state array in your submit function will be the ID and not the name, it will be the key of the options in the drop down, not the value.
If you want to shorten your code slightly you could use the fetchAllKeyed() method of the database query to build that array of categories automatically. You don't need the category_options element as you'll already have access to that array in the $form variable in your submission function.
Also I'd be careful giving the outer and and inner elements the same name (category), that might cause some problems.
This code should help:
function mymodule_myform($form, &$form_state) {
$categories = db_query("SELECT id, name FROM {am_category}")->fetchAllKeyed();
$form['category_wrapper']['category'] = array(
'#type' => 'select',
'#title' => t('Category'),
'#description' => t('Please select the category of this achievement.'),
'#options' => $categories
);
return $form;
}
function mymodule_myform_submit(&$form, &$form_state) {
$selected_category_id = $form_state['values']['category'];
// Just in case you wanted to know, this would get the corresponding name for the selected category:
$selected_category_name = $form['category_wrapper']['category']['#options'][$selected_category_id];
}

Categories