2014-11-14 52 views
0

我想用單引號PHP變量,因爲,我不能在我的JQUERY插件訪問雙引號變量(X-Editable如何附上變量單引號PHP

PHP

$countries = array(); 
foreach($countries as $c){ 
    $country_id = $c['country_id']; 
    $short_name = $c['short_name']; 
    array_push($countries, array('value' => '$country_id', 'text' => '$short_name')); <-- I want something like this... 
} 

我不能用:

array('value' => $country_id, 'text' => $short_name) OR 
array('value' => "$country_id", 'text' => "$short_name") 

JQUERY

$('#cus_country').editable({ 
type: 'select', 
pk: '1', 
url: '/user/inline_edit', 
title: 'Enter Country', 
source: '<?=json_encode($countries)?>', 
display: function(value) { 
    if (value !== "Add Country"){ 
     $(this).html('<i class="fa fa-pencil"></i>' + value); 
    } 
    }, 
success: function(response, newValue) { 
    //alert(response); 
    if(response.status == 'error') return response.msg; 
} 
}); 

編輯 當我使用靜態值談到在下拉菜單中沒有任何錯誤。但是當我把變量放在數組中時,就會出現問題。

例如:

CASE # 1 
$countries = array('value'=>'some', 'text'=>'thing'); 
output of this variable: 
Array([0]=>Array([value]=>some [text]=>thing)) 

CASE # 2 
$countries = array('value'=>$some, 'text'=>$thing); 
output of this variable: 
Array([0]=>Array([value]=>some [text]=>thing)) 

正如你所看到的,這兩種情況都給予我同樣的輸出。但是,第一種情況會生成下拉菜單,第二種情況不會。

+0

如果您無法使用雙引號訪問jQuery中的變量,請嘗試轉義它們。你必須告訴我們你想用哪個引號。 – 2014-11-14 21:17:21

+0

@ Fred-ii-:請你詳細說明一下嗎?我不知道逃跑。 – 2014-11-14 21:18:31

+0

就像'\「$ var \」' – 2014-11-14 21:18:52

回答

0

我發現了這個問題。

這是不正確的變量形式。在數據庫中,變量以這種格式存儲:People's thing等。

感謝@jonathan Kuhn的幫助。

+0

@jonathan謝謝 – 2015-02-07 20:03:06

0
array_push($countries, array('value' => "'.$country_id.'", 'text' => '$short_name')); 

你必須使用。 。語法在PHP中定義去變量的地方。通過使用'$test',您不是使用變量,而是使用美元符號和字母測試的字符串。