• shadd

    (@shadd)


    Hi Guys
    I have created a custom table in my DB called user_courses. I want to get its contents and print it out in my theme. I know you can use have_posts(); the_post() etc to get data from the post table. But how can I do this with my custom table?

Viewing 10 replies - 1 through 10 (of 10 total)
  • MichaelH

    (@michaelh)

    Use wpdb

    $mydata = $wpdb->get_results($wpdb->prepare("SELECT * FROM mydatatable"));
    echo "<pre>"; print_r($mydata); echo "</pre>";  //just to see everything
    wilecoyote

    (@wilecoyote)

    Thread Starter shadd

    (@shadd)

    Thanks. Ok, I have just realised another problem which I dont know how to fix. The initial idea was to have 3 tables (user_courses, user_events, user_news) – I want a different page for each table. So for example:

    page?id=1 will load all the courses table blogs/comments
    page?id=2 will load all the event table blogs/comments
    and so on..

    Is this possible in WP ?

    MichaelH

    (@michaelh)

    Create a page template (then attach that Template when edit/writing a page) and in that page template use wpdb to get the data. Of course you’d need three pages and three templates (or code one template to handle the three conditions).

    Thread Starter shadd

    (@shadd)

    Thanks MichaelH, I will try it out and see how it goes. BTW, is there any encapulsation involved ? I come from a Java background and mixing model/view/controller logic is bad practise for the simple reason that it makes it a nightmare to manage/maintain.

    MichaelH

    (@michaelh)

    Not really. You will want to understand Template Hierarchy and Stepping Into Templates to make use of theme template files.

    Thread Starter shadd

    (@shadd)

    Thanks. Ok, considering the complexity of the project do you think it would be better to just use a framework like codeignitor and build from that ? WP would be good for one blog but three at once looks like quite a challenge.

    MichaelH

    (@michaelh)

    It’s hard to say–three custom tables doesn’t seem to be too much to handle but your concerns about ‘bad practise’ may lead you in a different direction.

    Thread Starter shadd

    (@shadd)

    Ok – The three tables will have comments, so I may have to mod the existing table or create a new one.
    BTW, do you know whether I can get rid of ‘posts’, ‘media’, ‘links’ menus/features from the admin CP ?

    MichaelH

    (@michaelh)

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘printing data from custom table’ is closed to new replies.