-2

I use a webservice to get data instead of using mysql and I try to show results with a pagination:

function partenaire_search_partners(){
  $results = partenaire_ws_get_partners();
  foreach($results->Partenaire as $partenaire) {
    $rows[] = array($partenaire->raisonSociale);
  }

  $headers = array(
      array(
          'data' => t('Raison Sociale')
      )
  );
  $output = theme('table', array('header' => $headers, 'rows' => $rows)) . theme('pager');


  return $output;
}

I can restrieve my data, but either with 10 or 200 results I never see the pagination. Do you have some clues to help me resolve this issue ?

Thanks

1

2 Answers 2

2

Are you using Drupal 7 then copy paste this function after your partenaire_search_partners()

function pager_array_splice($data, $limit = 9, $element = 0) {
global $pager_page_array, $pager_total, $pager_total_items;
$page = isset($_GET['page']) ? $_GET['page'] : '';

// Convert comma-separated $page to an array, used by other functions.
$pager_page_array = explode(',', $page);

// We calculate the total of pages as ceil(items / limit).
$pager_total_items[$element] = count($data);
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element],   ((int)$pager_total[$element]) - 1));
return array_slice($data, $pager_page_array[$element] * $limit, $limit, TRUE);
}

and add this line to below of your foreach loop

  $paging =  pager_array_splice($rows, 10);

then use copy this line

   $output = theme('table', array('header' => $headers, 'rows' => $paging)) . theme('pager');

instead of this

     $output = theme('table', array('header' => $headers, 'rows' => $rows)) . theme('pager');
1
  • it gives 10 records in each page
    – Manu
    Dec 18, 2014 at 15:29
0
<?php


/*
* Implements hook_mennu()
*/
function test_menu() {
  $items['test'] = array(
    'title'             => t('Test'),
    'page callback'     => 'test_search_page',
    'access callback'   => True,
  );

  return $items;
}


function test_search_page(){
    $form = drupal_get_form('test_search_form');

    return $form;
}


function test_search_form($form, &$form_state){
  $header = array(t('id'), t('name'), t('firstname'));
  $rows = Null;
  $form['name'] = array(
    '#type'             => 'textfield',
    '#title'            => t('Name'),
    '#required'         => True,
    '#default_value'    => isset($_GET['name']) ? $_GET['name'] : Null
  );

  $form['submit'] = array(
    '#type'           => 'submit',
    '#value'          => t('submit'),
  );



  if (isset($_GET['name'])){
    $rows = get_data();
  }
  $form['table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('Aucun résultat.')
  );
  $form['pager'] = array('#markup' => theme('pager'));

  /*
  if (isset($form_state['table'])) {
    $form['table']  = $form_state['table'];
  }
  $form['pager'] = array('#markup' => theme('pager'));
  */
  return $form;
}

function test_search_form_submit($form, &$form_state){
   $form_state['redirect'] = array(
    // $path
    'test',
    // $options
    array('query' => array('name' => $form_state['values']['name'])),
    // $http_response_code
    302,
  );
}

//$header = array(t('id'), t('name'), t('firstname'));

function get_data(){
    $data =  array(
        0   => array(
            'id' => '0',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        1   => array(
            'id' => '1',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        2   => array(
            'id' => '2',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        3   => array(
            'id' => '3',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        4   => array(
            'id' => '4',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        5   => array(
            'id' => '5',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        6   => array(
            'id' => '6',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        7   => array(
            'id' => '7',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        8   => array(
            'id' => '8',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        9   => array(
            'id' => '9',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        10   => array(
            'id' => '10',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        ),
        11   => array(
            'id' => '11',
            'name'  => 'pokpokpok',
            'firstname' => 'pokpokpok',
        )
    );
    $paging = pager_array_splice($data, 2);

    return $paging;
}
/*
    $header = array(t('id'), t('name'), t('firstname'));

    $form_state['table'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $paging,
      '#empty' => t('Aucun r?sultat.')
    );

    $form_state['rebuild'] = True;*/


function pager_array_splice($data, $limit = 9, $element = 0) {
  global $pager_page_array, $pager_total, $pager_total_items;
  $page = isset($_GET['page']) ? $_GET['page'] : '';

  // Convert comma-separated $page to an array, used by other functions.
  $pager_page_array = explode(',', $page);

  // We calculate the total of pages as ceil(items / limit).
  $pager_total_items[$element] = count($data);
  $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  return array_slice($data, $pager_page_array[$element] * $limit, $limit, TRUE);
}

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.