2013-10-22 45 views
0

我正在使用Drupal 7數據庫API來搜索我的表。我也使用分頁和排序擴展。所以問題是,當我的查詢由於分頁而使用限制時,如何顯示找到的記錄總數?我是否需要運行具有所有TWICE條件的查詢?一旦得到數量和另一個極限?這似乎效率低下。這裏是我的代碼供參考。我是數據庫API的新手,所以請隨時調整我的代碼,或者如果我做錯了什麼,請指點正確的方向。另外,我不這樣做但只有在發生一個條件,但我最終將不得不3.感謝:當使用限制時使用Drupal 7數據庫API獲取總行數

function job_search() { 
    // Initialising output 
    $output = 'SOME STUFF'; 

    // Table header 
    $header = array(
    array('data' => 'Description'), 
    array('data' => 'Location', 'field' => 'job_location_display'), 
    array('data' => 'Specialty', 'field' => 'specialty_description'), 
    array('data' => 'Job Type', 'field' => 'job_board_type'), 
    array('data' => 'Job Number', 'field' => 'job_number'), 
); 

    // Setting the sort conditions 
    if(isset($_GET['sort']) && isset($_GET['order'])) { 
    // Sort it Ascending or Descending? 
    if($_GET['sort'] == 'asc') 
     $sort = 'ASC'; 
    else 
     $sort = 'DESC'; 

    // Which column will be sorted 
    switch($_GET['order']) { 
     case 'Location': 
     $order = 'job_location_display'; 
     break; 
     case 'Specialty': 
     $order = 'specialty_description'; 
     break; 
     case 'Job Number': 
     $order = 'job_number'; 
     break; 
     case 'Job Type': 
     $order = 'job_board_type'; 
     break; 
     default: 
     $order = 'job_number'; 
    } 
    } 
    else { 
    $sort = 'ASC'; 
    $order = 'job_number'; 
    } 

    // Query object 
    $query = db_select("jobs", "j"); 

    // Adding fields 
    $query->fields('j'); 

    if(isset($_GET['location'])) { 
    $query->condition('j.job_state_code', $_GET['location'], '='); 
    } 

    // Set order by 
    $query->orderBy($order, $sort); 

    // Pagination 
    $query = $query->extend('TableSort')->extend('PagerDefault')->limit(20); 

    // Executing query 
    $result = $query->execute(); 

    // Looping for filling the table rows 
    while($data = $result->fetchObject()) { 
    $description = '<div class="thumbnail"><img src="/sites/all/themes/zen/vista_assets/images/job_headers/' . $data->job_image_file . '"/></div>'; 
    $description .= '<div class="title">' . $data->job_board_subtitle . '</div>'; 
    // Adding the rows 
    $rows[] = array($description, $data->job_location_display, $data->specialty_description, $data->job_board_type, $data->job_number);  
    } 

    $output .= theme('pager'); 

    // Setting the output of the field 
    $output .= theme_table(
    array(
     'header' => $header, 
     'rows' => $rows, 
     'attributes' => array('id' => array('job-listing')), 
     'sticky' => true, 
     'caption' => '', 
     'colgroups' => array(), 
     'empty' => t("No records found.") 
    ) 
).theme('pager'); 

    // Returning the output 
    return $output; 
} 

這結束了工作:

//get total records 
$num_rows = $query->countQuery()->execute()->fetchField(); 

// add paging and sorting 
$query = $query->extend('TableSort')->extend('PagerDefault')->limit(20); 

//execute again 
$result = $query->execute(); 

回答

0

根據文檔:https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_query/7

,以獲得行的總數最好是使用db_query()功能,因爲它有它返回的查詢總行數的方法rowCount()

<?php 
// Using the same query from above... 
$uid = 1; 
$result = db_query('SELECT n.nid, n.title, n.created 
FROM {node} n WHERE n.uid = :uid', array(':uid' => $uid)); 

// Fetch next row as a stdClass object. 
$record = $result->fetchObject(); 

// Fetch next row as an associative array. 
$record = $result->fetchAssoc(); 

// Fetch data from specific column from next row 
// Defaults to first column if not specified as argument 
$data = $result->fetchColumn(1); // Grabs the title from the next row 

// Retrieve all records into an indexed array of stdClass objects. 
$result->fetchAll(); 

// Retrieve all records as stdObjects into an associative array 
// keyed by the field in the result specified. 
// (in this example, the title of the node) 
$result->fetchAllAssoc('title'); 

// Retrieve a 2-column result set as an associative array of field 1 => field 2. 
$result->fetchAllKeyed(); 
// Also good to note that you can specify which two fields to use 
// by specifying the column numbers for each field 
$result->fetchAllKeyed(0,2); // would be nid => created 
$result->fetchAllKeyed(1,0); // would be title => nid 

// Retrieve a 1-column result set as one single array. 
$result->fetchCol(); 
// Column number can be specified otherwise defaults to first column 
$result->fetchCol($db_column_number); 

// Count the number of rows 
$result->rowCount(); 
?> 
+0

我不確定這會怎樣幫助我,儘管因爲我的查詢仍然有限制,所以如果我使用rowcount(),它只會給我任何我的限制。這聽起來像我仍然需要運行兩個單獨的查詢。一個用相同的標準獲得rowcount和另一個,但限制它,除非我錯過了一些東西。 –

+0

查看我的更新後的文章,我最終做了什麼。 –