1

I try to make a plugin that can show a table (with rows) in wordpress by using the WP_List_Table class.

In my "students" table Ive got

Fname,Lname,school,Class

I've already found a example where they use arrays and typing manual everything to the table, which then looks like this

var $example_data = array(


array( 'ID' => 1,'booktitle' => 'Quarter Share', 'author' => 'Nathan Lowell', 
               'isbn' => '978-0982514542' ),
        array( 'ID' => 2, 'booktitle' => '7th Son: Descent','author' => ' Hutchins',
               'isbn' => '0312384378' ),
        array( 'ID' => 3, 'booktitle' => 'Shadowmagic', 'author' => 'John Lenahan',
               'isbn' => '978-1905548927' ),
        array( 'ID' => 4, 'booktitle' => 'The Crown Conspiracy', 'author' => 'Michael',
               'isbn' => '978-0979621130' ),
        array( 'ID' => 5, 'booktitle'     => 'Max', 'author' => 'Mark Jeffrey',
               'isbn' => '978-0061988929' ),
        array('ID' => 6, 'booktitle' => 'Jack Wakes Up: A Novel', 'author' => ' Har',
              'isbn' => '978-0307454355' )
    );

So I was wondering about is it possible to make an maybe more arrays where I can type Select * From table name? Or something like that?

3 Answers 3

1

I think you should rethink if creating a custom wp_list_table & stuff is the way to go.

I can tell from experience wp_list_table isn't fun to work with.

Besides I think (as far as I can judge here) a custom post type (or multiple) would be the way to go for you. This way you probably don't need queries.

1

You can make use of $wpdb to query database for arbitrary data via SQL.

I would be hesitant about this approach, as per other answer WP_List_Table is one of more questionable APIs. :) But if you get a start on data retrieval you can always reuse it in different context.

0

I´ve found a solution !

I´ve tried this and it´s working

private function table_data()
    {
        $data = array();


                $sqlget = "SELECT * FROM elever";
        $sqldata = mysql_query($sqlget) or die('error getting');
        while($row = mysql_fetch_array($sqldata, MYSQL_ASSOC)){

        $data[] = array(
                    'id'          => $row['id'],
                    'fornavn'       => $row['fornavn'],
                    'efternavn' => $row['efternavn'],
                    'skole'        => $row['skole'],
                    'klasse'    => $row['klasse'],
                    );

        }        


        return $data;
    }

It´s much easier than I´ve expected. Just SELECT * FROM elever and then explain where Im going to place the $row´s inside the array

4
  • Sorry, but I have to down vote this solution. Notice that mysql_query(), it's deprecated as of PHP 5.5. Why don't you use $wpdb to query the database as suggested by @Rarst? Please note the following statement from the Codex on WP_List_Table: "This class's access is marked as private. That means it is not intended for use by plugin and theme developers as it is subject to change without warning in any future WordPress release."
    – birgire
    Jun 23, 2014 at 0:42
  • @birgire to be fair it was "private" so long that no one longer gives a damn, people just use it... :)
    – Rarst
    Jun 23, 2014 at 9:47
  • @Rarst, sure, I must admit I've used the class myself and ignored this notice as well ;-) But my main concern was the mysql_query() usage instead of $wpdb, but I will upvote the answer when the OP has updated it. I wonder how many plugins use the WP_List_Table class? I tried to search for WP_List_Table site:plugins.svn.wordpress.org but it didn't give any result. Maybe these pages are not all indexed?
    – birgire
    Jun 23, 2014 at 11:18
  • I'll upvote this solutions because sometimes get_results is using TOO much memory to proceed a select query on a big table, plus if you'll ask get_results to return ARRAY_A it will increase memory usage twice.
    – Ignat B.
    Oct 28, 2014 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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