2013-04-26 3 views
0

PHP에서 테이블 데이터베이스에 A-Z 필터를 설정하려고합니다. 아래에서는 내가 작업 한 코드를 제공했습니다. 바 사이에는 내가 사용하려고하는 코드가 있습니다. 어떤 이유로 나는 데이터베이스를 선택해야한다고 말했고 나는 그것을했다고 생각했습니다. 제발 어떤 도움을 주시면 감사하겠습니다. db_select 호출 할 수있는 기능 * * select_db하는 사용자 정의 함수를 제외A-Z 필터를 설정하려고합니다.

하십시오와 당신

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


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

답변

0

감사드립니다. 또한 연결이 생성 된 것을 보지 못했지만 이전에 완료되었다고 가정합니다.

+0

이것은 시트 노드를 사용하여 스프레드 시트를 가져온 사용자 정의 Drupal 모듈이며 코드가이를 데이터베이스와 테이블로 변환합니다. 내가 그것을 만들거나 그것을 설계하지 않았기 때문에 100 % 확신하지 못했습니다.하지만 코드에 기반하여 수집 한 것입니다. – Alan

관련 문제