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; ?>
You are resetting the values each time round the loop, as for each item you'r saying...
($setting->id == 1) ? $school_name = $setting->option_value : $school_name = "";
As this loop has different values for $setting->id, this will reset all of the values which don't match.
You would be better off with a switch... case...
structure...
foreach( $sel_setting as $setting ) {
switch ($setting->id) {
case (1):
$school_name = $setting->option_value;
break;
case (2):
$school_logo = $setting->option_value;
break;
// Same for all the others.
}
}
it doesn't have sens:
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;
for example if $setting->id == 5, you set all variables to blank string OR if you $setting->id == 1 you set $school_name to option_value BUT in the same time set all other variables to blank string.
Simple solution is to use switch / case statement like below:
foreach( $sel_setting as $setting ) {
switch ($setting->id) {
case 1:
$school_name = $setting->option_value;
break;
case 2:
$school_logo = $setting->option_value;
break;
...
case 13:
$school_site = $setting->option_value;
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.