php date only show in first row - php

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' );

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>

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.

Related

How to limit this foreach loop to 10 loops?

How can I limit the code below to show 10 loops.
foreach( $entries as $entry ) {
echo '<tr>';
$fields = wpforms_decode( $entry->fields );
foreach( $fields as $field ) {
if ( in_array( $field['id'], $ids)) {
echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
}
}
echo '</tr>';
}
like this :
$i = 0;
foreach( $entries as $entry ) {
$i++;
if ($i > 9 ) break; // this will stop after the 10th loop and in the beginning of loop 11
echo '<tr>';
$fields = wpforms_decode( $entry->fields );
foreach( $fields as $field ) {
// if you want to stop this loop too use $ii not $i
// but notice stopping this loop will not stop the parent loop !
if ( in_array( $field['id'], $ids)) {
echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
}
}
echo '</tr>';
}
One possibility where the loop to break is the first one:
foreach( $entries as $key=>$entry )
{
if($key==9) break;
echo '<tr>';
$fields = wpforms_decode( $entry->fields );
foreach( $fields as $field )
{
if ( in_array( $field['id'], $ids))
{
echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
}
}
echo '</tr>';
}
Another possiblity where the loop to break is the second one:
foreach( $entries as $entry )
{
echo '<tr>';
$fields = wpforms_decode( $entry->fields );
foreach( $fields as $key=>$field )
{
if($key==9) break;
if ( in_array( $field['id'], $ids))
{
echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
}
}
echo '</tr>';
}
If you want to stop the whole process / method / function you might want to use return instead of break. Break will just stop the current loop process.

PHP - Is it possible to add a string to a variable?

I would like to make a small change in a code for a table of contents.
I want to add a sign in front of each heading. The character should be recognized as text.
I've tried a few things, but unfortunately I have not found the right variable.
The code comes from a plugin for Wordpress
I have already tried the following variables:
$items
$tic
$find
$replace
$post
Here is the code that prints the list:
if ( $tic->is_eligible($custom_toc_position) ) {
extract( $args );
$items = $tic->extract_headings( $find, $replace,wptexturize($post->post_content) );
$title = ( array_key_exists('title', $instance) ) ? apply_filters('widget_title', $instance['title']) : '';
if ( strpos($title, '%PAGE_TITLE%') !== false ) $title = str_replace( '%PAGE_TITLE%', get_the_title(), $title );
if ( strpos($title, '%PAGE_NAME%') !== false ) $title = str_replace( '%PAGE_NAME%', get_the_title(), $title );
$hide_inline = $toc_options['show_toc_in_widget_only'];
$css_classes = '';
// bullets?
if ( $toc_options['bullet_spacing'] )
$css_classes .= ' have_bullets';
else
$css_classes .= ' no_bullets';
if ( $items ) {
// before widget (defined by themes)
echo $before_widget;
// display the widget title if one was input (before and after titles defined by themes)
if ( $title ) echo $before_title . $title . $after_title;
// display the list
echo '<ul class="toc_widget_list' . $css_classes . '">' . $items . '</ul>';
// after widget (defined by themes)
echo $after_widget;
}
This are the full code of function extract_headings:
public function extract_headings( &$find, &$replace, $content = '' )
{
$matches = array();
$anchor = '';
$items = false;
// reset the internal collision collection as the_content may have been triggered elsewhere
// eg by themes or other plugins that need to read in content such as metadata fields in
// the head html tag, or to provide descriptions to twitter/facebook
$this->collision_collector = array();
if ( is_array($find) && is_array($replace) && $content ) {
// get all headings
// the html spec allows for a maximum of 6 heading depths
if ( preg_match_all('/(<h([1-6]{1})[^>]*>).*<\/h\2>/msuU', $content, $matches, PREG_SET_ORDER) ) {
// remove undesired headings (if any) as defined by heading_levels
if ( count($this->options['heading_levels']) != 6 ) {
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
if ( in_array($matches[$i][2], $this->options['heading_levels']) )
$new_matches[] = $matches[$i];
}
$matches = $new_matches;
}
// remove specific headings if provided via the 'exclude' property
if ( $this->options['exclude'] ) {
$excluded_headings = explode('|', $this->options['exclude']);
if ( count($excluded_headings) > 0 ) {
for ($j = 0; $j < count($excluded_headings); $j++) {
// escape some regular expression characters
// others: http://www.php.net/manual/en/regexp.reference.meta.php
$excluded_headings[$j] = str_replace(
array('*'),
array('.*'),
trim($excluded_headings[$j])
);
}
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
$found = false;
for ($j = 0; $j < count($excluded_headings); $j++) {
if ( #preg_match('/^' . $excluded_headings[$j] . '$/imU', strip_tags($matches[$i][0])) ) {
$found = true;
break;
}
}
if (!$found) $new_matches[] = $matches[$i];
}
if ( count($matches) != count($new_matches) )
$matches = $new_matches;
}
}
// remove empty headings
$new_matches = array();
for ($i = 0; $i < count($matches); $i++) {
if ( trim( strip_tags($matches[$i][0]) ) != false )
$new_matches[] = $matches[$i];
}
if ( count($matches) != count($new_matches) )
$matches = $new_matches;
// check minimum number of headings
if ( count($matches) >= $this->options['start'] ) {
for ($i = 0; $i < count($matches); $i++) {
// get anchor and add to find and replace arrays
$anchor = $this->url_anchor_target( $matches[$i][0] );
$find[] = $matches[$i][0];
$replace[] = str_replace(
array(
$matches[$i][1], // start of heading
'</h' . $matches[$i][2] . '>' // end of heading
),
array(
$matches[$i][1] . '<span id="' . $anchor . '">',
'</span></h' . $matches[$i][2] . '>'
),
$matches[$i][0]
);
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
}
// build a hierarchical toc?
// we could have tested for $items but that var can be quite large in some cases
if ( $this->options['show_heirarchy'] ) $items = $this->build_hierarchy( $matches );
}
}
}
return $items;
}
I tried it like this:
$items = '>'.$items
$tic = '>'.$tic
$find = '>'.$find
.
.
.
Unfortunately, nothing has hit the right place
$ items addressed only the entire list
The other Variables had no effect or led to errors
The extract_headings is creating the list items. This section of the function...
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
Should look like this:
// assemble flat list
if ( !$this->options['show_heirarchy'] ) {
$items .= '<li><a href="#' . $anchor . '">>';
if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
$items .= strip_tags($matches[$i][0]) . '</a></li>';
}
You can see I've added an extra > inside the hyperlink on the third line. If adding an extra > causes any issues, you can also use >.

How to Display Wordpress Custom Fields after 2nd paragraph in a post

am using genesis framework. used this code in functions.php file. but displayed data in a table format at the end of post only. But i want to display data after Nth paragraph in a post only.
function add_product_spec_to_content($content) {
if(get_field('befestigung') || get_field('gurtsystem') || get_field('gruppe') || get_field('typ') || get_field('amazon') || get_field('bezug') || get_field('gewicht')) { $content = $content . '<table id="product-specification" cellspacing="0" border="0" style="border-collapse:collapse;"><tbody>'; }
if(get_field('befestigung')) { $content = $content . '<tr><td>Befestigung</td><td>' . get_field('befestigung') . '</td></tr>'; }
if(get_field('gurtsystem')) { $content = $content . '<tr><td>Gurtsystem</td><td>' . get_field('gurtsystem') . '</td></tr>'; }
if(get_field('bezug')) { $content = $content . '<tr><td>Bezug</td><td>' . get_field('bezug') . '</td></tr>'; }
if(get_field('gruppe')) { $content = $content . '<tr><td>Gruppe</td><td>' . get_field('gruppe') . '</td></tr>'; }
if(get_field('typ')) { $content = $content . '<tr><td>Typ</td><td>' . get_field('typ') . '</td></tr>'; }
if(get_field('gewicht')) { $content = $content . '<tr><td>Gewicht</td><td>' . get_field('gewicht') . ' kg</td></tr>'; }
if(get_field('amazon')) { $content = $content . '<tr><td>Preis</td><td>' . get_field('amazon') . '</td></tr>'; }
if(get_field('befestigung') || get_field('gurtsystem') || get_field('gruppe') || get_field('typ') || get_field('amazon') || get_field('bezug') || get_field('gewicht')) { $content = $content . '</tbody></table>'; }
return $content; } add_filter( 'the_content', 'add_product_spec_to_content', 1150 );
You can create a shortcode for this und place it where you want:
function sc_my_table( $atts ) {
$fields = array(
'befestigung' => 'Befestigung',
'gurtsystem' => 'Gurtsystem',
'bezug' => 'Bezug',
'gruppe' => 'Gruppe',
'typ' => 'Typ',
'gewicht' => 'Gewicht',
'amazon' => 'Preis',
);
$table_open = false;
$content = '';
foreach ( $fields as $field => $name ) {
if ( $value = get_field( $field ) ) {
if ( ! $table_open ) {
$content .= '<table id="product-specification" cellspacing="0" border="0" style="border-collapse:collapse;"><tbody>';
$table_open = true;
}
$content .= '<tr><td>'. $name .'</td><td>' . $value . '</td></tr>';
}
}
if ( $table_open ) {
$content .= '</tbody></table>';
}
return $content;
}
add_shortcode('my-table', 'sc_my_table');
Then use [my-table] in text editor at the desired place.
Reference: add_shortcode()

Else in foreach looping

What I want if the result is not found from the query select, I want to show else function like:
else {
$html .= 'You have not added stock at all';
}
Because in this case I used string of html, I don't know how to echo the else statement.
More or less my codes is looking like this right now (There are many parts I have removed since it's too long)
<?php
include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if ($resultsClaim) {
while($obj = $resultsClaim->fetch_object()) {
$orders[$obj->id_cart][$obj->items] = array('status' => $obj->status ...);
}
foreach ($orders AS $order_id => $order) {
$orderCount = count($order);
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$row++;
}
$html .= '<div>
<div class="member-popUpeStock'.$data['id'].' member-PopUp">
<div class="member-PopUp-box">
X
<div class="tablePopUp">
<div class="table-row">
<div class="col">: '.$data['method'].' </div>';
///HERE WHERE I WANT TO DO THAT////
else {
echo 'Nothing';
}
echo $html;
?>
Can anyone help me, please! Thanks in Advance.
Since you are taking the no of orders into a variable, you can make use of that directly to see if the orders exists or not.
$orderCount = 0; //Define orderCount in the beginning.
//Rest of the code, till foreach
foreach(){
//Rest of the code here
}
if(intval($orderCount) <= 0) //Using intval, for the case the for loop is not executed at all
{
$html = "Nothing";
}
Here you dont need to concatenate it to $html since you are displaying the table content if order exists or else you displaying "Nothing".
So $html will either have the table content or else "Nothing".
Hope this helps.
First get count of $orders
if (! empty($orders)) {
foreach ($orders AS $order_id => $order) {
// YOUR CODE HERE
}
}
else {
$html .= 'Nothing'; // Append your `no records found` message to the `$html` variable:
}
foreach ($orders AS $order_id => $order) { } else {}
So "}" is missing for foreach loop. Hope it will work for you. Let me know if you need further help.
Give it a try
<?php
include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if (!empty($resultsClaim)) {
while ($obj = $resultsClaim->fetch_object()) {
$orders [$obj->id_cart][$obj->items] = array('status' => $obj->status);
}
foreach ($orders AS $order_id => $order) {
$orderCount = count($order);
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) {
$html .= '</tr><tr>';
}
$html .= '<td>' . $item . '</td>';
$row++;
}
$html .= '<div>
<div class="member-popUpeStock' . $data['id'] . ' member-PopUp">
<div class="member-PopUp-box">
X
<div class="tablePopUp">
<div class="table-row">
<div class="col">: ' . $data['method'] . ' </div>';
}
}
///HERE WHERE I WANT TO DO THAT////
else {
echo 'Nothing';
}
echo $html;
?>

Unable to save custom metabox data from metabox plugin

I am quite new to php and wordpress but have programming knowledge.
i am attempting to save data from these metaboxes in metabox plugin by creating a new custom metabox. this is my code
$tour_rates = get_post_meta( $post->ID, $id, true ) ? maybe_unserialize(get_post_meta( $post->ID, $id, true )) : false;
if ($tour_rates){
foreach ( $tour_rates as $options => $option ) {
$key = 1
$html .= '<tr class="rate-line">';
$html .= '<td>';
$html .= '<span>Opt ' . ($options+1) . '</span>';
$html .= '<input type="hidden" name="' . $id . '[]" class="rwmb-text" size="30" value="">';
$html .= '</td>';
$html .= '<td><input type="text" name="pax_date_'.$key.'[]" class="rwmb-date" size="3" value="'.$option[$key-1].'"></td>';
$html .= '<td><input type="text" name="pax_price_'.$key.'[]" class="pax-price" size="3" value="'.$option[$key-1].'"></td>';
$html .= '</tr>';
}
Now in the save function i have this code. i am able to save only pax-price and not the date along with pax-price. need help and thanks in advance for all your help
static function save( $new, $old, $post_id, $field )
{
$name = $field['id'];
$tour_rates = array();
foreach ( $_POST[$name] as $k => $v ) {
$tour_rates[$k] = array(
$_POST['pax_price_1'][$k],
$_POST['pax_date_1'][$k],
);
}
$new = maybe_serialize( $tour_rates );
update_post_meta( $post_id, $name, $new );
}
Anyways I got the solution. i had made a mistake in the way i was displaying, just in case someone needs a way:
$tour_rates = get_post_meta( $post->ID, $id, true ) ? maybe_unserialize(get_post_meta( $post->ID, $id, true )) : false;
$html = '<div id="tour-rates-info">';
$html .= '<div class="inside">';
$html .= '<div class="rwmb-field">';
$html .= '<div class="rwmb-input">';
$html .= '<table>';
$html .= '<tr>';
$html .= '<th> </th>';
$html .= '<th>'."Departure Date".'</th>';
$html .= '<th>'."Price".'</th>';
$html .= '</tr>';
if ($tour_rates){
foreach ( $tour_rates as $k => $v ) {
$key = 1;
$html .= '<tr class="rate-line">';
$html .= '<td>';
$html .= '<span>Opt ' . ($k+1) . '</span>';
$html .= '<input type="hidden" name="' . $id . '[]" class="rwmb-text" size="30" value="">';
$html .= '</td>';
$html .= '<td><input type="text" name="pax_date_'.$key.'[]" class="rwmb-date" size="9" value="'.$v[0].'"></td>';
$html .= '<td><input type="text" name="pax_price_'.$key.'[]" class="pax-price" size="9" value="'.$v[1].'"></td>';
$html .= '</tr>';
}
Now the save function remains same
static function save( $new, $old, $post_id, $field )
{
$name = $field['id'];
$tour_rates = array();
$tour_rate = array();
foreach ( $_POST[$name] as $k => $v ) {
$tour_rates[$k] = array(
$_POST['pax_date_1'][$k],
$_POST['pax_price_1'][$k],
);
}
echo $tour_rates;
$new = maybe_serialize( $tour_rates );
update_post_meta( $post_id, $name, $new );
}

Categories