• Resolved Anonymous User 13716511

    (@anonymized-13716511)


    I am using the ‘tablepress_table_render_data’ filter to display all the data on the table.
    I’m using a query to get data from the database. However, only the last string of the data in the array is showing in the table.
    How do I get them all to show?
    Here is my code:
    `$args = array( ‘post_type’ => array(‘therapist_listing’, ‘therapist’), ‘posts_per_page’ => 10 );
    $loop = new WP_Query( $args );
    $i = 0;
    if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();
    $i++;
    $field_name_type = ‘what_type_of_mental_health_professional_are_you_’;
    $field = get_field_object($field_name_type);
    $value = $field[‘value’];
    foreach($value as $v ){
    $data = $field[‘choices’][ $v ];
    echo $data; //this displays all the data in the field eg Clinical Social Worker, Licensed Professional Counselor, Certified Alcohol and Drug Abuse Counselor
    $table[‘data’][$i][1] = $data; // this only give one value in the table eg Certified Alcohol and Drug Abuse Counselor
    }

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your question, and sorry for the trouble.

    Is your $i variable really increasing in your code?
    Also, no matter how many values you would have in that “what_type_of_mental_health_professional_are_you_” field, it would be overwritten each time. Your loop simply is wrong for what you want to do, I think.

    Regards,
    Tobias

    Thread Starter Anonymous User 13716511

    (@anonymized-13716511)

    Yes the $i is increasing as it should in the code. The loop seems to be correct, I’ve used it in other places just I can’t seem to store more then one value in the variable $table[‘data’][$i][1];
    if I echo $value all the values are shown so why if I set $table[‘data’][$i][1] = $value; is only one value shown?

    Thread Starter Anonymous User 13716511

    (@anonymized-13716511)

    If $table[‘data’][$i][1] = $field;
    where $field is an array I get the errors:
    `Warning: substr() expects parameter 1 to be string, array given in xxx/wp-content/plugins/tablepress/classes/class-render.php on line 562

    Warning: nl2br() expects parameter 1 to be string, array given in xxx/wp-content/plugins/tablepress/classes/class-render.php on line 766

    if I try and loop through the array with an if($field as $fields){ $table[‘data’][$i][1] = $fields; } I can only get the last value in the array.
    $table[‘data’][$i][1] = $field[0] . $field[1] . $field[2] gives me the correct data but I need to loop through it, how can I do this?

    Thank you so much for your help

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    well, $table[‘data’][$i][1] is a string in itself, so setting
    $table[‘data’][$i][1] = $value;
    simply sets the string to a new one every time.
    What you want is to concatenate the existing value with the new piece. For that, you’d need e.g. code like
    $table['data'][$i][1] = $table['data'][$i][1] . ' ' . $value;
    or the shorter version
    $table['data'][$i][1] .= ' ' . $value;

    Regards,
    Tobias

    Thread Starter Anonymous User 13716511

    (@anonymized-13716511)

    Thank you so much for explaining that. I didn’t realise that was the case. I spent hours on this.
    It is working now.
    Thank you for your great plugin and your amazing prompt support! Really appreciated.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! 🙂 Good to hear that this helped!
    This is the same for any other variable in PHP, nothing special. It might just be confusing that you are dealing with a multidimensional array here.

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘display all data’ is closed to new replies.