• Resolved thejohnnyduke

    (@thejohnnyduke)


    Hi,

    I’ve a function to return the table inside a repeater field with the following code:

    if ( have_rows( $repeater_name ) ) {
    	while ( have_rows( $repeater_name ) ) { the_row();
    		$table = get_sub_field('content_table'));
    	}
    }

    If I var dump the table sub_field I get:

    string(69) "{"p":{"o":{"uh":0}},"c":[{"p":""}],"h":[{"c":""}],"b":[[{"c":"aa"}]]}"

    Everything is ok, except that, using the documentation provided I expected to be an array, so I can use the cycles described in the plugin description.

    Am I doing something wrong?

    Thanks

    https://ww.wp.xz.cn/plugins/advanced-custom-fields-table-field/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Johann Heyne

    (@jonua)

    Your code is ok.

    May there went something wrong with the PHP json_decode() function in your server environment. Please test the following code that should decode the JSON string to an array…

    var_dump( json_decode( '{"p":{"o":{"uh":0}},"c":[{"p":""}],"h":[{"c":""}],"b":[[{"c":"aa"}]]}', true ) );

    It should output this…

    array(4) { ["p"]=> array(1) { ["o"]=> array(1) { ["uh"]=> int(0) } } ["c"]=> array(1) { [0]=> array(1) { ["p"]=> string(0) "" } } ["h"]=> array(1) { [0]=> array(1) { ["c"]=> string(0) "" } } ["b"]=> array(1) { [0]=> array(1) { [0]=> array(1) { ["c"]=> string(2) "aa" } } } }
    Thread Starter thejohnnyduke

    (@thejohnnyduke)

    Hi Johann, thanks for the quick reply! 🙂

    The code you provided outputted the correct array (as you wrote).

    This is my complete code:

    single-post page (inside the loop):

    $product = new SG_products;
    $product->getProductRepeater('measurements', 'content_table', get_the_ID()));

    SG_products class:

    public function getProductRepeater( $repeater_name, $field_name, $product_id ) {
    	if ( have_rows( $repeater_name ) ) {
    		$descriptions = array();
    		while ( have_rows( $repeater_name ) ) { the_row();
    			foreach (get_sub_field('taxonomy_association') as $taxonomy_group) {
    				$descriptions[$taxonomy_group][$field_name]	= $this->getTable( $field_name );
    			}
    		}
    	}
    	return $descriptions;
    }
    
    private function getTable( $field_name ) {
    	$field = get_sub_field($field_name);
    	var_dump($field);
    	$table = '<table>';
    	foreach ( $field['body'] as $tr ) {
    		$table .= '<tr>';
    		foreach ( $tr as $td ) {
    			$table .= '<td>';
    			$table .= $td['c'];
    			$table .= '</td>';
    		}
    		$table .= '</tr>';
    	}
    	$table .= '</table>';
    	return $table;
    }

    The output of the var_dump is the json I wrote in the first post.
    Any thoughts?

    Thanks for the help!

    Plugin Author Johann Heyne

    (@jonua)

    There are to many ) at the end in line…

    $product->getProductRepeater('measurements', 'content_table', get_the_ID()));

    Anyway, I tested this class and it worked as expected.

    Strange

    Thread Starter thejohnnyduke

    (@thejohnnyduke)

    Hi Johann,

    I’ve found the error. I’m using a local wp install but with an remote database (dev env). I forgot to sync the plugin folder…
    In the admin area everything was ok (since I access it remotely), but testing locally, the wp installation didn’t find the plugin, hence the error…

    Sorry for the inconvenience, I need the weekend asap 😛

    Thanks once again and keep up the great work!

    Plugin Author Johann Heyne

    (@jonua)

    Nice, this is a topic for the plugins trouble shouting page.
    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘get field echoing json’ is closed to new replies.