2013-04-14 39 views
-1

我嘗試在if條件中附加我的數據數組。我正在使用array_push函數來完成它第一次使用這個函數來追加數組。我正在使用條件,以便如果用戶在表單中添加了值,它會更新字段,否則它不會影響字段。問題是其更新不及時的數據庫,其無法設置字段,因爲它顯示了在「字段列表」在php/CodeIgniter中追加數組

$fid = 2; 
$password = "test_pass"; 
$title = "new title of folder"; 
$f_access = 1; 
$newName = TRUE; 

$data = array(
       'name' => $title, 
       'access_type' => $f_access 
      ); 

if($newName) 
{ 
    $data2 = "'icon' => $newName"; 
    array_push($data, $data2); 
} 

if($password) 
{ 
    $data3 = "'password' => $password"; 
    array_push($data, $data3); 
} 

$this->db->where('id', $fid); 
$this->db->update('folders', $data); 
+2

你想$ data ['icon'] = $ newName;而不是將此 – ahmad

+0

股票價值推入新數組用於轉換,並使用'array_merge'保留先例索引 –

+1

請閱讀PHP [數組](http://us3.php.net/manual/en/language.types .array.php) –

回答

3

未知未知列「0」到新數據插入到你要的關鍵=>值數組使用以下形式:

$arr['key'] = value; 

$data['password'] = $password; 
0
$data = array(
      'name' => $title, 
      'access_type' => $f_access 
     ); 

if(!empty($newName)) 
    $data['icon'] = $newName; 

if(!empty($password)) 
    $data['password'] = $password; 

這樣你可以很容易地在數組中推新的項目。