0

I am running PHP with CodeIgniter. My application needs to output formatted addresses throughout the site, pulled from MySQL. In some cases, address_1 may be blank or may be filled, in other cases postcode may or may not be there, etc. I have written a helper function that formats these addresses neatly, when passed a MySQL result set.

The issue is that due to the size and complexity of the site, in some places I need to use this function with multiple addresses in the same result set, and in other places I may pass the results as $query->row('address_1') or as $row->address_1.

I have written my function out like this:

// Get every element from the MySQL Resultset       
            if(method_exists($address, 'row'))
            {
                $a['abode']         = ($address->row($prefix.'abode')         ? $address->row($prefix.'abode') : '');
                $a['building_no']   = ($address->row($prefix.'building_no')   ? $address->row($prefix.'building_no') : ''); 
                $a['building_name'] = ($address->row($prefix.'building_name') ? $address->row($prefix.'building_name') : '');
                $a['office_number'] = ($address->row($prefix.'office_number') ? $address->row($prefix.'office_number') : '');
                $a['address_1']     = ($address->row($prefix.'address_1')     ? $address->row($prefix.'address_1') : '');
                $a['address_2']     = ($address->row($prefix.'address_2')     ? $address->row($prefix.'address_2') : '');
                $a['town']          = ($address->row($prefix.'town')          ? $address->row($prefix.'town') : '');
                $a['county']        = ($address->row($prefix.'county')        ? $address->row($prefix.'county') : '');
                $a['postcode']      = ($address->row($prefix.'postcode')      ? $address->row($prefix.'postcode') : '');
                $a['country']       = ($address->row($prefix.'country')       ? $address->row($prefix.'country') : '');
            }
            else
            {
                // PHP Variable Variables
                $a['abode']         = (isset($address->{$prefix.'abode'})           ? $address->{$prefix.'abode'}: '');     
                $a['building_no']   = (isset($address->{$prefix.'building_no'})     ? $address->{$prefix.'building_no'} : '');
                $a['building_name'] = (isset($address->{$prefix.'building_name'})   ? $address->{$prefix.'building_name'} : '');
                $a['office_number'] = (isset($address->{$prefix.'office_number'})   ? $address->{$prefix.'office_number'} : '');
                $a['address_1']     = (isset($address->${$prefix.'address_1'})      ? $address->${$prefix.'address_1'} : '');
                $a['address_2']     = (isset($address->{$prefix.'address_2'})       ? $address->{$prefix.'address_2'} : ''); 
                $a['town']          = (isset($address->{$prefix.'town'})            ? $address->{$prefix.'town'} : ''); 
                $a['county']        = (isset($address->{$prefix.'county'})          ? $address->{$prefix.'county'} : ''); 
                $a['postcode']      = (isset($address->{$prefix.'postcode'})        ? $address->{$prefix.'postcode'} : ''); 
                $a['country']       = (isset($address->{$prefix.'country'})         ? $address->{$prefix.'country'} : '');
            }

As you can see I was 'experimenting' a little on $a['address_1'] of the else statement trying to find a combination that worked.

I am trying to get the else statement case to work, but PHP throws no errors or anything except for the $a['address_1'] line, in which it says:

Message: Undefined variable: app_address_1, Filename: helpers/address_helper.php.

My $prefix variable is set to app_, but this isn't working. Why?

2
  • $address->{$prefix.'address_1'} should work just fine. Maybe it just doesn't have a address_1 property. Try to use var_dump($address) to see what's in it. Commented Dec 7, 2011 at 19:55
  • var_dump($address); to help you debug your code. Commented Dec 7, 2011 at 19:57

1 Answer 1

1

Look carefully at that line:

$a['address_1']     = (isset($address->${$prefix.'address_1'})      ? $address->${$prefix.'address_1'} : '');

Do you see what I see?

$address->${$prefix.'address_1'} 
----------^

You have an excess $ in there! The error is because it's trying to find the value of a variable named app_address_1, which is the result of $prefix.'address_1'.

Let's evaluate step-by-step:

$address->${$prefix.'address_1'}
$address->${'app_address_1'}
$address->{$app_address_1}
$address->{NULL}
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.