1

I am having a real issue with variable interpolation - maybe what I'm trying to do shouldn't be done? Sample code (IRL, the array is 70+ elements):

<form submit>
$_POST['A']; //returns 12
$_POST['B']; //returns 8

<query to get orig field values>
$OrigA = $row->appA; //returns 12
$OrigB = $row->appB; //return 14

<array of names>
$fields = array ("A", "B");

$querystr = "INSERT INTO tblEditHist VALUES ";
foreach ($fields as $field) {
    $orig = "Orig" . $field;
    $new = "\$_POST['app" . $field . "']";
    $fieldname = "app" . $field;

    if (${$orig} != ${$new}) { $querystr .= "('default', $applID, '$fieldname', '${$orig}', '${$new}', '$DateTimeReq', '$hvid'), "; };
}
print "querystr: " . $querystr . "\n";

The part if (${$orig} != ${$new}) is what's not working as I would expect. When I print it out to screen with print "DEBUG: if (${$orig} != ${$new}) { $querystr .= \"('default', $applID, '$fieldname', '{$orig}', '{$new}', '$DateTimeReq', '$hvid')\"; };<br />\n";, I see my variables aren't interpolating properly, my debug print says: DEBUG: if ( != ) { .... It should interpolate to (for B): DEBUG: if (8 != 14) { ...

I have tried various combinations of dollar signs and braces, but I don't seem to be making headway. Am I barking up the wrong tree here?

Thanks as always!!

2
  • Lose the curly brackets { }. they are not adding any added advantages to your comparison. Commented Jan 17, 2015 at 16:36
  • Thanks everyone for your help and guidance. I couldn't make anyone's solution work as described, but a minute ago I got it working by adding a second array that holds all the original values as the $OrigA format. In my foreach, I just use the key to loop through the 2nd array same time as the first and suck in the value. May not be pretty or ideal, but it is functional! Thanks again everyone - you all lead me to the answer with your contributions. Wish I could mark everyone's answer "Correct". Commented Jan 17, 2015 at 21:18

2 Answers 2

1

Like this : No interpolation POST variable

<?php
$A = $_POST['A'] = 12; //returns 12
$B = $_POST['B'] = 8; //returns 8


$OrigA = 12; //returns 12
$OrigB = 14; //return 14


$fields = array ("A", "B");

$querystr = "INSERT INTO tblEditHist VALUES ";
foreach ($fields as $field) {

    $orig = "Orig" . $field;
    $fieldname = "app" . $field;


    if (${$orig} != ${$field}) { $querystr .= "('default', $applID, '$fieldname', '${$orig}', '${$new}', '$DateTimeReq', '$hvid'), "; };
}
print "querystr: " . $querystr . "\n";

?>

See http://codepad.org/Ecro0PyG

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

Comments

1

I don't get all your questions but maybe this code could help you a little (you can use your $_POST var instead of mine $post, that is just to show result and debug):

$post = array();
$post['A'] = 12;
$post['B'] = 8;

$OrigA = 12;
$OrigB = 14;

$fields = array ("A", "B");

$querystr = "INSERT INTO tblEditHist VALUES ";
foreach ($fields as $field) {
    $origVarName = 'Orig'.$field;
    echo '$$origVarName = '.$$origVarName.'<br/>';
    echo '$post[$field] = '.$post[$field].'<br/>';
    $fieldname = "app" . $field;

    if ($$origVarName != $post[$field]) { 

        echo $field.' NOT EQUAL!';
    } else {
       echo $field.' EQUAL!';
     }
}

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.