Note that there are some explanatory texts on larger screens.

plurals
  1. POSet up a A-Z Bar in a Custom Drupal module using PHP
    primarykey
    data
    text
    <p>Im trying to use print a A-Z bar to filter entries based on the letter click. (i.e click "A" it only show the "A" listing, click "B" show only the "B" listing, if there arent "C" listing than "C" doesnt show, "A-B-D-E") </p> <p>This is in PHP and is part of a custom DRUPAL module. The contents are already alphabetized.</p> <p>I know there is alot of code below, I just dont know where or what to apply to solve this problem.</p> <p>Thank you in advance for all your help.</p> <p>Alan</p> <p>Im trying to get the RED OUTLINED box into the RED FILLED-IN box.</p> <p><img src="https://i.stack.imgur.com/ZkB9x.png" alt="enter image description here"></p> <p>Sorry it took so long to get an image up. Thank you for all your help!</p> <pre><code>&lt;?php // NITAAC Calendar Synchronization Module // Based off of Assyst's origonal solution // Re-written to only pull updates where needed // TODO: write a proper help section function cloud_computing_data_help($path, $arg){ switch ($path){ case "admin/help#cloud_computing_data": $text = '&lt;p&gt;' . t("TODO - WRITE A HELP FILE.") . '&lt;/p&gt;'; return $text; break; } } /** * Menu functions */ function cloud_computing_data_menu() { $items = array(); $items['cloud-computing/cio-sp3'] = array( 'title' =&gt; 'Cloud Computing', 'description' =&gt; '', 'page callback' =&gt; 'cloud_computing_data_grid_display', 'access callback' =&gt; TRUE, ); $items['cloud-computing/update-from-node/%'] = array( 'title' =&gt; 'Cloud Computing data update', 'description' =&gt; 'Utility to update the CIO-SP3/SB Cloud Computing data in the database based on a sheetnode', 'page callback' =&gt; 'cloud_computing_data_populate_from_node', 'page arguments' =&gt; array(2), 'access arguments' =&gt; array('access administration pages'), ); return $items; } /** * Block Definitions */ function cloud_computing_data_block_info(){ $blocks['cloud_computing_filters'] = array( 'info' =&gt; t('NITAAC Cloud Computing Filters'), 'status' =&gt; 1, 'region' =&gt; 'subfeature_top', 'visibility' =&gt; BLOCK_VISIBILITY_LISTED, 'weight' =&gt; '999', 'pages' =&gt; 'cloud-computing/cio-sp3', 'cahce' =&gt; DRUPAL_CACHE_PER_ROLE, ); return $blocks; } function cloud_computing_data_theme(){ $module_path = drupal_get_path('module', 'cloud_computing_data'); $base = array( 'file' =&gt; 'theme.inc', 'path' =&gt; "$module_path/theme", ); return array( 'cloud_computing_page' =&gt; $base + array( 'template' =&gt; 'cloud-computing-page', 'variables' =&gt; array('companies' =&gt; array()), ), 'cloud_computing_item' =&gt; $base + array( 'template' =&gt; 'cloud-computing-item', 'variables' =&gt; array('company' =&gt; array()), ), 'cloud_computing_item_details' =&gt; $base + array( 'template' =&gt; 'cloud-computing-item-details', 'variables' =&gt; array('company' =&gt; array()), ), ); } /// Page Definition Functions ////////////////////////////////////////////////// function cloud_computing_data_grid_display_filters(){ // form definition $filters = array( '#method' =&gt; 'get', '#tree' =&gt; true, '#theme_wrappers' =&gt; array('form'), '#no_redirect' =&gt; true, '#always_process' =&gt; true, '#type' =&gt; 'form', '#token' =&gt; false, '#after_build' =&gt; array('cloud_computing_data_grid_display_filters_unset_id'), '#attributes' =&gt; array( 'class' =&gt; array('cc-filters'), ), ); // get service provider listing $qry = db_select('cloud_computing_capability_data', 'cd'); $qry -&gt; fields('cd', array( 'service_provider', )) -&gt; orderBy('service_provider', 'ASC') -&gt; groupBy('service_provider'); $result = $qry -&gt; execute(); $providers = array(); foreach($result as $provider_serialized){ $provider_parts = explode(';',$provider_serialized-&gt;service_provider); foreach($provider_parts as $part){ $part = trim($part); if (!empty($part)){ $providers[$part] = $part; } } } $qry = db_select('cloud_computing_capability_data', 'cd'); $qry -&gt; fields('cd', array( 'contract', )) -&gt; orderBy('contract', 'ASC') -&gt; groupBy('contract'); $result = $qry -&gt; execute(); $contracts = array(); foreach($result as $row){ $contracts[$row-&gt;contract] = $row-&gt;contract; } // contract dropdown $filters['contract'] = array( '#type' =&gt; 'select', '#title' =&gt; t('Contract'), '#default_value' =&gt; 'Any', '#options' =&gt; array( 'any' =&gt; 'Any' ), '#multiple' =&gt; false, '#name' =&gt; 'contract', ); foreach($contracts as $contract){ $contract_plain = check_url($contract); $contract_plain = preg_replace('/\s/','-',strtolower($contract_plain)); $filters['contract']['#options'][$contract_plain] = $contract; } // providers dropdown $filters['service_provider'] = array( '#type' =&gt; 'select', '#title' =&gt; t('Service Provider'), '#default_value' =&gt; 'Any', '#options' =&gt; array( 'any' =&gt; 'Any' ), '#multiple' =&gt; false, '#name' =&gt; 'provider', ); foreach($providers as $provider){ $provider_plain = check_url($provider); $provider_plain = preg_replace('/\s/','-',strtolower($provider_plain)); $filters['service_provider']['#options'][$provider_plain] = $provider; } // services checkboxes $filters['services'] = array( '#attributes' =&gt; array( 'class' =&gt; array('checkbox-list'), ), '#type' =&gt; 'container', ); $filters['services']['iaas'] = array( '#type' =&gt; 'checkbox', '#title' =&gt; t('IaaS'), '#value' =&gt; false, '#name' =&gt; 'iaas', ); $filters['services']['paas'] = array( '#type' =&gt; 'checkbox', '#title' =&gt; t('PaaS'), '#value' =&gt; false, '#name' =&gt; 'paas', ); $filters['services']['saas'] = array( '#type' =&gt; 'checkbox', '#title' =&gt; t('SaaS'), '#value' =&gt; false, '#name' =&gt; 'saas', ); $filters['services']['eaas'] = array( '#type' =&gt; 'checkbox', '#title' =&gt; t('EaaS'), '#value' =&gt; false, '#name' =&gt; 'eaas', ); // if the form was submitted previously... if (!empty($_GET)){ // handle previous submissions manually for service provider if (isset($_GET['provider'])){ $provider_plain = check_url($_GET['provider']); $provider_plain = preg_replace('/\s/','-',strtolower($provider_plain)); if (isset($filters['service_provider']['#options'][$provider_plain])){ $filters['service_provider']['#value'] = $provider_plain; } $filters['us_hosting']['#value'] = check_plain($_GET['hosted']); } // handle previous submissions manually for contract if (isset($_GET['contract'])){ $contract_plain = check_url($_GET['contract']); $contract_plain = preg_replace('/\s/','-',strtolower($contract_plain)); if (isset($filters['contract']['#options'][$contract_plain])){ $filters['contract']['#value'] = $contract_plain; } } // handle previous submissions manually for services foreach($filters['services'] as $k =&gt; &amp;$service){ if (isset($_GET[$service['#name']])){ $service['#value'] = $_GET[$service['#name']] ? true : false; } } } // add service description label $filters['services']['description'] = array( '#type' =&gt; 'markup', '#name' =&gt; 'Services Offerings', '#markup' =&gt; '&lt;div class="checkbox-label"&gt;Service Offerings&lt;/div&gt;', '#weight' =&gt; -1, ); // services checkboxes $filters['filter'] = array( '#attributes' =&gt; array( 'class' =&gt; array('submission-buttons'), ), '#type' =&gt; 'container', ); // add service description label $filters['filter']['description'] = array( '#type' =&gt; 'markup', '#name' =&gt; 'Filter Results', '#markup' =&gt; '&lt;div class="submission-buttons-label"&gt;Filter Results&lt;/div&gt;', '#weight' =&gt; -1, ); // add submit button $filters['filter']['submit'] = array( '#type' =&gt; 'submit', '#value' =&gt; t('Filter'), '#submit' =&gt; array('cloud_computing_data_grid_display_filters_submit'), '#name' =&gt; '', '#processed' =&gt; true, ); $path = base_path() . 'cloud-computing/cio-sp3'; $filters['filter']['reset'] = array( '#type' =&gt; 'markup', '#name' =&gt; 'reset', '#markup' =&gt; '&lt;a class="reset-button" href="' . $path . '"&gt;Reset&lt;/a&gt;' ); return $filters; } function cloud_computing_data_grid_display_filters_unset_id($form){ unset($form['#build_id'], $form['form_build_id'], $form['form_id']); return $form; } function cloud_computing_data_grid_display_filters_submit(){ return; } function cloud_computing_data_block_view($delta = ''){ switch($delta){ case 'cloud_computing_filters': $block['subject'] = t('Cloud Computing Filters'); $form_state = array(); $filters = drupal_build_form('cloud_computing_data_grid_display_filters', $form_state); $filters['#action'] = base_path() . 'cloud-computing/cio-sp3'; $block['content'] = drupal_render($filters); return $block; } return null; } /** * Class to help make managing company data easier. */ class cloud_computing_data_company{ // instance variables public $name = ''; public $contract = ''; public $roles = array(); public $service_providers = array(); public $cloud_types = array(); public $us_hosted = array(); public $iaas = false; public $paas = false; public $saas = false; public $eaas = false; public $fedramp = false; public $other = false; // function to build object from DB row public static function build($row){ $instance = new cloud_computing_data_company(); $instance -&gt; ccid = $row -&gt; ccid; $instance -&gt; delta = $row -&gt; delta; $instance -&gt; name = $row -&gt; company; $instance -&gt; contract = $row -&gt; contract; $instance -&gt; roles = explode(';',$row -&gt; role); $instance -&gt; service_providers = explode(';',$row -&gt; service_provider); $instance -&gt; cloud_types = explode(';',$row -&gt; cloud_type); $instance -&gt; us_hosted = explode(';',$row -&gt; us_hosted); $instance -&gt; iaas = $row -&gt; iaas; $instance -&gt; paas = $row -&gt; paas; $instance -&gt; saas = $row -&gt; saas; $instance -&gt; eaas = $row -&gt; eaas; $instance -&gt; fedramp = $row -&gt; fedramp; $instance -&gt; other = $row -&gt; other; return $instance; } // function to un-foobar an array of terribly formatted spreadsheet data public static function un_foobar_array($foobar_array){ $assoc_parts = array(); foreach($foobar_array as $foobar_item){ $foobar_item = preg_replace('/\?/','',$foobar_item); $foobar_item = preg_replace('/(,|;|(&lt;br\/&gt;)|([0-9]\.))/',',',$foobar_item); $foobar_parts = explode(',', $foobar_item); // BOOM! &lt;-- FUUUUNNY foreach($foobar_parts as $part){ $part = trim($part); if (!empty($part)){ $assoc_parts[$part] = $part; } } } $fixed_array = array(); foreach($assoc_parts as $k =&gt; $v){ $fixed_array[] = $v; } sort($fixed_array); return $fixed_array; } // function to merge multiple companies into one // I wish it were as effecient as the one AT&amp;T uses to combat anti-monopoly measures &lt;--- FUNNY AGAIN public static function merge($companies){ $merged_company = new cloud_computing_data_company(); // uncommented string processing code ahead foreach($companies as $company){ if (!empty($company -&gt; name)){ $merged_company -&gt; name = trim($company -&gt; name); } $company -&gt; contract = trim($company -&gt; contract); if (!empty($company -&gt; contract)){ $merged_company -&gt; contract = trim($company -&gt; contract); } $merged_company -&gt; roles = array_merge($merged_company-&gt;roles, $company-&gt;roles); $merged_company -&gt; service_providers = array_merge($merged_company-&gt;service_providers, $company-&gt;service_providers); $merged_company -&gt; cloud_types = array_merge($merged_company-&gt;cloud_types, $company-&gt;cloud_types); $merged_company -&gt; us_hosted = array_merge($merged_company-&gt;us_hosted, $company-&gt;us_hosted); if (($company-&gt;iaas) &amp;&amp; (strtolower($company-&gt;iaas) != 'no')){ $merged_company -&gt; iaas = true; } if (($company-&gt;paas) &amp;&amp; (strtolower($company-&gt;paas) != 'no')){ $merged_company -&gt; paas = true; } if (($company-&gt;saas) &amp;&amp; (strtolower($company-&gt;saas) != 'no')){ $merged_company -&gt; saas = true; } if (($company-&gt;eaas) &amp;&amp; (strtolower($company-&gt;eaas) != 'no')){ $merged_company -&gt; eaas = true; } $company -&gt; fedramp = trim($company -&gt; fedramp); if (!empty($company -&gt; fedramp)){ $merged_company -&gt; fedramp = trim($company -&gt; fedramp); } $company -&gt; other = trim($company -&gt; other); if (!empty($company -&gt; other)){ $merged_company -&gt; other = trim($company -&gt; other); } } // fix all of the corrupt arrays $merged_company -&gt; roles = cloud_computing_data_company::un_foobar_array($merged_company -&gt; roles); $merged_company -&gt; service_providers = cloud_computing_data_company::un_foobar_array($merged_company -&gt; service_providers); $merged_company -&gt; cloud_types = cloud_computing_data_company::un_foobar_array($merged_company -&gt; cloud_types); $merged_company -&gt; us_hosted = cloud_computing_data_company::un_foobar_array($merged_company -&gt; us_hosted); return $merged_company; } // function to get DB fields public static function db_fields(){ return array( 'ccid', 'delta', 'company', 'contract', 'role', 'service_provider', 'cloud_type', 'us_hosted', 'iaas', 'paas', 'saas', 'eaas', 'fedramp', 'other', ); } // function to convert to array for DB insertion public function db_values(){ return array( 'ccid' =&gt; $this-&gt;ccid, 'delta' =&gt; $this-&gt;delta, 'company' =&gt; $this-&gt;name, 'contract' =&gt; $this-&gt;contract, 'role' =&gt; implode(';',$this-&gt;roles), 'service_provider' =&gt; implode(';',$this-&gt;service_providers), 'cloud_type' =&gt; implode(';',$this-&gt;cloud_types), 'us_hosted' =&gt; implode(';',$this-&gt;us_hosted), 'iaas' =&gt; $this-&gt;iaas, 'paas' =&gt; $this-&gt;paas, 'saas' =&gt; $this-&gt;saas, 'eaas' =&gt; $this-&gt;eaas, 'fedramp' =&gt; $this-&gt;fedramp, 'other' =&gt; $this-&gt;other, ); } // function to convert to array for theming public function to_array(){ return array( 'ccid' =&gt; $this-&gt;ccid, 'delta' =&gt; $this-&gt;delta, 'company' =&gt; $this-&gt;name, 'name' =&gt; $this-&gt;name, 'contract' =&gt; $this-&gt;contract, 'roles' =&gt; $this-&gt;roles, 'service_providers' =&gt; $this-&gt;service_providers, 'cloud_types' =&gt; $this-&gt;cloud_types, 'us_hosted' =&gt; $this-&gt;us_hosted, 'iaas' =&gt; $this-&gt;iaas, 'paas' =&gt; $this-&gt;paas, 'saas' =&gt; $this-&gt;saas, 'eaas' =&gt; $this-&gt;eaas, 'fedramp' =&gt; $this-&gt;fedramp, 'other' =&gt; $this-&gt;other, ); } } /** * This function displays a grid of cloud computing companies */ function cloud_computing_data_grid_display($arg){ // Definitely should have just used views here. Opted to do it live, because // I didn't want to create more useless nodes to store cloud // computing details. Really should have just made a "cloud computing dossier" // content type or something. At least this executes (comparitively) fast... $form_state = array('method' =&gt; 'get'); $filters = drupal_build_form('cloud_computing_data_grid_display_filters', $form_state); // get companies ------------use this to filter company names and apply A-Z Filter---------- $companies = array(); // array of company names for filtering $qry = db_select('cloud_computing_capability_data', 'cd'); $qry -&gt; fields('cd', cloud_computing_data_company::db_fields()); // add service provider filter if (!empty($form_state['values']['service_provider'])){ if (strtolower($form_state['values']['service_provider']) != 'any'){ $provider_plain = check_plain($form_state['values']['service_provider']); $provider_wildcards = preg_replace('/-/','%',strtolower($provider_plain)); $provider_wildcards = '%' . $provider_wildcards . '%'; $qry -&gt; condition('cd.service_provider', $provider_wildcards, 'LIKE'); } } // add service provider filter if (!empty($form_state['values']['contract'])){ if (strtolower($form_state['values']['contract']) != 'any'){ $contract_plain = check_plain($form_state['values']['contract']); $contract_wildcards = preg_replace('/-/','%',strtolower($contract_plain)); $contract_wildcards = '%' . $contract_wildcards . '%'; $qry -&gt; condition('cd.contract', $contract_wildcards, 'LIKE'); } } // filter by services offered $iaas_required = $form_state['values']['services']['iaas']; $paas_required = $form_state['values']['services']['paas']; $saas_required = $form_state['values']['services']['saas']; $eaas_required = $form_state['values']['services']['eaas']; if ($iaas_required){ $qry -&gt; condition('cd.iaas', true); } if ($paas_required){ $qry -&gt; condition('cd.paas', true); } if ($saas_required){ $qry -&gt; condition('cd.saas', true); } if ($eaas_required){ $qry -&gt; condition('cd.eaas', true); } $qry -&gt; orderBy('cd.company', 'ASC'); $company_rows = $qry -&gt; execute(); foreach ($company_rows as $row){ $company = cloud_computing_data_company::build($row); $companies[$company -&gt; name] = $company-&gt;to_array(); } $companies_themed = array(); foreach($companies as $name =&gt; $company){ $company['services_display'] = array(); $company['services_display']['IaaS'] = cloud_computing_data_wrap_service($company['iaas'], 'IaaS'); $company['services_display']['PaaS'] = cloud_computing_data_wrap_service($company['paas'], 'PaaS'); $company['services_display']['SaaS'] = cloud_computing_data_wrap_service($company['saas'], 'SaaS'); $company['services_display']['EaaS'] = cloud_computing_data_wrap_service($company['eaas'], 'EaaS'); $companies_themed[] = theme('cloud_computing_item', array('company' =&gt; $company)); } $res_path = drupal_get_path('module', 'cloud_computing_data'); drupal_add_css($res_path . '/theme/cloud-computing.css'); drupal_add_js($res_path . '/theme/cloud-computing-grid.js'); return theme('cloud_computing_page', array('companies' =&gt; $companies_themed)); } // filter by company name /** * This function wraps a service name in a span and adds an icon in order * to indicate whether a given company provides said service. */ function cloud_computing_data_wrap_service($value, $name){ global $base_url; $module_path = $base_url . '/' . drupal_get_path('module', 'cloud_computing_data'); $returnVal = ""; if ($value){ $returnVal .= "&lt;span class='cloud-service-offering offered' title='This contract holder provides $name.'&gt;$name"; $returnVal .= "&lt;img class='icon' src='{$module_path}/theme/service-provided.png' alt='This contract holder provides $name.'/&gt;"; $returnVal .= "&lt;/span&gt;"; } else { $returnVal .= "&lt;span class='cloud-service-offering not-offered' title='This contract holder does not provide $name.'&gt;$name"; $returnVal .= "&lt;img class='icon' src='{$module_path}/theme/service-not-provided.png' alt='This contract holder does not provide $name.'/&gt;"; $returnVal .= "&lt;/span&gt;"; } return $returnVal; } /** * The function that went here has been deleted. */ /// Database Population Functions ////////////////////////////////////////////// /** * This function accepts the nid of a sheetnode, and updates the cloud computing * database using the data from said node. It is indended to be triggered via * a url redirect (implemented by Rules) after saving the node in question. */ function cloud_computing_data_populate_from_node($nid){ // ensure we have a viable node to work with $node = node_load($nid); if (!$node){ drupal_set_message('The specified node was not found.', error); return array(); } if ($node -&gt; type != 'sheetnode'){ drupal_set_message('The specified node is not of the correct type (not a sheetnode).', error); return array(); } // include socialcalc api functionality from sheetnode require_once(drupal_get_path('module', 'sheetnode') . '/socialcalc.inc'); // standard column mapping for cloud computing sheet $col_mapping = array( 'A' =&gt; 'company', 'B' =&gt; 'contract', 'C' =&gt; 'role', 'D' =&gt; 'service_provider', 'E' =&gt; 'cloud_type', 'F' =&gt; 'us_hosted', 'G' =&gt; 'iaas', 'H' =&gt; 'paas', 'I' =&gt; 'saas', 'J' =&gt; 'eaas', 'K' =&gt; 'fedramp', 'L' =&gt; 'other', ); $sheetnodes = db_query("SELECT * FROM {sheetnode} WHERE nid in (:nid) GROUP BY nid", array(':nid' =&gt; $nid)); if (empty($sheetnodes)){ throw new Exception('Unable to find a sheetnode with matching id in database.'); } foreach($sheetnodes as $sheetnode){ $sheet = socialcalc_parse_sheet((string) $sheetnode-&gt;value); } // transpose values into an easy-to-reference 2d array $rows = array(); foreach($sheet['cells'] as $loc =&gt; $cell){ $row = preg_replace('/[^0-9]/', '',$loc); $col = preg_replace('/[0-9]/', '',$loc); $rows[$row][$col] = $cell['datavalue']; } $unmerged_companies = array(); $last_company_name = ''; foreach($rows as $k =&gt; $row){ $mapped_row = array(); foreach($row as $row_key =&gt; $row_val){ $mapped_row[$col_mapping[$row_key]] = $row_val; } $company = cloud_computing_data_company::build((object)$mapped_row); if (!(empty($company-&gt;name))){ $last_company_name = $company -&gt; name; } else { $company -&gt; name = $last_company_name; } if (!isset($unmerged_companies[$company -&gt; name])){ $unmerged_companies[$company -&gt; name] = array(); } $unmerged_companies[$company -&gt; name][] = $company; } $companies = array(); foreach($unmerged_companies as $name =&gt; $company_group){ $company = cloud_computing_data_company::merge($company_group); if (strtolower($company -&gt; name) != 'company'){ $companies[$company -&gt; name] = $company; } } // clear existing data $query = db_delete('cloud_computing_capability_data') -&gt; where(1); $query -&gt; execute(); // insert new values into database. $query = db_insert('cloud_computing_capability_data'); $query -&gt; fields(cloud_computing_data_company::db_fields()); $ccid = 0; foreach($companies as $name =&gt; $row){ $delta = 0; $row-&gt;ccid = $ccid; $row-&gt;delta = $delta; $query-&gt;values($row-&gt;db_values()); $ccid++; } $query-&gt;execute(); drupal_set_message('The CIO-SP3 / SB Cloud Computing database has been updated!'); drupal_set_message("Debug info: $ccid companies recognized and added to database."); drupal_goto("node/$nid"); return array(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload