0

I am trying to paginate the results from the table, I saw some examples but those make use of db_select, I chose db_query. Is there a way to get the paging with db_query? I have been trying to translate the code into db_select but no luck, support please! Thanks! Here is my code!

function rss_list_form($form, $form_state) {
  $header = array(
    'article' => t('Article'),
  );

  $sql = "SELECT * FROM {rss_table}";
  $result = db_query($sql);

  $rows = array();
  foreach ($result as $dbrow) {
    $rows[$dbrow->GUID] = array(
      'article' => '<h3>'.$dbrow->title . '</h3>' . '<br><p>' . strip_tags($dbrow->description, '<p><b><a><img><br>') . '<p><br>' . '<b>' . $dbrow->date . '</b><br><em>' . $dbrow->link . '</em><br>',
    );
  }

  $form['rss_select'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => t('No articles found.'),
  );

  $form['import'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
    '#weight' => 0,
    '#submit'=> array('rss_list_form_import'),
  );

  $form['update'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
    '#weight' => 1,
    '#submit'=> array('rss_list_form_update'),
  );

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

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

  return $form;

}

0

1 Answer 1

1

The better way to use db_select with called as query extender.

Here is an example

$query = db_select('rss_table')
  ->extend('PagerDefault')
  ->limit(5);

Then theme_pager() can be used for outputting a pager.

For more detailed example check out

Either way to use pagination

 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);
}




 $query = db_select('rss_table','t');
            $query->fields('t');

           $resultget=$query->execute();

           $result = $resultget->fetchAll();
              $limit=5;
              $output = pager_array_splice($result, $limit); 
              $html='';

   foreach ($output as $dbrow) {

                    $items[]=(array)$dbrow;
                    $title = $dbrow->title;
                    $description = $dbrow->description;

$html.="         //HTML code here//       ";



 }


  $output1 = theme('item_list', array(
    'items' => $items));

    echo $html;

You can use it as in the form of table too or append it to the form as well. This is the way to use pagination.

Use this code to modify as per desired output.

Hope it works for you!

0

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.