0

I have form submission that redirects based on the results of a survey. On the landing page, I call a function to process query string, query the database and return results as an array for in-page processing.

function surveyResults() {
 if($goodtogo) {
 $survey = $wpdb->get_results(...,ARRAY_A);
 $name_has_space = strpos(trim($q_name_field[0]),' ');
 if($name_has_space === false) {
 $q_first_name = $q_name_field[0];
 $name_has_num = preg_match('/[0-9]/',$q_first_name);
 $q_first_name = ((0 === $name_has_num) ? " ".ucfirst($q_first_name).", " : '');
 } else {
 $q_first_name = substr(trim($q_name_field[0]),0,$name_has_space);
 $name_has_num = preg_match('/[0-9]/',$q_first_name);
 $q_first_name = ((0 === $name_has_num) ? " ".ucfirst($q_first_name).", " : '');
 }
 $survey['name']['q_fname'] = $q_first_name;
 $results = $survey;
 } else {
 $results = false;
 }
 return $results;
}

Output:

Array (
 [0]=> Array (
 'key' => 'value'
 )
 ...
 [n]=> Array (
 'key' => 'value'
 )
 ['name'] => Array (
 [q_fname] => MyName
 )
)

Which is perfect – except – each time I test the page, the $survey[0-n] results change as queried, but the $survey['name']['q_fname'] still holds the previous value MyName.

I have tried adding unset($survey['name']['q_fname']); immediately after setting $results = $survey; but that doesn't seem to make a difference. Do I need to unset($results) or use a reference &$fname...

What am I missing here?

Thanks

asked Oct 10, 2013 at 22:04
3
  • 1
    PHP variables don't hold their values from one run to the next, everything starts fresh each time you load the page. So something else must be going on. Post your actual code rather than pseudo-code. Commented Oct 10, 2013 at 22:11
  • Updated the code above, which I originally thought might be the issue, but don't understand how this could make $survey['name']['q_fname'] = $q_first_name; be persistent. Commented Oct 11, 2013 at 13:27
  • I tested the following to determine output: if($name_has_space === 0) { $q_first_name = '0'; } else { $q_first_name = 'false'; } $name_has_num = preg_match('/[0-9]/',$q_first_name); if(0 === $name_has_num) { $q_first_name = '0'; } else { $q_first_name = $name_has_num; } Tested with and w/o spaces and numbers and the output changed, but as soon I went back to the original code, the previous value was displayed. Double checked the database and no such record, so I'm completely baffled. Commented Oct 11, 2013 at 13:44

1 Answer 1

1

I'm macgregor, and I'm an idiot. Missed a critical piece of condition in the query.

answered Oct 11, 2013 at 15:19
Sign up to request clarification or add additional context in comments.

1 Comment

I fully endorse this solution.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.