2014-09-05 68 views
0

我想插入多個原始數據庫插件激活,並設法只插入一個原始數據。
我可以弄清楚如何添加多個原始的例如10原料。WordPress插件激活插入多個原始碼

下面是將一個原始的工作代碼只:

function my_func() { 
global $wpdb; 
$_IL_TABLE_SETTINGS = $wpdb->prefix . "table"; 

$wpdb->insert(
    $_IL_TABLE_SETTINGS, 
    array('id' => '1', 'options' => 'something', 'values' => 'something')); 
} 

我想這樣,但不工作的第二原料:

function my_func() { 
    global $wpdb; 
    $_IL_TABLE_SETTINGS = $wpdb->prefix . "table"; 

    $wpdb->insert(
    $_IL_TABLE_SETTINGS, 
    array('id' => '1', 'options' => 'something', 'values' => 'something'), 
    array('id' => '2', 'options' => 'something', 'values' => 'something') 
); 
} 

如何,我可以添加這個值代入您的代碼:

$id = array('1', '2', '3'); 
$op = array('first', 'second', 'thaird'); 
$data = array('value1', 'value2', 'value3'); 

我將不勝感激任何幫助。

+0

你是說排? – Mike 2014-09-05 09:09:11

+0

是的行,對不起,我解決了我的拼寫錯誤@mikemike – ionluchian 2014-09-05 09:10:25

+0

只是嘗試任何循環 – Dinesh 2014-09-05 09:10:43

回答

1
 Here is the code which will work. 

     $id = array('1', '2', '3'); 
     $op = array('first', 'second', 'thaird'); 
     $data = array('value1', 'value2', 'value3');  


     for($i=0;$i<count($id);$i++) 
     { 
      $wpdb->insert($_IL_TABLE_SETTINGS,array("id"=>$id[$i],"option"=>$op[$i],"values"=>$data[$i]));   
     } 

     Please let me know if it didnt work for you. 
0

您可以向其中添加一個循環,並在該循環中放置$ wpdb-> insert。

+0

這就是我無法得到一個工作循環的問題,可以分享我的代碼如何循環? @tristup – ionluchian 2014-09-05 09:11:04

+0

葉當然....只是給我幾分鐘,所以我可以建立它,並分享給你 – Tristup 2014-09-05 09:11:53

1

例如,如果要添加10條記錄,那麼你可以嘗試for循環:

for($i=0;$i<10;$i++) 
    { 
    $wpdb->insert(
    $_IL_TABLE_SETTINGS, 
    array('id' => '$i', 'options' => 'something', 'values' => 'something'), 
    } 
當然,你將需要添加相應的動態數據的

0
If value is stored in an array 

    $arr=array();//containing the values you want to insert 
    $arr[0]=array("id"=>idvalue,"key"=>"value"); 
    for($i=0;$i<count($arr);$i++) 
    { 
     $wpdb->insert($_IL_TABLE_SETTINGS,$arr[$i]));   
    } 


    if you want to store imaginari values 

    for($i=0;$i<count($arr);$i++) 
    { 
     $wpdb->insert($_IL_TABLE_SETTINGS,array('id' => $i, 'options' => 'something', 'values' => 'something'));   
    } 

    Hope you can relate your code with the above one, if no please let me know. 
+0

我有這樣的事情,我可以在你的代碼中使用? $ id = array('1','2','3'); $ option = array('first','second','thaird'); $ value = array('value1','value2','value3'); @tristup – ionluchian 2014-09-05 09:25:34