I know such questions are previously answered and I applied all the possible solutions. I defined all the variables before the foreach loop but still, it's not working. Here My code:
$settings_table = $wpdb->prefix."wpsp_settings";
$sel_setting = $wpdb->get_results("select * from $settings_table");
$school_name = "";
$school_logo = "";
$school_add = "";
$school_city = "";
$school_state = "";
$school_country = "";
$school_number = "";
$school_email = "";
$school_site = "";
foreach( $sel_setting as $setting ) :
($setting->id == 1) ? $school_name = $setting->option_value : $school_name = "";
($setting->id == 2) ? $school_logo = $setting->option_value : $school_logo = "";
($setting->id == 6) ? $school_add = $setting->option_value : $school_add = "";
($setting->id == 7) ? $school_city = $setting->option_value : $school_city = "";
($setting->id == 8) ? $school_state = $setting->option_value : $school_state = "";
($setting->id == 9) ? $school_country = $setting->option_value : $school_country = "";
($setting->id == 10) ? $school_number = $setting->option_value : $school_number = "";
($setting->id == 12) ? $school_email = $setting->option_value : $school_email = "";
($setting->id == 13) ? $school_site = $setting->option_value : $school_site = "";
endforeach; ?>
$sel_settingget all the data you need?: $school_name = ""type code on each line as you already set them to blank before the loop. They will in fact reset the values each time.