1

I am using pager_query and theme_pager function as below.But unfortunately the pager is not getting displayed.Please help.

function advanced_product_search() {
    $output = "";

    $sectors = $material_types = $resource_types = array();
    $where_array = array();
    $node_data = array();
    $product_query = "SELECT n.* FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid ";
    $product_count_query = "SELECT COUNT(DISTINCT(n.nid)) FROM {node} n 
        INNER JOIN {term_node} tn ON n.nid = tn.nid ";

    $product_query .= " WHERE n.type = 'product' ";
     if(isset($_POST['keyword']) && !empty($_POST['keyword'])){
        $key_word = check_plain($_POST['keyword']);
        $product_query.= " AND n.title LIKE '%$key_word%' ";
        $product_count_query.=" AND n.title LIKE '%$key_word%' ";
    }

    if(!empty($_POST['sector'])){
      $sectors = $_POST['sector'];
    }

    if(!empty($_POST['material_type'])){
      $material_types = $_POST['material_type'];
    }

    if(!empty($_POST['resource_type'])){
      $resource_types = $_POST['resource_type'];
    }

    $terms = array_merge($sectors ,$material_types ,$resource_types);
    if(!empty($terms)){
      $count = 1;
      $terms_list = "";
      foreach($terms as $tid){
        if($count < count($terms)){
          $terms_list.="$tid,";
        }else{
          $terms_list.="$tid";
        }
        $count++;
      }
      $product_query.=" AND tn.tid IN ($terms_list)";
      $product_count_query.=" AND tn.tid IN ($terms_list)";
    }

    $product_query.=" GROUP BY n.nid ORDER BY n.changed DESC ";
    $product_count_query.=" GROUP BY n.nid ORDER BY n.changed DESC ";

    $product_array = array();
    $product_query_result = pager_query($product_query, 5, 0, $product_count_query);
    while ($node = db_fetch_object($product_query_result)) {
      $node_data = node_load($node->nid);

      if($_POST['accredited'] == 'on'){
        if(empty($node_data->field_right_way_accredited[0]['value']) || !$node_data->field_right_way_accredited[0]['value']){
          continue;
        }
      }

      $product_array[] = array(
        'nid'=>$node_data->nid,
        'title'=>$node_data->title,
        'price'=>$node_data->sell_price,
        'image'=>$node_data->field_image_cache[0]['filepath'],
        'url'=> $node_data->path
      );
    }

    $output .= theme('ssa_product_search_template', $product_array);
    $output .= theme('pager', NULL, 5, 0);
    return $output;
}
2
  • Check $product_count_query is not returning 0, or the pager will think there is no items to show.
    – avpaderno
    Commented Jan 26, 2015 at 9:45
  • Please write an answer when you will be allowed to write one. If it was not merely a typo, the question could be helpful for future users.
    – avpaderno
    Commented Jan 26, 2015 at 10:26

1 Answer 1

0

Solved this problem by correcting the fourth argument of pager_query function i.e $product_count_query.

Pager query takes the fourth argument as an SQL query to count the number of matching records.It should a valid sql query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.