0

I am working with a set of variables that i check each time a page loads. I have successfully checked through $_REQUEST and $_SESSION, but am having trouble with the dynamci checking of variables assigned higher int he page, if they were set.

the following is my code:

$important_field = array('treatmentId','category','state','providerId','sellDoc','insuranceName','grossCharge','discount','allowable','patientPortion','insurancePortion','dateOfService','billFileLocation','eobFileLocation','fromTable');
foreach ($important_field as $key) {
    if (!$$key || $$key == "none" || $$key == "" || $$key == NULL) {
        if (!$_REQUEST[$key] || $_REQUEST[$key] == "" || $_REQUEST[$key] == NULL) {
            if (!$_SESSION[$key]) {
                // wow, guess it just wasn't set anywhere...
            } else {
                $user->sell->$key = $_SESSION[$key];
            }
        } else { 
            $user->sell->$key = $_REQUEST[$key]; $_SESSION[$key] = $_REQUEST[$key]; 
        }
    } else {
        $user->sell->$$key = $$key; $_SESSION[$$key] = $$key; 
    }
}

Apparently evaluating $$key does not seem to do what i'm looking for as it never assigns the variable to the session... how should I be evaluating the $key to get the currently set value of say, field $eobFileLocation if it was already set in the PHP prior to the check?

Thanks,

Silver Tiger

Update:

ok, i have the following code, but there's still one bug with it. When i follow my process through af ew of these variables are set on each page and are carried over by the session variable as expected. The issue I am still having is that when i submit a new $_REQUEST variable which SHOULD change the session variable to the new submitted value, the script is finding a local variable ... where is it pulling $key and $$key from that is finding these as a local variable?

$important_field = array('treatmentId','category','state','city','providerId','sellDoc','insuranceName','grossCharge','discount','allowable','patientPortion','insurancePortion','dateOfService','billFileLocation','eobFileLocation','fromTable');
foreach ($important_field as $key) {
    if (isset($$key) && !empty($$key) && $$key != "none") {
        echo "Found local variable for ".$key.", i'll set the session and user to this.<br>\n";
        $user->sell->$key = $$key;
        $_SESSION[$key] = $$key; 
    } elseif (isset($_REQUEST[$key]) && !empty($_REQUEST[$key])) {
        echo "Found submitted form  variable for ".$key.", i'll set the session and user to this.<br>\n";
        $user->sell->$key = $_REQUEST[$key];
        $_SESSION[$key] = $_REQUEST[$key]; 
    } elseif (isset($_SESSION[$key]) && !empty($_SESSION[$key])) {
        echo "Found a session variable ".$key.", i'll set the user to this.<br>\n";
        $user->sell->$key = $_SESSION[$key];
    } else {
        echo "There was no provided data for ".$key."<br>\n";
    }
}

Any ideas why when i load the page it thinks the local (as listed above) is set? does $key and $$key read from $_SESSION['blah']/$_REQUEST['blah'] and think it's just $blah?

6
  • ok ... found the issue - though i thought i had already addressed it. apparently i reverted to old code and didnt catch it .. here's the fix .. don't use $$ everywhere, just where you want the value of the variable .. i.e. user->sell->$key = $$key ... not user->sell->$$key = $$key. Commented Aug 31, 2011 at 8:21
  • Maybe you could give a high-level introduction of what this code is supposed to do in the first place? You're checking if a variable is set locally, else if it's set in $_REQUEST, else if it's set in $_SESSION, then use the first matching value for your $user object? Commented Aug 31, 2011 at 8:22
  • And BTW, this seems like pretty bad data hygiene and confused data flow. If you don't know where a value is and if you have that many cases for "false", including "none", you should first work on internal consistency and data management. Commented Aug 31, 2011 at 8:28
  • im working to make sure this is available mainly in the $_SESSION variable, but shoving it int o the $user just in case as well. I ahve also changed my if statements as seen in the updated post above ... but apparently it still needs work .. Commented Aug 31, 2011 at 8:42
  • deceze - This is a custom page built to interface with PHPBB3, the $user->sell is a set of variables i've created to track sales data. I am using the SESSION to keep the data across pages as I'm not sure when $user gets cleared, though it is defined on each page based on the integration. Commented Aug 31, 2011 at 8:48

4 Answers 4

1

Are you sure this is what you want:

$user->sell->$$key = $$key;

should it not be

$user->sell->$key = $$key;

try using the empty function:

if (empty($$key) || $$key == "none")
if (empty($_REQUEST[$key]))

and the isset function:

if (!isset($_SESSION[$key]))
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to say "i shortened it" ... but i guess i'll change it up a bit .. here's what i had
0

it is

$user->sell->$key = $$key;

not

$user->sell->$$key = $$key;

and

$_SESSION[$$key] 

should be

$_SESSION[$key] 

Comments

0

You're messing with your variables, this is not good. In fact, using two var signs ($$) describes a "variable variable".

$foo = "name";
$$foo = "value";

So you expect the following:

echo $foo; // => "name"
echo $$foo; // => "value"

What you maybe are not expecting, is:

echo $name; // => "value"

So you're better off cleaning up your code first.

Comments

0

Well, I have expressed my concerns about the cleanliness of this approach in the comments. Here's at least some more compact code without variable variables to do the same thing:

$keys = array('treatmentId', 'category', ...)

$values = array_filter(array_merge(
    array_intersect_key($_SESSION, array_flip($keys)),
    array_intersect_key($_REQUEST, array_flip($keys)),
    compact($keys)
), function ($v) { return $v && $v != 'none'; });

$_SESSION = array_merge($_SESSION, $values);
foreach ($values as $key => $value) {
    $user->sell->$key = $value;
}

// For debugging purposes:
//
// $diff = array_diff_key(array_flip($keys), $values);
// if ($diff) {
//    'No values for ' . join(', ', array_keys($diff));
// }

6 Comments

hmm .. i have never even used some of the functions you're talking about there .. I'll have to read up to understand them better because i hate to copy/paste code if I don't know what it's doing... and after thinking about it, as long as i can maintain my session I'll not need the user-> values
@Silvertiger Certainly, read up on these functions! Wholesale array handling, merging and compacting and much preferable over lots of foreachs and repetitive code.
I'd love to test it out, but it fails syntax check on the array_filter command. If I understood it better I'd be able to fix it.
@Silvertiger The array_filter callback uses PHP 5.3 syntax. In PHP 5.2-, you can replace the function () { } part with create_function('$v', 'return $v && $v != "none";').
ugh .. it doesn't like either method
|

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.