WordPress.org

Ready to get started?Download WordPress

Forums

PHP $_GET within WordPress (4 posts)

  1. efield
    Member
    Posted 2 years ago #

    I am trying to display information (a member directory) from a table within the wordpress database (Families) within a WordPress site. The objective is to list all the families (master) with a clickable link to the details page for family details. I thought the quick and dirty way would be to simply import my functioning PHP page into template pages and change the code to point to the WordPress generated page_id=631?record_ID=<?php echo$row_RecordsetFamily['FamilyID']; ?>. The families page correctly displays the list of all the families with links. The family details page comes up with the text on the details page. However, no database table information is displayed. Does WordPress have issues with URL-passed recordIDs? Can you suggest a work-around? I've dabbled with WPDB but that was not passing the recordID either. Any help would be most appreciated.

    EF

  2. Big Bagel
    Member
    Posted 2 years ago #

    The best way to pull information from a custom table in WordPress' database is to use wpdb to run an SQL query. Here's the codex page on wpdb; might be useful:

    http://codex.wordpress.org/Class_Reference/wpdb

    Here's a quick example; I'm using "families" as the name of the table and querying for all the information stored in two columns (named "name" and "details_url") with the results ordered by "name":

    <?php
    global $wpdb;
    
    $tablename = $wpdb->prefix . 'families';
    
    $sql = "SELECT name, details_url
    FROM $tablename
    ORDER BY $tablename.name DESC";
    
    $family_list = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );

    I can't put backticks in my post cause the forums would screw up my code block, but there should be backticks around "name" and "details_url" in the sql string.

    $family_list now contains an array filled with those two columns. To list everything you could do something like:

    foreach( $family_list as $family ) {
        echo '<a href="' . $family['details_url'] . '">' . $family['name'] . '</a>';
    }
  3. efield
    Member
    Posted 2 years ago #

    Thanks so much for the direction. I've tried the wpdb function but I'm not able to retrieve any data from my custom tables. I AM able to retrieve data from the WP installed tables (Posts, Users, etc.). Do I have to register custom tables at all instead of just dropping them in the same database and calling on them? Just wondering what is different. Thanks again for your help!!!

  4. Big Bagel
    Member
    Posted 2 years ago #

    The way I've always troubleshot $wpdb is to setup an email that sends me the details when it has a problem:

    $family_list = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
    if ( ! $family_list ) {
        wp_mail( 'your@email.com', 'ERROR', 'wpdb broke here -> ' . $wpdb->last_error );
    }

    That should show you what it's having a problem with. SQL queries can be difficult to get just right without a little troubleshooting.

Topic Closed

This topic has been closed to new replies.

About this Topic