1

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; ?>
4
  • What is not working? What error do you get? Your $sel_setting get all the data you need? Commented Mar 14, 2018 at 15:04
  • You don't need the : $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. Commented Mar 14, 2018 at 15:05
  • I'm trying to access variable outside the body of foreach loop. I'm not getting any error message. it simply do not displaying the value of the variable Commented Mar 14, 2018 at 15:06
  • You say 'displaying the value of the variable' - which variable? Commented Mar 14, 2018 at 15:14

2 Answers 2

2

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.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.