Make a php array load faster - php

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

Related

Create custom field inside OptionsetField in Silverstripe CMS 3.1

I want to choose the type of my calendar events from some predefined values but also, create a new (custom) type if it's not listed.
So i have created the field in $db like so:
'Type' => 'Varchar',
'EventCustomType' => 'Varchar'
Then, in the getCMSFields() i have:
$f->addFieldsToTab("Root.Main", $eventType = new OptionsetField(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => TextField::create('EventCustomType','Event type')
)
)
);
The problem is that i don't know how to insert the label "Custom" before the Textareafield and style them in the same line.
Also, i'm not sure if i need a second field for the custom one. Can i insert the custom value inside "Type" field or validate it ?
Thanks for any suggestions
This could be achieved by having a separate field for "EventCustomType" and then using Display Logic to show it with something like...
$eventType = OptionsetField::create(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => _t('CalendarEvent.CUSTOM','Custom'),
)
);
$fEventCustomType = TextField::create('EventCustomType','Event type')
->displayIf('Type')->isEqualTo('custom');
$f->addFieldsToTab("Root.Main", array($eventType,$fEventCustomType));
As an alternative if you wanted to rescue This module then you could create this to save into one field as it is designed to do as you are asking... but it is with an error (last time I tried) so it is refernce only for now.
Finally, i have figured it out with separate fields:
$eventType = OptionsetField::create(
'Type',
_t('CalendarEvent.EVENTTYPE','Type'),
array (
'music' => _t('CalendarEvent.MUSIC','Music'),
'sport' => _t('CalendarEvent.SPORT','Sport'),
'drama' => _t('CalendarEvent.DRAMA','Drama'),
'custom' => _t('CalendarEvent.CUSTOM','Custom'),
)
);
$customEventType = TextField::create('EventCustomType','Custom type');
$f->addFieldsToTab("Root.Main", array($eventType,$customEventType));
and jQuery:
$('#Root_Main').entwine({
onmatch: function(){
var $tab = this.closest('.tab');
var $eventType = $tab.find('ul.eventType');
var $customType = $tab.find('.customEventType').hide();
if($eventType.find('input:checked').val() == 'custom'){
$customType.show();
}
$eventType.find('input').change(function(){
if($(this).val() == 'custom'){
$customType.show();
}else{
$customType.hide();
}
});
}
});

how to edit checkbox values in drupal 7

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 ?

how to create treeview in yii framework

I want to create a tree view which fetching
data from Database and after that
order it by parent field
So table fields are included :
product_category_id
product_category_parent_id
product_category_name
the record that is been mentioned
by name " product_category_parent_id"
is 0 without any root and when it wish have any
ID code / number , parent Id should come in this palce
so structure of the table must be sent to the
View :
<ul><li><ul><li></li></ul></li></ul>
There is an example how to create CTreeView
private function generateTree($models)
{
$data = array();
foreach ($models as $category) {
$data[$category->id] = array(
'id' => $category->id,
'text' => ''.$category->category_name.'',
);
foreach ($category->goods as $item) {
$data[$category->id]['children'][$item->id] = array(
'id' => $item->id,
'text' => ''.$item->article.'',
'expanded' => false,
);
}
}
return $data;
}
In view
$this->widget('CTreeView', array('data' => $data,'persist'=>'cookie'));

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