Title: Custom table plugin
Last modified: September 1, 2016

---

# Custom table plugin

 *  [marieliese](https://wordpress.org/support/users/marieliese/)
 * (@marieliese)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/custom-table-plugin/)
 * hi, i’m having a trouble in calling a data from my database to be viewed in my
   admin menu, for now. my data are static
    Is there someone can help me to make
   it dynamic please. Here is my code:
 * <?php
 * if(is_admin())
    { new Marz_Wp_List_Table(); }
 * /**
    * Marz_Wp_List_Table class will create the page to load the table */ class
   Marz_Wp_List_Table { /** * Constructor will create the menu item */ public function
   __construct() { add_action( ‘admin_menu’, array($this, ‘add_menu_example_list_table_page’));}
 *  /**
    * Menu item will allow us to load the page to display the table */ public
   function add_menu_example_list_table_page() { add_menu_page( ‘Agent Users’, ‘
   Agent Users’, ‘manage_options’, ‘example-list-table.php’, array($this, ‘list_table_page’));}
 *  /**
    * Display the list table page * * [@return](https://wordpress.org/support/users/return/)
   Void */ public function list_table_page() { $exampleListTable = new Example_List_Table();
   $exampleListTable->prepare_items(); ?> <div class=”wrap”> <div id=”icon-users”
   class=”icon32″></div> <h2>Agent Users</h2> <?php $exampleListTable->display();?
   > </div> <?php } }
 * // WP_List_Table is not loaded automatically so we need to load it in our application
   
   if( ! class_exists( ‘WP_List_Table’ ) ) { require_once( ABSPATH . ‘wp-admin/includes/
   class-wp-list-table.php’ ); }
 * /**
    * Create a new table class that will extend the WP_List_Table */ class Example_List_Table
   extends WP_List_Table { /** * Prepare the items for the table to process * * 
   [@return](https://wordpress.org/support/users/return/) Void */ public function
   prepare_items() { $columns = $this->get_columns(); $hidden = $this->get_hidden_columns();
   $sortable = $this->get_sortable_columns();
 *  $data = $this->table_data();
    usort( $data, array( &$this, ‘sort_data’ ) );
 *  $perPage = 20;
    $currentPage = $this->get_pagenum(); $totalItems = count($data);
 *  $this->set_pagination_args( array(
    ‘total_items’ => $totalItems, ‘per_page’
   => $perPage ) );
 *  $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
 *  $this->_column_headers = array($columns, $hidden, $sortable);
    $this->items 
   = $data; }
 *  /**
    * Override the parent columns method. Defines the columns to use in your
   listing table * * [@return](https://wordpress.org/support/users/return/) Array*/
   public function get_columns() { $columns = array( ‘id’ => ‘ID’, ‘name’ => ‘Name’,’
   email’ => ‘Email’, ‘role’ => ‘Role’,
 *  );
 *  return $columns;
    }
 *  /**
    * Define which columns are hidden * * [@return](https://wordpress.org/support/users/return/)
   Array */ public function get_hidden_columns() { return array(); }
 *  /**
    * Define the sortable columns * * [@return](https://wordpress.org/support/users/return/)
   Array */ public function get_sortable_columns() { return array(‘name’ => array(‘
   name’, false)); }
 *  /**
    * Get the table data * * [@return](https://wordpress.org/support/users/return/)
   Array */ private function table_data() { $data = array();
 *  $data[] = array(
    ‘id’ => 1, ‘name’ => ‘Ana’, ’email’ => ‘ana@empassion.com.
   au’, ‘role’ => ‘agent’ );
 *  $data[] = array(
    ‘id’ => 2, ‘name’ => ‘Jonalyn’, ’email’ => ‘jona@empassion.
   com.au’, ‘role’ => ‘agent’ );
 *  $data[] = array(
    ‘id’ => 3, ‘name’ => ‘Customer Service Agent’, ’email’ => ‘
   test1@empassion.com.au’, ‘role’ => ‘agent’ );
 *  $data[] = array(
    ‘id’ => 4, ‘name’ => ‘Isla’, ’email’ => ‘warehouse@empassion.
   com.au’, ‘role’ => ‘agent’ );
 *  $data[] = array(
    ‘id’ => 5, ‘name’ => ‘Kate’, ’email’ => ‘kate@empassion.com.
   au’, ‘role’ => ‘agent’ );
 *  return $data;
    }
 *  /**
    * Define what data to show on each column of the table * * [@param](https://wordpress.org/support/users/param/)
   Array $item Data * [@param](https://wordpress.org/support/users/param/) String
   $column_name – Current column name * * [@return](https://wordpress.org/support/users/return/)
   Mixed */ public function column_default( $item, $column_name ) { switch( $column_name){
   case ‘id’: case ‘name’: case ’email’: case ‘role’: return $item[ $column_name];
 *  default:
    return print_r( $item, true ) ; } }
 *  /**
    * Allows you to sort the data by the variables set in the $_GET * * [@return](https://wordpress.org/support/users/return/)
   Mixed */ private function sort_data( $a, $b ) { // Set defaults $orderby = ‘name’;
   $order = ‘asc’;
 *  // If orderby is set, use this as the sort column
    if(!empty($_GET[‘orderby’])){
   $orderby = $_GET[‘orderby’]; }
 *  // If order is set use this as the order
    if(!empty($_GET[‘order’])) { $order
   = $_GET[‘order’]; }
 *  $result = strnatcmp( $a[$orderby], $b[$orderby] );
 *  if($order === ‘asc’)
    { return $result; }
 *  return -$result;
    } } ?>
 * and i want it to call the query SELECT SQL_CALC_FOUND_ROWS `ID`, `user_nicename`,`
   user_email`, `display_name` FROM wp_pprgu2_users WHERE user_nicename LIKE ‘cs%’;
 * Thanks for the help in advance.
 * [https://wordpress.org/plugins/custom-database-tables/](https://wordpress.org/plugins/custom-database-tables/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [ka2](https://wordpress.org/support/users/ka2/)
 * (@ka2)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/custom-table-plugin/#post-8137180)
 * Thank you for your inquiry.
 * If you use the “Custom DataBase Tables” plugin, you should use the built-in method
   that plugin.
    For example, it’s as follows:
 *     ```
       <?php
       global $cdbt;
       $sql = "SELECT SQL_CALC_FOUND_ROWS ID,user_nicename,user_email,display_name FROM wp_pprgu2_users WHERE user_nicename LIKE 'cs%'";
       // Matchies rows as integer
       $rows = $cdbt->run_query( $sql );
       // Matchies row data as array
       $data = $cdbt->run_query( $sql, 'PDO' );
       ```
   
 * Notice: if you use the “run_query” method, you cannot get number of rows with
   issuing the “SELECT FOUND_ROWS()” query after using “SQL_CALC_FOUND_ROWS” query.
 * Thank you,
    -  This reply was modified 9 years, 8 months ago by [ka2](https://wordpress.org/support/users/ka2/).

Viewing 1 replies (of 1 total)

The topic ‘Custom table plugin’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-database-tables_e4e2e2.svg)
 * [Custom DataBase Tables](https://wordpress.org/plugins/custom-database-tables/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-database-tables/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-database-tables/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-database-tables/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-database-tables/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-database-tables/reviews/)

## Tags

 * [custom plugin](https://wordpress.org/support/topic-tag/custom-plugin/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)
 * [tables](https://wordpress.org/support/topic-tag/tables/)

 * 1 reply
 * 2 participants
 * Last reply from: [ka2](https://wordpress.org/support/users/ka2/)
 * Last activity: [9 years, 8 months ago](https://wordpress.org/support/topic/custom-table-plugin/#post-8137180)
 * Status: not resolved