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:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With