0

how do i get the array value from below code?. and use while loop to save the data those are not empty. not quite sure what needs to be added from the code.

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')");

in the treatment_group, the array returns Array instead of 'deep_wrap, etch..

expecting for database :

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

2 Answers 2

1

what you want to do is this (i think):

$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')");

here is a demo with a query echo: http://codepad.org/3NgGu1Tm

OR

make a new query for each 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')");
}

OR CONCATENATE THE QUERIES:

$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;
}

here is a demo of the above with fake values: http://codepad.org/ox4kG43b

Sign up to request clarification or add additional context in comments.

8 Comments

@Neal: thanks:) but i want the result store in each row, not to be one row.
@Neal: perhaps you have another solution, please share :)
@Neal: thanks for the effort :D. i guess thats the only way ;)
or you can do one more thing. give me a minute
@Neal: wow ur star :D .. so i just change the table name here ($table, $cols, $values)?
|
0

drop the square brackets on the declaration to be. You are making a 3 dimensional array otherwise

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

And in your query you still need to dereference the array to get the value by specifying the index

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

An index of 0 will return the value of $tw_deep

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.