php - Multiple Arrays - Insert Value into database -
currently working on service script friend. when service script mean used updating , adding values database. friend has stored values of database arrays.
i have succeeded @ entering in 1 of arrays confused on how multiple arrays. in more of question on how can refactor current code more dynamic.
also please keep in mind, has hundreds of arrays needs done not 2 or 3 literally hundreds.
my updated code : (please read comments)
$colors['colors_all'] = array("black","charcoal"); // add unique indexes $colors['colors_bright_all'] = array("silver","white"); // add unique indexes $allarrays = get_defined_vars(); // defined vars $arrays = array(); // set default array foreach ($allarrays $varname => $value) { // run through variables set in allarrays if(is_array($value) && $varname == 'colors') { // if array colors $arrays = array_merge($arrays, $value); // merge arrays new array } } var_dump($arrays); $sql = "insert `product_features` ("; // create initial query foreach ($arrays $column => $value) { // foreach on array $sql .= "$column,"; // use key example : 'colors_all , color_bright_all' column name } $sql2 = rtrim($sql, ","); // trim initial "," columns @ end $sql2 .= ")"; // close off columns names $sql2 .= " values "; foreach ($arrays $column => $value) { // problem starts -_- foreach ($value $key => $insert) { // value $sql2 .= "('$insert', '$insert'),"; // need have unique values here :( } } $finsql = rtrim($sql2, ","); // strip off remaining ","
also know not binding parameters, once actual hard stuff out way.
now when doing dump of $finsql :
string(152) "insert `product_features` (colors_all,colors_bright_all) values ('black', 'black'),('charcoal', 'charcoal'),('silver', 'silver'),('white', 'white')"
how can have unique values values in insert query? last part of confusing me.
put $colors_bright_all, $colors_light_all 1 final array lets $finalarray , use foreach loop loop through array , perform existing foreach loop in that.
Comments
Post a Comment