0

I have a large datatable form. I like to set the database variables dynamically in this case the form input value submitted is 99

$nbs = array();
    foreach ($rows as $r) {
        $dec1 = 'q' . $r["sort"] . 'z1';
        $dec2 = '(int) $_POST["q' . $r["sort"] . 'z1"]';
        $nbs[$dec1] = $dec2;
                        }

extract($nbs);

This creates the following:

 $q1000z1 = (int) $_POST[q1000z1];
 var_dump($q1000z1);

outputs: string(27) "(int) $_POST["q100000z1"]"

but if i write the code

      $q1000z1 = (int) $_POST[q1000z1];
      var_dump($q1000z1);

outputs: 99

I like to get the 99 but something is wrong in my extract method because it doesn't get referenced to the form input value? Any suggestions?

1 Answer 1

1

Don't put the $_POST[…] expression in a string. Just do:

 $dec2 = (int) $_POST[$dec1];

You already assembled the array index $dec1 there. So just use it for literal array access. Afterwards $dec2 contains the desired $_POST input value.

(Now what you generally don't want to do is use extract() there. That only makes sense to unfold small in-application arrays. For larger structures, just keep and work with the $nbs array instead.)

Sign up to request clarification or add additional context in comments.

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.