php - Display Data in Table from External Database in WP using Shortcodes

admin2025-06-02  3

I have successfully coded a secondary database connection for my wordpress site in the functions.php file in wordpress. I have used the following to create shortcode and run the data to my table where I can display it anywhere on my site by pasting in the shortcode. I am getting the following error on line 313 and I do not understand what it is telling me to fix.

Parse error: syntax error, unexpected 'Beginning' (T_STRING) in /home/g3boat5/genba.g3boatco.info/wp-content/themes/twentynineteen/functions.php on line 313

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

// this function generates the shortcode output
function pontoon_table_shortcode( $args ) {
    global $wpdb;
    // Shortcodes RETURN content, so store in a variable to return
    $content = '<table>';
    $content .= '</tr><th>Week Beginning</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th><th>Weekly Total</th><th>Previosu Week Total</th></tr>';
    $results = $wpdb->get_results( ' SELECT * FROM wpdb_PontoonBoats_LineOne_2Log' );
    foreach ( $results AS $row ) {
        $content = '<tr>';
        // Modify these to match the database structure
        $content .= '<td>' . $row->Week Beginning . '</td>';
        $content .= '<td>' . $row->Monday . '</td>';
        $content .= '<td>' . $row->Tuesday . '</td>';
        $content .= '<td>' . $row->Wednesday . '</td>';
        $content .= '<td>' . $row->Thursday . '</td>';
        $content .= '<td>' . $row->Friday . '</td>';
        $content .= '<td>' . $row->Saturday . '</td>';
        $content .= '<td>' . $row->Sunday . '</td>';
        $content .= '<td>' . $row->Weekly Total . '</td>';
        $content .= '<td>' . $row->Previous Week Totals . '</td>';
        $content .= '</tr>';
    }
    $content .= '</table>';

    // return the table
    return $content;
}

Line 313 is this one

$content .= '<td>' . $row->Week Beginning . '</td>';

I am not sure what I have done wrong to this code to make it not work for me. Can someone please help me understand what needs to be fixed? Thank you!

I have successfully coded a secondary database connection for my wordpress site in the functions.php file in wordpress. I have used the following to create shortcode and run the data to my table where I can display it anywhere on my site by pasting in the shortcode. I am getting the following error on line 313 and I do not understand what it is telling me to fix.

Parse error: syntax error, unexpected 'Beginning' (T_STRING) in /home/g3boat5/genba.g3boatco.info/wp-content/themes/twentynineteen/functions.php on line 313

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

// this function generates the shortcode output
function pontoon_table_shortcode( $args ) {
    global $wpdb;
    // Shortcodes RETURN content, so store in a variable to return
    $content = '<table>';
    $content .= '</tr><th>Week Beginning</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th><th>Weekly Total</th><th>Previosu Week Total</th></tr>';
    $results = $wpdb->get_results( ' SELECT * FROM wpdb_PontoonBoats_LineOne_2Log' );
    foreach ( $results AS $row ) {
        $content = '<tr>';
        // Modify these to match the database structure
        $content .= '<td>' . $row->Week Beginning . '</td>';
        $content .= '<td>' . $row->Monday . '</td>';
        $content .= '<td>' . $row->Tuesday . '</td>';
        $content .= '<td>' . $row->Wednesday . '</td>';
        $content .= '<td>' . $row->Thursday . '</td>';
        $content .= '<td>' . $row->Friday . '</td>';
        $content .= '<td>' . $row->Saturday . '</td>';
        $content .= '<td>' . $row->Sunday . '</td>';
        $content .= '<td>' . $row->Weekly Total . '</td>';
        $content .= '<td>' . $row->Previous Week Totals . '</td>';
        $content .= '</tr>';
    }
    $content .= '</table>';

    // return the table
    return $content;
}

Line 313 is this one

$content .= '<td>' . $row->Week Beginning . '</td>';

I am not sure what I have done wrong to this code to make it not work for me. Can someone please help me understand what needs to be fixed? Thank you!

Share Improve this question asked Feb 21, 2019 at 16:29 LellaLella 451 silver badge8 bronze badges 5
  • use $object->{'Property Name'} as described in my answer below. Please remember to accept the answer if this resolves your issue so others know this is no longer an open issue :) – sMyles Commented Feb 21, 2019 at 17:41
  • HI sMyles, I did use it and after adjusting the $wpdb to $seconddb, I did successfully connect to the database, it did return my data on the page, but it did not display it in a table. It simply displayed it in a string with no formatting. Can you see what I have done wrong? – Lella Commented Feb 21, 2019 at 18:59
  • so this sounds like some other issue you're having (separate from this one). Please open a new question for that and comment here and i will take a look at it for you. Please provide a screenshot or something so we can see what is being output (or at least an example of it), and please make sure to include in the question the updated code you're using, and details as to what the problem is. – sMyles Commented Feb 21, 2019 at 19:38
  • Ahhh as I said that I realized where the issue is, check my updated answer. Looks like you have ` $content = '<tr>';` when it should be ` $content .= '<tr>';` – sMyles Commented Feb 21, 2019 at 19:41
  • That took care of it. Thank you! I could not see that . was missing. I have marked your answer as complete. Appreciate it very much. – Lella Commented Feb 21, 2019 at 19:59
Add a comment  | 

1 Answer 1

Reset to default 2

While it is VERY bad practice to have spaces in the object names, there are times where it may be out of your control. You can use the syntax of $object->{'Property Name'}, like this:

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

// this function generates the shortcode output
function pontoon_table_shortcode( $args ) {

    global $wpdb;
    // Shortcodes RETURN content, so store in a variable to return
    $content = '<table>';
    $content .= '</tr><th>Week Beginning</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th><th>Weekly Total</th><th>Previosu Week Total</th></tr>';
    $results = $wpdb->get_results( ' SELECT * FROM wpdb_PontoonBoats_LineOne_2Log' );
    foreach ( $results AS $row ) {
        $content .= '<tr>';
        // Modify these to match the database structure
        $content .= '<td>' . $row->{'Week Beginning'} . '</td>';
        $content .= '<td>' . $row->Monday . '</td>';
        $content .= '<td>' . $row->Tuesday . '</td>';
        $content .= '<td>' . $row->Wednesday . '</td>';
        $content .= '<td>' . $row->Thursday . '</td>';
        $content .= '<td>' . $row->Friday . '</td>';
        $content .= '<td>' . $row->Saturday . '</td>';
        $content .= '<td>' . $row->Sunday . '</td>';
        $content .= '<td>' . $row->{'Weekly Total'} . '</td>';
        $content .= '<td>' . $row->{'Previous Week Totals'} . '</td>';
        $content .= '</tr>';
    }
    $content .= '</table>';

    // return the table
    return $content;
}

UPDATE:

It looks like you had $content = '<tr>'; which resets the string instead of appending to it, changed that to $content .= '<tr>';

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748879323a314482.html

最新回复(0)