代码提供了一行数据,但没有将其格式化为表格

时间:2019-02-21 作者:Lella

我正在使用以下代码:

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;
}

enter image description here

因此,这是从数据库中提取的正确数据,但正如您所看到的,它没有按编码显示在表中。我的下一步是如何尝试将其呈现在构造良好的表中?

此外如果有人知道,你能解释一下我如何在这个页面上放置一个位置来选择一个日期,然后从数据库中提取的数据就是从该日期开始的吗?

我试着用短代码做每件事。

2 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

它不会显示为表,因为您没有返回正确的HTML代码。让我们看看您的代码(我在不正确的行末尾添加了注释):

$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>\';
因此,如果没有行(这是不正确的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>
类似于这样,如果找到任何行:

<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>
下面是该函数的固定版本:

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网友:Qaisar Feroz

代码中有一些错误,请更换此部件

    $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>\';
用这个

 $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>\';
还有这个

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

foreach ( $results AS $row ) {
    $content .= \'<tr>\';
....
....
}
并查看结果。

要使其成为一个好看的表,请通过CSS添加一些样式。

我希望这会有所帮助。

更新:@Krzysiek Dródżanswer详细解释了代码的错误。

相关推荐

CREATE TABLE WITH DBDelta,无法放置任何默认数据

我想向wp\\U用户添加更多自定义表但我希望它们包含默认的JSON数据这一个有效。。。当默认值为空时。。。 register_activation_hook(__FILE__, \'toto_user_db_setup\'); function toto_user_db_setup() { global $wpdb; $user_data = \"wp_users\"; $charset_collate = $wpdb->get