简体   繁体   中英

Trying to set up a A-Z filter

Im trying to set a AZ filter for a table database in PHP. Below I have provided the code I have been working with. In between the bars is the code that I am trying to use. For some reason its saying I need to select a database and I thought I did that. Please any help would be appreciated.

Please and Thank You

/**
 * 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' => '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 -> 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 -> 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 -> 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 -> condition('cd.iaas', true); }
  if ($paas_required){ $qry -> condition('cd.paas', true); }
  if ($saas_required){ $qry -> condition('cd.saas', true); }
  if ($eaas_required){ $qry -> condition('cd.eaas', true); }

  $qry -> orderBy('cd.company', 'ASC');
  $company_rows = $qry -> execute();

  foreach ($company_rows as $row){
    $company = cloud_computing_data_company::build($row);
    $companies[$company -> name] = $company->to_array();
  }

  $companies_themed = array();
  foreach($companies as $name => $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' => $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' => $companies_themed));
}



//----------------------------------------------------------------------------------


// filter by company name


/* Get the letter user clicked on and assign it a variable called $sort */
$sort = $_REQUEST['letter'];

/* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */
if($sort == ""){
$qry= "SELECT * FROM cloud_computing_capability_data ORDER BY companies ASC " ;
}else{
/* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */
$qry = "SELECT * FROM cloud_computing_capability_data WHERE companies LIKE '$sort%' ORDER BY companies ASC" ;
}
/* Notice the use of '%' wilde card in the above query  "LIKE '$sort%'". */

//next step is to execute the query.
$execute = mysql_query($qry) or die(mysql_error());

/* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */
echo "<p>" ;
for ($i = 65; $i < 91; $i++) {
    printf('<a href="%s?letter=%s">%s</a> | ',
    $PHP_SELF, chr($i), chr($i));
}
echo "</p>" ;

/* now we are ready to display the results. Since out cloud_computing_capability_data table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table.
To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */
if(mysql_num_rows($execute)>0){
do{
echo "<p>" .$result['companies']. "</p>" ;
}while($result = mysql_fetch_assoc($execute));
}else{
echo "<p>No customer found.</p>" ;
}


//-----------------------------------------------------------------------------------

Unless db_select is a custom function the function to call is *select_db*. Also I did not see a connection created but I assume that was done earlier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM