• Resolved kemalkautsar

    (@kemalkautsar)


    Hi Guys,

    Thanks for the support so far, Adam, Zafer, Kris. you are a legend!

    I have another thing going on. As you can see I am using my code to show the latest entry of a form that you guys helped me out to build on another ticket

    what I am trying to achieve now is showing the list of form entries using shortcode. What I did so far is showing just the var_dump but not the actual table of entries. not sure where it went wrong i guess because of the way i populate the array.

    here’s the code:

    add_shortcode('wd-listentries', 'wd_listentries_func');

    function wd_listentries_func($atts){
    $a = shortcode_atts( array(
    'title' => 'guestman',
    'field' => 'name-1'
    ), $atts );

    $wd_form_id = get_page_by_title( $a['title'], '', 'forminator_forms' );
    $entry = Forminator_Form_Entry_Model::get_entries($wd_form_id->ID);

    if ($entry){


    var_dump($entry);

    foreach ($entry as $item){

    echo '<table>';
    echo ' <tbody>';
    echo ' <tr>';
    echo ' <td>';
    $entry[] = $item['name-1'];
    echo ' </td>';
    echo ' <td>';
    $entry[] = $item['text-1'];
    echo ' </td>';
    echo ' <td>';
    $entry[] = $item['text-2'];
    echo ' </td>';
    echo ' </tr>';
    echo ' </tbody>';
    echo '</table>';

    }
    }

    }

    the inputs inside the guestman are: name-1, text-1, and text-2.

    Is there a specific way to index the individual input inside entries? can you show me where I did wrong? I’m torn between the way i index it or how i foreach it. :/

    p/s: again, my programming skill is equivalent of a Psyduck or a salamander, so you guys might want to take me step by step

    THANKS GUYS! YOU ARE THE BEST<3

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @kemalkautsar

    I hope you’re well today and thank you for your kind words!

    As for your question, note please that the entries data returned by the “get_entries()” method is a nested structure but the most important part is that it’s not just an array but rather array of objects.

    The top level is an array but each element of the array (the “item” in case of your loop) is an object so you cannot handle it this way. The actual entry data inside that object is in the “meta_data” property so you need to access that first and then you have an array where the field name is array key. Finally, in that array you may have different data structures depending on the filed type.

    Assuming you only want to display name and text fields, and your name field is a simple name field (as in name field settings – it’s not “multiple” with prefix, first and last name and so on), you can do it like this:

    $html = '';
    
    	if ($entry){
    
    		$html = '<table><tbody>'; 
    		
    		foreach( $entry as $key=>$value ) { 
    		
    			$field_data = $value->meta_data;
    			
    			$html .= "<tr>";
    			
    			if ( array_key_exists( 'name-1', $field_data ) ) {
    				$html .= "<td>" . $field_data['name-1']['value'] . "</td>";
    			} else {
    				$html .= "<td></td>";
    			}
    			
    			if ( array_key_exists( 'text-1', $field_data ) ) {
    				$html .= "<td>" . $field_data['text-1']['value'] . "</td>";
    			} else {
    				$html .= "<td></td>";
    			}
    			
    			if ( array_key_exists( 'text-2', $field_data ) ) {
    				$html .= "<td>" . $field_data['text-2']['value'] . "</td>";
    			} else {
    				$html .= "<td></td>";
    			}
    			
    			$html .= "</tr>";
    			
    			
    			
    		}
    		
    		
    		$html .= '</tbody></table>';
    
    	}
    	
    	return $html;

    Note: I assumed that you already have entries fetched.

    You could probably notice also that I’m not using “echo” function here. That doesn’t really affect data access, it’s just that it’s the more “elegant” way (preferred) way to output content of shortcode.

    Let me also share a small trick to format var_dump() output so you could better see how the data is structured, this would help you better understand how to access the data to; so instead of using just

    var_dump( $something );

    do it like this

    echo "<pre>";
    var_dump( $someting );
    echo "</pre>";

    and you’ll get a nice “tree-like” structured display.

    I hope this helps!

    Best regards,
    Adam

    Thread Starter kemalkautsar

    (@kemalkautsar)

    Adam,

    That works GREAT! solved.!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.