Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display database info into html table Wordpress

I have the table called: persona, in my database. I just need to retrieved the data in this table into a html table inside a page in wordpress. SO far this is what i have:

<table border="1">
<tr>
 <th>Firstname</th>
 <th>Lastname</th>
 <th>Points</th>
</tr>
<tr>
  <?php
    global $wpdb;
    $result = $wpdb->get_results ( "SELECT * FROM persona" );
    foreach ( $result as $print )   {
        echo '<td>' $print->ID_per.'</td>';
        }
  ?>
</tr>               

i added in the specific page i am working on and publish it but when i refresh the page it only shows the code printed in the page. I wonder if i am puting the code in the right place or i do not know where to put it.

Look at the images below: enter image description here

like image 537
Alejo_Blue Avatar asked Aug 31 '25 06:08

Alejo_Blue


1 Answers

Given your situation, the simplest, best way to do this would be to add a shortcode to your theme.

If you add this code to your theme's functions.php file, you will then be able to display the information wherever you like by adding [persona-table] to any page or post.

// add the shortcode [persona-table], tell WP which function to call
add_shortcode( 'persona-table', 'persona_table_shortcode' );

// this function generates the shortcode output
function persona_table_shortcode( $args ) {
    global $wpdb;
    // Shortcodes RETURN content, so store in a variable to return
    $content = '<table>';
    $content .= '</tr><th>Firstname</th><th>Lastname</th><th>Points</th></tr>';
    $results = $wpdb->get_results( ' SELECT * FROM persona' );
    foreach ( $results AS $row ) {
        $content = '<tr>';
        // Modify these to match the database structure
        $content .= '<td>' . $row->firstname . '</td>';
        $content .= '<td>' . $row->lastname . '</td>';
        $content .= '<td>' . $row->ID_per . '</td>';
        $content .= '</tr>';
    }
    $content .= '</table>';

    // return the table
    return $content;
}
like image 105
random_user_name Avatar answered Sep 02 '25 21:09

random_user_name