Retrieve and display data from table using Shortcode EXEC PHP - Wordpress Solution
[Solved] - Retrieve and display data from table using Shortcode EXEC PHP - Wordpress Solution
Try following codes
I have a form using Gravity Forms that collects first name, last name, city and state on my wordpress site. I want to then display that data in a table on a wordpress page, sorted by last name. I started using the Shortcode EXEC PHP plugin to create the code to retrieve/display the data, but I just can't seem to get it to work. I need this completed by Wed July 7. Update: Here is the code that I was trying to get to work:

global $wpdb;
$results = $wpdb->get_results("SELECT f.id, f1.value as first_name, f2.value as last_name, f3.value as city, f4.value as state, FROM ".$wpdb->wp_."rg_lead as f INNER JOIN ".$wpdb->wp_."rg_lead_detail  as f1 ON (f.id = f1.lead_id AND f1.field_number = '1') INNER JOIN ".$wpdb->wp_."rg_lead_detail  as f2 ON (f.id = f2.lead_id AND f2.field_number = '2') INNER JOIN ".$wpdb->wp_."rg_lead_detail  as f3 ON (f.id = f3.lead_id AND f3.field_number = '3') INNER JOIN ".$wpdb->wp_."rg_lead_detail  as f4 ON (f.id = f4.lead_id AND f4.field_number = '4') WHERE f.form_id = '4');
foreach ($results as $result){
return .$result['first_name']." ".$result['last_name']."";
return .$result['city']." ".$result['state']."------------";
Notice all four fields have the same lead_id, so you'll need to filter by that, plus only grab those fields that are form_id=4. The last part is what was failing - when it was trying to output the results. Although I need to the results in an html table, so that part needs to be changed anyway. And perhaps this is not the most efficient SQL query, so if you can improve it, that would be great too. Since I'm really out of my league here, I'll need all the code provided for me. Attached is a screenshot of the db table screenshot and dump

Solution - 1

no need to use external plugin for this... simple add your shortcode and return all records in shortcode

add_shortcode( 'shortcodename' , 'shortcode_callback' );
function shortcode_callback($atts, $content){
	gloabl $wpdb;
	// in array your shortcodes attributes
	extract(shortcode_atts(array('show' => 'form'), $atts));

		case 'some':
			$results = $wpdb->get_results("SQL SELECT STATMENT GOSE HERE"); 
			$content = '<table>';
			foreach($results as $k=>$i){
				$content .= 'some html code + database values gose here';
			$content .= '</table>';
	return "{$content}";

Date: 2022-10-21 05:21:58
Category: Wordpress
More for You