user1227914
user1227914

Reputation: 3514

variable inside variable in a PHP function?

I have the following function.

The problem is that the $lang variable will vary depending on the $this->setts['site_lang']; . The actual problem is the following:

$condition_details['description_$lang']

(which isn't working). How do I get this to display

$condition_details['description_us']

or

$condition_details['description_fr']

depending on the $lang setting?

And here is the full function:

function shipped_drop_down($box_name = 'item_shipped_via', $selected = null, $form_refresh = null)
{
    (string) $display_output = null;
    $lang = $this->setts['site_lang'];
    $sql_select_conditions = $this->query("SELECT id, description_".$lang." FROM " . DB_PREFIX . "advanced_shipping_carriers ORDER BY theorder,description_".$lang." ASC");
    $display_output = '<select name="' . $box_name . '" ' . (($form_refresh) ? 'onChange = "submit_form(' . $form_refresh . ', \'\')"' : '') . ' style="font-size:10px; width: 120px;"> ';
    while ($condition_details = $this->fetch_array($sql_select_conditions))
    {
        $display_output .= '<option value="' . $condition_details['id'] . '" ' . (($condition_details['id'] == $selected) ? 'selected' : '') . '>' . $condition_details['description_$lang'] . '</option> ';
    }
    $display_output .= '</select> ';
    return $display_output;
}

Upvotes: 1

Views: 79

Answers (4)

SenorAmor
SenorAmor

Reputation: 3345

Use double quotes like so:

$condition_details["description_$lang"]

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You could do

$condition_details['description_'.$lang]

or use "

$condition_details["description_$lang"]

Upvotes: 5

NeeL
NeeL

Reputation: 720

You could store your lang settings in arrays like this :

$lang['fr']['condition_details']

so you could use $lang[$selected_lang]['condition_details']

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Use "double quotes" instead of 'single quotes'. That way the $lang will be parsed and replaced with the relevant value.

Upvotes: 1

Related Questions