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!
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>';
$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