L
L
ligisayan2019-04-23 11:37:11
WordPress
ligisayan, 2019-04-23 11:37:11

In php I get a different scope for a variable inside a condition - how to fix it?

Hello!
The online store has fields that receive values ​​from the $_POST form , including billing_email1, billing_email2, billing_email3 , etc. depending on the given amount. (there are other fields too, but we are not interested here)
My task is to write the incoming values ​​into the fields:
In billing_email , the first value is billing_email1 , and in billing_email_dop , all subsequent billing_email2, billing_email3 , etc. through a semicolon.
I've been fighting for a day now, I just can't figure out what is the reason why $key outside and inside the conditions turn out to be different ..

<?php	
    $fields = $checkout->get_checkout_fields( 'billing' );

  foreach ( $fields as $key => $field ) {
    $i = 1;
    $postdata = "";
    $postdop = "";
    $postemail = "";
    var_dump($key); // $key = billing_email_dop есть
    if($_POST["$key"."$i"]) {
      var_dump($key); // $key = billing_email_dop уже нет, вместо него $key = billing_email - почему?
      while($_POST["$key"."$i"]) { 
        
        if($key==="billing_email") {
          if(("$key"."$i")=="billing_email1") {
            $postemail = $_POST['billing_email1'];
          }
          else {
            $postdop .= $_POST["$key"."$i"]."; "; //в $postdop значение записывается корректно
          }
        }
        else {
          $postdata .= $_POST["$key"."$i"]."; ";
        }
        $i++;
      }
    }
      if($key=="billing_email") {
        woocommerce_form_field( $key, $field, $postemail );
      }
      else if($key=="billing_emaildop") {
        woocommerce_form_field( $key, $field, $postdop );
      }
      else {
        woocommerce_form_field( $key, $field, $postdata );
      }
  } 
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2019-04-23
@ligisayan

wardump $fields after $checkout->get_checkout_fields( 'billing' ); otherwise it’s not clear what you get there, maybe it’s a resource in general ...
PS: add if($_POST["$key"."$i"]) {...} else{var_dump($key);} and everything will fall into place.
PPS: in general, the code, of course, besides being built feet first, is replete with both obvious errors and implicit (for beginners) glitches ... for example , billing_email_dop eventually becomes billing_emaildop , I am silent about skipping iterations inside conditional operators, and as Maxim Timofeev pointed out - cyclically overwritten data
In short, a classic example of how NOT to do it.
How to change:
1) It makes no sense to build a loop on $fields, you clearly know its structure, unlike the data from $_POST.
2) In the $_POST loop, look for keys that match the pattern (for example, checking for the billing_email substring or the billing_email\d{1,2} regular expression). If exactly billing_email1 - write to $postemail, otherwise $postdop[] = $val; On the client, the form fields should be named more clearly, for example, billing_email[], after which you can immediately get the ENTIRE array of emails.
3) at the end if $postdop is not empty - multiply $postdop values ​​with commas, get your string with emails separated by commas.
4) assign everything to the required fields of your $fields.
5) Profit.

M
Moses Fender, 2019-04-23
@mosesfender

What does woocommerce_form_field(...) look like?
Apparently, there the first parameter is passed by reference, and it changes there. Because it is different in the cycle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question