• Hi,
    I am adding a custom table data in admin panel of my plugin and wanted pagination for it.

    Everything is working fine except the pagination links.
    After clicking on other pages of pagination i am still on page on itself. Below is my code:

    global $wpdb;
    global $wp_rewrite;
    $rows_per_page = 5;
    $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
    $results = $wpdb->get_results( "SELECT * FROM custom_table WHERE col1 != 0" );
     $pagination_args = array(
     'base' => @add_query_arg('paged','%#%'),
     'format' => '',
     'total' => ceil(sizeof($results)/$rows_per_page),
     'current' => $current,
     'show_all' => false,
     'type' => 'plain'
    );
    
    if( !empty($wp_query->query_vars['s']) )
     $pagination_args['add_args'] = array('s'=>get_query_var('s'));
    
    echo '<strong>Pages</strong>'.paginate_links($pagination_args);
    
    $start = ($current - 1) * $rows_per_page;
    $end = $start + $rows_per_page;
    $end = (sizeof($results) < $end) ? sizeof($results) : $end;
    ?>
     <table>
      <tr>
        <td>No</td>
        <td>Name</td>
        <td>Emails</td>
        <td>Address</td>
        <td>Phone</td>
      </tr>
     <?php
    $result = $wpdb->get_results( "SELECT * FROM custom_table WHERE col1 != 0 LIMIT $start, $rows_per_page" );
    for ($i=$start;$i < $end ;++$i ) {
     $row = $result[$i];
      <tr>
    <td align="center" class="countcol"></td>
    <td><?php echo $row->name; ?></td>
    <td><?php echo $row->email; ?></td>
    <td><?php echo $row->address; ?></td>
    <td><?php echo $row->phone; ?></td>
    <?php  } ?>  </tr>
    </table>

    I tried everything but not able to find the reason.

Viewing 1 replies (of 1 total)
  • Thread Starter Ajay Tiwari

    (@mmajay)

    Solved my problem by my own:

    global $wpdb;
    global $wp_rewrite;
    $rows_per_page = 5;
    $current = isset($_GET['paged']) ? $_GET['paged'] : 1;
    $results = $wpdb->get_results( "SELECT * FROM custom_table WHERE col1 != 0" );
     $pagination_args = array(
     'base' => @add_query_arg('paged','%#%'),
     'format' => '',
     'total' => ceil(sizeof($results)/$rows_per_page),
     'current' => $current,
     'show_all' => false,
     'type' => 'plain'
    );
    
    if( !empty($wp_query->query_vars['s']) )
     $pagination_args['add_args'] = array('s'=>get_query_var('s'));
    
    echo '<strong>Pages</strong>'.paginate_links($pagination_args);
    
    $start = ($current - 1) * $rows_per_page;
    $end = $start + $rows_per_page;
    $end = (sizeof($results) < $end) ? sizeof($results) : $end;
    ?>
     <table>
      <tr>
        <td>No</td>
        <td>Name</td>
        <td>Emails</td>
        <td>Address</td>
        <td>Phone</td>
      </tr>
     <?php
    $result = $wpdb->get_results( "SELECT * FROM custom_table WHERE col1 != 0 LIMIT $start, $rows_per_page" );
    for ($i=$start;$i < $end ;++$i ) {
     $row = $result[$i];
    $class_row = ($i % 2 == 1 ) ? ' class="alternate"' : '';
      <tr ' . $class_row . '>
    <td align="center" class="countcol"></td>
    <td><?php echo $row->name; ?></td>
    <td><?php echo $row->email; ?></td>
    <td><?php echo $row->address; ?></td>
    <td><?php echo $row->phone; ?></td>
    <?php  } ?>  </tr>
    </table>

    Now everything works properly for me..

Viewing 1 replies (of 1 total)
  • The topic ‘Pagination Links not working for custom table in admin panel’ is closed to new replies.