php - I want a pagination to my options page of wordpress plugin? -


my demo

i want pagination according results coming wordpress database...this done on options page of wordpress plugin..

my code retrieve database follows

$per_page=5; $sql = "select * wp_dive "; $result = $wpdb->get_results($sql_10) or die(mysql_error()); $length=count($result); $pages = ceil($length/$per_page); foreach( $result $results )       {     $id=$results->id;     $name= $results->name_cust;     $gender= $results->gender_cust;     $dob= $results->dob_cust;  <?php $html= "<div class=\"divcontentbody\">";?> <?php $html .= "<span class=\"clsorderno\">". $id."</span>";?> <?php $html .= "<span class=\"clsname\">". $name."</span>";?> <?php $html .= "<span class=\"clsgender\">".$gender."</span>";?> <?php $html .= "<span class=\"clsdob\">".  $dob ."</span>";?> <?php $html .= "</div>"?> <?php $data_html .=$html;   }   ?>  

i m getting data dynamically ..i want add pagination ..to show 5 entries on first page , accordingly ...

the native way of doing extending class wp_list_table , let wordpress handle specifics of table display. know plugin internal link check, kaiser. example bellow stripped version performs simple sql query.

first, need helper page (using php5.3+ anonymous functions):

add_action('admin_menu', function()  {     add_menu_page(         'te',          '<span style="color:#e57300;">table example</span>',          'edit_pages',          'table-example',          function() {              echo '<div class="wrap">';             screen_icon('edit');             echo '<h2>table example</h2>';             new b5f_wp_table();              echo '</div>';         },         'http://sstatic.net/stackexchange/img/favicon.ico',         1  // create before dashboard menu item     ); }); 

this end result:

custom wp table

and here class performs (note need of importing main class). you'll have adjust query , table columns data.

if( is_admin() && !class_exists( 'wp_list_table' ) )     require_once( abspath . 'wp-admin/includes/class-wp-list-table.php' );  class b5f_wp_table extends wp_list_table {     private $order;     private $orderby;     private $posts_per_page = 5;      public function __construct()     {         parent :: __construct( array(             'singular' => 'table example',             'plural'   => 'table examples',             'ajax'     => true         ) );         $this->set_order();         $this->set_orderby();         $this->prepare_items();         $this->display();     }      private function get_sql_results()     {         global $wpdb;         $args = array( 'id', 'post_title', 'post_date', 'post_content', 'post_type' );         $sql_select = implode( ', ', $args );         $sql_results = $wpdb->get_results("                 select $sql_select                     $wpdb->posts                 post_status = 'publish'                 order $this->orderby $this->order "         );         return $sql_results;     }      public function set_order()     {         $order = 'desc';         if ( isset( $_get['order'] ) , $_get['order'] )             $order = $_get['order'];         $this->order = esc_sql( $order );     }      public function set_orderby()     {         $orderby = 'post_date';         if ( isset( $_get['orderby'] ) , $_get['orderby'] )             $orderby = $_get['orderby'];         $this->orderby = esc_sql( $orderby );     }      /**      * @see wp_list_table::ajax_user_can()      */     public function ajax_user_can()      {         return current_user_can( 'edit_posts' );     }      /**      * @see wp_list_table::no_items()      */     public function no_items()      {         _e( 'no posts found.' );     }      /**      * @see wp_list_table::get_views()      */     public function get_views()     {         return array();     }       /**      * @see wp_list_table::get_columns()      */     public function get_columns()     {         $columns = array(             'id'         => __( 'id' ),             'post_title' => __( 'title' ),             'post_date'  => __( 'date' ),             'post_type'  => __( 'type' )         );         return $columns;             }      /**      * @see wp_list_table::get_sortable_columns()      */     public function get_sortable_columns()     {         $sortable = array(             'id'         => array( 'id', true ),             'post_title' => array( 'post_title', true ),             'post_date'  => array( 'post_date', true )         );         return $sortable;     }      /**      * prepare data display      * @see wp_list_table::prepare_items()      */     public function prepare_items()     {         $columns  = $this->get_columns();         $hidden   = array();         $sortable = $this->get_sortable_columns();         $this->_column_headers = array(              $columns,             $hidden,             $sortable          );          // sql results         $posts = $this->get_sql_results();         empty( $posts ) , $posts = array();          # >>>> pagination         $per_page     = $this->posts_per_page;         $current_page = $this->get_pagenum();         $total_items  = count( $posts );         $this->set_pagination_args( array (             'total_items' => $total_items,             'per_page'    => $per_page,             'total_pages' => ceil( $total_items / $per_page )         ) );         $last_post = $current_page * $per_page;         $first_post = $last_post - $per_page + 1;         $last_post > $total_items , $last_post = $total_items;          // setup range of keys/indizes contain          // posts on displayed page(d).         // flip keys values range outputs range in values.         $range = array_flip( range( $first_post - 1, $last_post - 1, 1 ) );          // filter out posts we're not displaying on current page.         $posts_array = array_intersect_key( $posts, $range );         # <<<< pagination          // prepare data         $permalink = __( 'edit:' );         foreach ( $posts_array $key => $post )         {             $link     = get_edit_post_link( $post->id );             $no_title = __( 'no title set' );             $title    = ! $post->post_title ? "<em>{$no_title}</em>" : $post->post_title;             $posts[ $key ]->post_title = "<a title='{$permalink} {$title}' href='{$link}'>{$title}</a>";         }         $this->items = $posts_array;     }      /**      * single column      */     public function column_default( $item, $column_name )     {         return $item->$column_name;     }      /**      * override of table nav avoid breaking bulk actions & according nonce field      */     public function display_tablenav( $which ) {         ?>         <div class="tablenav <?php echo esc_attr( $which ); ?>">             <!--              <div class="alignleft actions">                 <?php # $this->bulk_actions( $which ); ?>             </div>              -->             <?php             $this->extra_tablenav( $which );             $this->pagination( $which );             ?>             <br class="clear" />         </div>         <?php     }      /**      * disables views 'side' context there's not enough free space in ui      * displays them on screen/browser refresh. else we'd have via ajax db update.      *       * @see wp_list_table::extra_tablenav()      */     public function extra_tablenav( $which )     {         global $wp_meta_boxes;         $views = $this->get_views();         if ( empty( $views ) )             return;          $this->views();     } } 

helper plugin style admin: wordpress admin style, bueltge


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -