1

I am a newbie i wordpress dev. So i wanna display a results, taken from custom wordpress database table. So I have written this:

    if( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Q_List_Table extends WP_List_Table {
    public function __construct() {
        parent::__construct( array(
            'singular' => 'test',
            'plural' => 'tests',
            'ajax' => false
        ));
        $this->prepare_items();
        $this->display();
    }

    function get_columns() {
        $columns = array(
            'id'    => 'ID',
            'subject'     => 'Title',
            'name'   => 'Username',
            'date'   => 'Posted on'
        );
        return $columns;
    }

    function column_default( $item, $column_name ) {
        switch( $column_name ) {
            case 'id':
            case 'subject':
            case 'name':
            case 'date':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;
        }
    }

    function prepare_items() {

        global $wpdb;

        $sql = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}fenq_questions"));
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);


        var_dump($sql);


        $this->items = $sql;
    }
}


class FenQuestingAdmin{



    function display(){
        error_reporting(E_ALL);
        //admin all questions template
        ?>

        <div class="wrap">

         <h2><?php _e('FenQuesting Settings','fenquesting-slug'); ?></h2>

        <?php 
           new Q_List_Table();
        ?>

        </div>        
        <?php
    }

}

The display function from FenQuestingAdmin class is linked to the main file and set as a function for admin page...

So this code gives this result:

Results

When I am using an example array, constructed by me, I am getting 0 problems, but the main aim is to display the array, taken from db.

Thanx for your help!

P.S. I have tried to change all objects to arrays, this is not changing the situation.

1
$data = $wpdb->get_results($sql, ARRAY_A);

Adding ARRAY_A forces get_results() to return an associative array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.