$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>shortcode - Code is providing a row of data but not formatting it as table|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

shortcode - Code is providing a row of data but not formatting it as table

matteradmin10PV0评论

I am using the following code:

function seconddb() {
    global $seconddb;
    $seconddb = new wpdb('username','password','g3boat5_G3_Genba','localhost');
}
add_action('init','seconddb');
// add the shortcode [pontoon-table], tell WP which function to call
add_shortcode( 'pontoon-table', 'pontoon_table_shortcode' );

// 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 $seconddb;
    // 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>Previous Week Totals</th></tr>';
    $results = $seconddb->get_results( ' SELECT * FROM seconddb_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;
}

So that is the correct data being pulled from the database but as you can see it is not being displayed in the table as coded. What is my next step to try and get this to render in a nicely constructed table?

Furthermore...if someone knows, can you explain how I can put a place on this page to to select a date and then the data pulled from the database be from that date?

I am trying to do everything in shortcode.

I am using the following code:

function seconddb() {
    global $seconddb;
    $seconddb = new wpdb('username','password','g3boat5_G3_Genba','localhost');
}
add_action('init','seconddb');
// add the shortcode [pontoon-table], tell WP which function to call
add_shortcode( 'pontoon-table', 'pontoon_table_shortcode' );

// 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 $seconddb;
    // 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>Previous Week Totals</th></tr>';
    $results = $seconddb->get_results( ' SELECT * FROM seconddb_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;
}

So that is the correct data being pulled from the database but as you can see it is not being displayed in the table as coded. What is my next step to try and get this to render in a nicely constructed table?

Furthermore...if someone knows, can you explain how I can put a place on this page to to select a date and then the data pulled from the database be from that date?

I am trying to do everything in shortcode.

Share Improve this question asked Feb 21, 2019 at 19:20 LellaLella 451 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

It's not displayed as table, because you don't return correct HTML code. Let's take a look at your code (I've put comments at end of incorrect lines):

$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>Previous Week Totals</th></tr>';  // <- here you open TR with </tr>, so it's already incorrect
$results = $seconddb->get_results( ' SELECT * FROM seconddb_PontoonBoats_LineOne_2Log' );
foreach ( $results AS $row ) {
    $content = '<tr>';  // <- here you overwrite previous value of $content with string '<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>';

So your function returns something like this, if there are no rows (it's incorrect HTML):

<table>
    </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>Previous Week Totals</th></tr>
</table>

And something like this, if there are any rows found:

<tr>
    <td><VALUE FOR: $row->{'Week Beginning'}></td>
    <td><VALUE FOR: $row->Monday></td>
    <td><VALUE FOR: $row->Tuesday></td>
    <td><VALUE FOR: $row->Wednesday></td>
    <td><VALUE FOR: $row->Thursday></td>
    <td><VALUE FOR: $row->Friday></td>
    <td><VALUE FOR: $row->Saturday></td>
    <td><VALUE FOR: $row->Sunday></td>
    <td><VALUE FOR: $row->{'Weekly Total'}></td>
    <td><VALUE FOR: $row->{'Previous Week Totals'}></td>
  </tr>
</table>

And here's the fixed version of that function:

function pontoon_table_shortcode( $args ) {

    global $seconddb;
    // 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>Previous Week Totals</th></tr>';
    $results = $seconddb->get_results( ' SELECT * FROM seconddb_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;
}

There are some mistakes in your code, replace this part

    $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>Previous Week Totals</th></tr>';

with this

 $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>Previous Week Totals</th></tr>';

and this

foreach ( $results AS $row ) {
    $content = '<tr>';
....
....
}

with this

foreach ( $results AS $row ) {
    $content .= '<tr>';
....
....
}

and see the result.

To make it a nice looking table, add some style through CSS.

I hope this will help.

UPDATE: @Krzysiek Dróżdż answer explains in detail what is wrong with your code.

Post a comment

comment list (0)

  1. No comments so far