WordPress.org

Ready to get started?Download WordPress

Forums

Query to db for list_table (2 posts)

  1. Madsex83
    Member
    Posted 1 year ago #

    Hi guys,

    i was very excited becouse i try to create my first plug-in.

    Oviusly i have a little problem.
    I want to create a table, wordpress style, with some data from a specific table of my db.

    Now i have install Custom List Table Example and i have reproduce it in my plugin.
    The table show itself well but i don't know how and where i can write my personal query to database and show results in table.

    I have see that in the middle of script there is a comment are that sugest me to write the query in that point, but i don't know why, results not appear. This is the cose, i think that it is correct:

    /***********************************************************************
             * ---------------------------------------------------------------------
             * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
             *
             * In a real-world situation, this is where you would place your query.
             *
             * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             * ---------------------------------------------------------------------
             **********************************************************************/
    
    		$data = $wpdb->get_results("SELECT* FROM utenti_registrati");
    		foreach($data as $element){
            $user_list = array(
                    'ID' => $element->id,
                    'title'     => $element->nome,
                    'rating'    => $element->email,
                    'director'  => $element->attivazione,
            );
            return $user_list;
    
    		}

    I include the comment are.

    Can you help me please? I'm stuck!

    Thank for all.

  2. bcworkz
    Member
    Posted 1 year ago #

    First, I don't know if this is a problem or not, but you should put a space between 'SELECT' and the '*'

    The real problem is your code is just returning the last row of data because you reassign a new value to $user_list in each loop of foreach. It looks like you are maybe trying to build a numeric indexed array of associative arrays. If so, you can get this returned by the $wpdb method directly simply by supplying an ARRAY_A argument after the query argument. No need to build the array with a foreach loop, just supply the ARRAY_A argument and return $data.

    Finally, some where before this, your function must declare global $wpdb;

Topic Closed

This topic has been closed to new replies.

About this Topic