2011-04-21 119 views
0

如何从下面的代码获取数组值?并使用while loop来保存那些不为空的数据。 不太清楚需要从代码中添加什么。将数组中的值插入到SQL查询中

if(!empty($bookslot['t_deep'])) : 
    $tw_deep = 'deep_wrap'; 
elseif(!empty($bookslot['t_eyelashes'])) : 
    $tw_eyelashes = 'eyeLashes_wrap'; 
elseif(!empty($bookslot['t_holistic'])) : 
    $tw_holistic = 'holistic_wrap'; 
elseif(!empty($bookslot['t_male_nail'])) : 
    $tw_male_nail = 'male_nail_wrap'; 
endif; 

$arr[] = array($tw_deep, $tw_eyelashes, $tw_holistic, $tw_male_nail); 

$db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) VALUES('$arr', '1','$id_bookslot', '$b_ref')"); 
在treatment_group

,阵列返回Array instead of 'deep_wrap, etch..

期待为数据库:

Array   1 123 abc // <-- result above code 
-------------------------------------------------------- 
    deep_wrap  1 123 abc 
    holistic_wrap 2 123 abc 

回答

1

你想做的事是这样的(我认为):

$arr = array($tw_deep, $tw_eyelashes, $tw_holistic, $tw_male_nail); 
$arrPlode = implode(', ', $arr); 

$db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
        VALUES('$arrPlode', '1','$id_bookslot', '$b_ref')"); 

这里是一个查询回声演示:http://codepad.org/3NgGu1Tm

OR

作出新的查询每个if:

if(!empty($bookslot['t_deep'])) { 
    $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
     VALUES('deep_wrap', '1','$id_bookslot', '$b_ref')"); 
} 
if(!empty($bookslot['t_eyelashes'])) { 
    $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
     VALUES('eyeLashes_wrap', '1','$id_bookslot', '$b_ref')"); 
} 
if(!empty($bookslot['t_holistic'])) { 
    $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
     VALUES('holistic_wrap', '1','$id_bookslot', '$b_ref')"); 
} 
if(!empty($bookslot['t_male_nail'])) { 
    $db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) 
     VALUES('male_nail_wrap', '1','$id_bookslot', '$b_ref')"); 
} 

或连接查询:

$concat = array(); 

if(!empty($bookslot['t_deep'])) { 
    $concat[] = "('deep_wrap', '1','$id_bookslot', '$b_ref')"; 
} 
if(!empty($bookslot['t_eyelashes'])) { 
    $concat[] = "('eyeLashes_wrap', '1','$id_bookslot', '$b_ref')"; 
} 
if(!empty($bookslot['t_holistic'])) { 
    $concat[] = "('holistic_wrap', '1','$id_bookslot', '$b_ref')"; 
} 
if(!empty($bookslot['t_male_nail'])) { 
    $concat[] = "('male_nail_wrap', '1','$id_bookslot', '$b_ref')"; 
} 

$db->query(build_insert_query('checked_treatments', 
     'treatment_group, id_treatment, id_booking, b_ref', $concat)); 


function build_insert_query($table, $cols, $values){ 
     $return = "INSERT INTO $table ($cols) VALUES"; 
     $val_length = count($values); 
     foreach($values as $key=>$val){ 
      $return .= $val; 
      if($key < ($val_length-1)){ $return .= ", "; } 
     } 
     return $return; 
} 

这里是假冒伪劣值的演示:http://codepad.org/ox4kG43b

+0

@Neal:thanks :)但我想结果存储在每一行,而不是一行。 – tonoslfx 2011-04-21 14:29:18

+0

@boyee比为什么把它存入数组? – Neal 2011-04-21 14:30:55

+0

@Neal:也许你有另一种解决方案,请分享:) – tonoslfx 2011-04-21 14:32:15

0

滴在声明的方括号是。你是一个3维数组,否则

$arr = array($tw_deep, $tw_eyelashes, $tw_holistic, $tw_male_nail); 

而在你的查询,你仍然需要数组取消引用通过指定索引来获取值

$db->query("INSERT INTO checked_treatments (treatment_group, id_treatment, id_booking, b_ref) VALUES('$arr[0]', '1','$id_bookslot', '$b_ref')"); 

索引0将返回的值$tw_deep