0

我试图在 PHP 中为表数据库设置 AZ 过滤器。下面我提供了我一直在使用的代码。栏之间是我尝试使用的代码。出于某种原因,它说我需要选择一个数据库,我以为我做到了。请任何帮助将不胜感激。

谢谢,麻烦您了

/**
 * 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>" ;
}


//-----------------------------------------------------------------------------------
4

1 回答 1

0

除非 db_select 是自定义函数,否则要调用的函数是 *select_db*。我也没有看到创建的连接,但我认为这是之前完成的。

于 2013-04-26T17:09:44.337 回答