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?
$address->{$prefix.'address_1'}should work just fine. Maybe it just doesn't have aaddress_1property. Try to usevar_dump($address)to see what's in it.