php日期只显示在第一行

时间:2014-01-31 09:36:07

标签: php

美好的一天,

我收到此代码以构建Ninja Forms的插件。此代码使您可以将表单的提交放在前端。在我们添加日期功能之前,一切正常。该函数应该将日期放在它创建的每一行的前面。不幸的是,它只将日期放在第一行的前面,之后的每一行,信息向左移动一列,没有日期。

<?php
function test_display_submissions( $args = array() ) {
    // $args = array(
    //   'form_id' => 1
    // );
    $form_id = $args['form_id'];
    $columns = $args['cols'];
    $columns = explode( ',', $columns );
    $sub_results = ninja_forms_get_subs( array( 'form_id' => $form_id ) );
    $plugin_settings = get_option("ninja_forms_settings");
    if(isset($plugin_settings['date_format'])){
        $date_format = $plugin_settings['date_format'];
    } else {
        $date_format = 'm/d/Y';
    }
    $content = '<table>
        <thead>
    <tr>';
    $content .= '<th>Date</th>';
    foreach ( $columns as $id ) {
        $field = ninja_forms_get_field_by_id( $id );
        if( isset( $field['data']['label'] ) ){
            $label = $field['data']['label'];
        } else {
            $label = '';
        }
        $content .= '<th>' . $label . '</th>';
    }
    $content .= '</tr>
    </thead>
    <tbody>';
    $content .= '<td>' . date($date_format, strtotime($sub['date_updated'])) . '</td>';
    foreach ( $sub_results as $sub ) {
        $fields = $sub['data'];
        echo '<tr>';
        foreach ( $fields as $field ) {
            $field_id = $field['field_id'];
            $user_value = $field['user_value'];
            if ( in_array( $field_id, $columns ) ) {
                $content .= '<td>' . $user_value . '</td>';
            }
        }
    $content .= '</tr>';
    }
    $content .= '</tbody>
    </table>';
    return $content;
}
add_shortcode( 'display_subs', 'test_display_submissions' );

2 个答案:

答案 0 :(得分:2)

问题在于foreach循环。您需要将日期放在循环中,而不是之前:

    foreach ( $sub_results as $sub ) {
        // Display date
        $fields = $sub['data'];
        $content .= '<tr>';
        $content .= '<td>' . date($date_format, strtotime($sub['date_updated'])) . '</td>';
        foreach ( $fields as $field ) {
            $field_id = $field['field_id'];
            $user_value = $field['user_value'];
            if ( in_array( $field_id, $columns ) ) {
                $content .= '<td>' . $user_value . '</td>';
            }
        }
        $content .= '</tr>';
    }
    $content .= '</tbody>

答案 1 :(得分:0)

您将日期添加到表格后即可开始。

它应该是<tbody><tr><td>DATE STUFF</td><td>MORE CONTENT</td></tr><tbody> 在此格式中,您的日期列应放在foreach循环中。