Reputation: 75
Good Day,
I have received this code to form a plugin for Ninja Forms. This code enables you to put the submissions of a form on the front end. Everything was working fine until we added a date function. This function is supposed to put the date in front of every row it creates. Unfortunately it only puts the date in front of the first row, the every row after that, the information moves one column to the left with no date.
<?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' );
Upvotes: 0
Views: 159
Reputation: 1527
The problem is in the foreach loop. You need to place your date in the loop, not before :
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>
Upvotes: 2
Reputation: 3321
You have the beginning after you have the date added to the table.
It should be <tbody><tr><td>DATE STUFF</td><td>MORE CONTENT</td></tr><tbody>
in this format and your date column should be placed within your foreach loop.
Upvotes: 0