2013-04-20 38 views
-1

當前正在與朋友一起處理服務腳本。當我說服務腳本時,我的意思是它只會用於更新和向數據庫添加值。現在我的朋友已經把她所有的數據存儲爲數組了。多個數組 - 將值插入數據庫

我已經成功地進入了其中一個數組,現在我很困惑我該怎麼做多個數組。因此,總而言之,這是一個關於如何將當前代碼重構爲更具動態性的問題。

另外請記住,她有數百個數組,這需要做到,因此它不像2或3它的字面數百。

我更新的代碼:(請閱讀評論)

$colors['Colors_All'] = array("Black","Charcoal"); // Add unique indexes 
$colors['Colors_Bright_All'] = array("Silver","White"); // Add unique indexes 

$AllArrays = get_defined_vars(); // Get all defined vars 
$Arrays = array(); // Set a default array 

foreach ($AllArrays as $varName => $value) { // Run through all the variables set in AllArrays 
     if(is_array($value) && $varName == 'colors') { // If array is colors then 
       $Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array 
     } 
} 

var_dump($Arrays); 

$sql = "INSERT INTO `product_features` ("; // Create the initial query 

foreach ($Arrays as $column => $value) { // ForEach over the array 
     $sql .= "$column,"; // Use the Key Example : 'Colors_All and Color_Bright_All' as a column name 
} 

$sql2 = rtrim($sql, ","); // trim the initial "," from the columns at the end 
$sql2 .= ")"; // Close off the columns names 
$sql2 .= " VALUES "; 

foreach ($Arrays as $column => $value) { // This is where the problem starts -_- 
     foreach ($value as $key => $insert) { // Get the value 
       $sql2 .= "('$insert', '$insert'),"; // I need to have unique values here :(
     } 
} 

$finSQL = rtrim($sql2, ","); // Strip off the remaining "," 

而且我知道我不是綁定的參數,我一旦我得到的實際硬的東西出路。

現在做$ finSQL轉儲,當我得到這個:

string(152) "INSERT INTO `product_features` (Colors_All,Colors_Bright_All) VALUES ('Black', 'Black'),('Charcoal', 'Charcoal'),('Silver', 'Silver'),('White', 'White')" 

怎樣纔可以有獨特的價值在我插入查詢中的值?這是令我困惑的最後一部分。

+0

你有很多定義的變量在PHP腳本中定義?我不會推薦它,但如果它們不遵循一個明確的模式,你可以嘗試使用'get_defined_vars()'。你能夠展示更多的代碼嗎? – calcinai 2013-04-20 05:04:49

+0

你可以重新輸入你剛輸入的內容嗎? PHP文檔中定義了一堆定義的變量?我會看看get_defined_vars() – 2013-04-20 05:05:44

+1

你似乎缺乏將各種顏色鏈接在一起的明智鑰匙。如果你解釋了最終數據庫應該是什麼樣子,這將有所幫助。 – 2013-04-20 05:14:03

回答

0

嗯,我知道了「做」的序列化每一個陣列,通過實際的數組索引作爲唯一標識符,像這樣的數據庫:

<?php 
ini_set("error_reporting", E_ALL); 
ini_set("display_errors", TRUE); 

$conn = new MysqlI("HOST", "USER", "PASSWORD", "DATABASE" /*Optionally put the PORT NUMBER HERE : , 3306 */);  
$conn->set_charset('utf8'); // Always explicitly state the character encoding! 

$clearSQL = "TRUNCATE TABLE `product_features`;"; 
$clearNOW = $conn->prepare($clearSQL);// For updating this will delete all records in the table 
$clearNOW->execute(); 

$colors['Colors_All'] = array("Black", "Charcoal", "Pink", "Cheese", "Dog", "Punk"); // Add unique indexes 
$colors['Colors_Bright'] = array("Silver", "White", "Yellow", "Blue", "Indigo"); // Add unique indexes 
$colors['Colors_Bright_All'] = array("Silver", "White", "DarkYellow", "Dark_Pink",); // Add unique indexes 
$colors['Colors_Dark'] = array("Silver", "White", "DarkWE", "DarkCheese", "DarkBla"); // Add unique indexes 
$colors['Colors'] = array("Silver", "White", "DarkIDK", "DarKsome", "waDark", "dark"); // Add unique indexes 
$colors['Colors_Green'] = array("Silver", "White", "WAA", "WIWIW", "OMG", "Blood"); // Add unique indexes 
$colors['Colors_Red'] = array("Silver", "White", "IRI", "owow", "Darkness", "night"); // Add unique indexes 

$AllArrays = get_defined_vars(); // Get all defined vars 
$Arrays = array(); // Set a default array 

foreach ($AllArrays as $varName => $value) { // Run through all the variables set in AllArrays 
    if (is_array($value) && $varName == 'colors') { // If array is colors then 
     $Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array 
    } 
} 

$sql = "INSERT INTO `product_features` (`ArrayIndex`, `ArrayValues`) VALUES "; // Create the initial query 

foreach ($Arrays as $ArrayIndex => $Arrayvalues) { // ForEach over the array 
    $sql .= "('$ArrayIndex','" . $mysqli->mysqli_real_escape_string(serialize($Arrayvalues)) . "'),"; // Use Array Index as the Unique Array Identifyer and Arrayvalues for the value 
} 

$finSQL = rtrim($sql, ","); // Strip off the remaining "," 

var_dump($finSQL); // Check the query 

$INSERT = $conn->prepare($finSQL); // Get query ready 
$INSERT->execute(); // Execute 

?> 

與數據庫中的結果是this

+0

all這個'$ AllArrays'的東西**完全沒用。**你已經得到**完全相同的'$ colors'數組**。將序列化數組存儲在數據庫中是沒有意義的。 – 2013-04-24 12:32:04

+0

誰在乎你對什麼有意義? - 你是誰?噢......這個工作,我取得了成功。我只是回答我的回答,因爲你和別人說什麼都是毫無用處的。所以請在你的生活中做點什麼。 - 謝謝。 – 2013-04-24 20:54:16

+0

如果你有更好的方法來做到這一點,那麼請告訴。這不是我的問題,我不會將數組存儲在數據庫中,但是我遇到了一位SO成員遇到的問題,並且我試圖提供幫助。不像其他人 – 2013-04-24 20:58:23

1

把$ Colors_Bright_All,$ Colors_Light_All放到最後一個數組中讓我們說$ finalArray並使用foreach循環遍歷該數組,然後執行你現有的foreach循環。

+0

對,但現在我需要這些值插入到基於數組的數據庫中的不同列中 – 2013-04-20 05:03:24

+0

if name = foo然後插入到else if name = yo然後插入到b中。佛拉。你應該使用單獨的數組。 – Jonast92 2013-04-20 05:13:46

+0

好吧chandresh我可能想出了一個解決方案,但我有一個問題給你。我如何將〜300個陣列組合起來而不必輸入它們? (字面意思是約300陣列) – 2013-04-20 13:29:40

-1
<?php 
function myInsert($myArray) 
{ 
    $colour_value = ""; 
    $stmt = $mysqli->prepare("INSERT INTO `product_features` (Colors_All) VALUES (?)"); 

    if ($stmt) { 
     $stmt->bind_param("s", $colour_value); 


     foreach ($myArrayas AS $value) { 
      $colour_value = $value; 
      $stmt->execute(); 
     } 

     $stmt->close(); 
    } 
} 


function myInsert($Colors_Bright_All); 
function myInsert($Colors_Light_All); 
?> 

編輯

<?php 
$newArray = array(); 

foreach($_GLOBALS as $val) 
{ 
    if(substr($val, 0, 7) == "Colors_") 
     myInsert($val); 
} 

?> 
+0

所以100個+陣列功能......是啊。 ..不,你剛剛做了什麼,我已經擁有了? – 2013-04-20 05:26:52

+0

我的文本缺失..是的,我知道他們是相似的,你只是部分地使用你準備的聲明。在你的問題中,你不會說明你從哪裏得到數組。你只需要在每個數組的循環中調用該函數。 – Bradmage 2013-04-20 05:29:41

+0

要有100 +陣列我不得不從數據庫中假設?或者他們是硬編碼的?你只需使用foreach($ result as $ data)myInsert($ data); – Bradmage 2013-04-20 05:31:28

0

作爲解決您上述問題的請嘗試執行下面的代碼片段

例如

$finalarray=array(); 

    $Colors_Bright_All = array("Silver","White","Gold","Royal_Blue","Dodger_Blue","Deep_Sky_Blue","Deep_Green","Forest_Green","Bright_Green","Violet"); 

    $Colors_Light_All = array("Light_Gray","Silver","White","Gold","Dodger_Blue","Deep_Sky_Blue","Light_Blue","Bright_Green","LightGreen","Light_Green"); 

array_push($finalarray,'('.join(',',$Colors_Bright_All).')'); 
array_push($finalarray,'('.join(',',$Colors_Light_All).')'); 

    $value=join(',',$finalarray); 

$sql = "INSERT INTO `product_features` (Colors_All) VALUES ".$value; // Build the initial query 
$finSQL = rtrim($sql, ","); // Strip the last , so our mysql query doesn't goof up. 
$stmt = $conn->prepare($finSQL); 
$stmt->execute(); 
0

正如你在其他的答案被告知已,做一個嵌套數組是唯一明智的方法

$colors['bright'] = array("Silver","White","Gold"...); 
$colors['light'] = array("Light_Gray","Silver","White"...); 

foreach ($colors as $type => $array) { 

    // put your code here 

} 

,如果你需要更多的數據只是$type(因爲它不能被旁邊插入從極其模糊的問題告訴了),你可以用這個數據增加一個陣列以及

+0

沒想到這是模糊的......我現在有一段代碼可以工作,我需要那段代碼來動態地將數據從數組添加到數據庫。對不起,如果這是什麼,每一個stumping。 – 2013-04-20 05:36:37

+0

'將數據從數組動態添加到數據庫的代碼' - 您的代碼已經完成了。還有一些其他問題你仍然面臨着人們的隱瞞。 – 2013-04-20 05:39:23

+0

隱藏它? :S我迷路了,如果你這麼想,我很抱歉。當我寫下這個問題時,我認爲它寫得很好,並且重點在於:S – 2013-04-20 05:40:37

0

,如果你的代碼工作正常,然後更改

foreach ($Arrays as $column => $value) { // This is where the problem starts -_- 
     foreach ($value as $key => $insert) { // Get the value 
       $sql2 .= "('$insert', '$insert'),"; // I need to have unique values here :(
     } 
} 

foreach ($Arrays as $column => $value) { // The problem will be fixed :D 
     $sql2 .="("; 
     foreach ($value as $key => $insert) { // Get the value 
       $sql2 .= "'$insert',"; // You will have unique values here :D 
     } 
     $sql2 = chop($sql2,","); // strip the ending , 
     $sql2 .= "),"; 
}