0

I'm very new to WordPress; I was wondering if someone would be able to let me know how I can add one of my database tables in a table format in one of my pages in WordPress; I did a research and I found this: http://www.youtube.com/watch?v=hb3lfwq1bfM but I don't know whether "wpData Tables" is a plugin that I need to install or...? I don't have that option in my wordpress!

If you need more clarification please let me know.

If someone can show me the right direction, I appreciate it :)

Thanks

1 Answer 1

1

In your page.php add this code within the loop at required area.

Replace values of $my_page_id, $table_name and add columns as per your requirement.

<?php
$my_page_id = 111; // page ID here
if( get_the_ID() === $my_page_id) { 

global $wpdb;
$table_name = 'your_table_name_here';
$myrows = $wpdb->get_results( "SELECT column_1, column_2, column_3 FROM ". $table_name); // add columns as you want
?>
<table>
    <thead>
        <tr>
          <th>Column 1</th>
          <th>Column 2</th>
          <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ( $myrows as $myrow ) {  ?>
        <tr>
          <td><?php echo $myrow->column_1; ?></td>
          <td><?php echo $myrow->column_2; ?></td>
          <td><?php echo $myrow->column_3; ?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>
<?php
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Omair, Sorry if I ask a silly question; but where is this page.php? Is it exactly the same name? Sorry I am pretty new to WP!Assume you create a new project where does page.php go? Thanks
page.php is at: /wp-content/themes/{theme-name}/page.php at your web server. Here is how you can find out where is page ID: wpbeginner.com/beginners-guide/…. I hope you know the database table name and column names that you want to retrieve :)
YEa I know :D But is it something like PHP or ... that we need to pass database configuration like host, password ,DBname? If so where am I supposed to pass these configs? You are very helpful man. Thanks :)
Is the database table not in Wordpress database? Is it remote and somewhere else?
I got it! It is cool :D My first experience with WP! Thanks for your patience:)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.