html - How to insert dynamic array into the database in php? -


i working on dynamic array need insert these array in database.when insert dynamic array database instead of displaying in 1 row cover 3 row.

coding insertion

this array  $asma[]=ga::select($ga->population,'total',3); 

this insertion query need insert detail in 6 column output comprises of 6 values .

    $voltage = array();     $duration = array();     $number = array();      foreach($asma $key => $value)      {         foreach ( $value $ind => $hobject )         {           $voltage[] = $hobject->voltage;           $duration[] = $hobject->duration;           $number[] = $hobject->number;                     } }// endforeach     for(i=0;i<row_count;i++)     {     $q = "insert ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) values (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";     $qs = sprintf( $q, $voltage[$i],$duration[$i],$number[$i],                         date("y-m-d h:i:s") );     $result = mysql_query($qs);     if ( ! $result ) {          die( 'insert failed ' . mysql_errno() . ' ' . mysql_error() );                     }}} ?> 

i need store 6 values in one row if user select 1 option. if user select two option two rows cover in database. here output

array (     [0] => h object         (             [voltage] => 18             [number] => 1             [duration] => 6         )      [1] => h object         (             [voltage] => 38             [number] => 4             [duration] => 14         )      [2] => h object         (             [voltage] => 38             [number] => 4             [duration] => 14         ) 

it store in database this

volatge     duration number volatge duration number volatge duration      number 18           6       1       18      6        1      18        6        1 38          14       4       38      14       4      38       14        4 38          14       4       38      14       4      38       14        4 

i need store this

 volatge     duration number volatge duration number volatge duration      number   18           6        1      38      4       14      38     14             4 

plz in this

you need move query out of foreach loop. here 1 way -

$voltage = array(); $duration = array(); $number = array();  foreach($asma $key => $value)  {     foreach ( $value $ind => $hobject ) {       $voltage[] = $hobject->voltage;       $duration[] = $hobject->duration;       $number[] = $hobject->number;               ,    }  } // endforeach $q = "insert ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) values (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')"; $qs = sprintf( $q, $voltage[0],$duration[0],$number[0],                    $voltage[1],$duration[1],$number[1],                    $voltage[2],$duration[2],$number[2],                    date("y-m-d h:i:s") ); $result = mysql_query($qs); if ( ! $result ) {      die( 'insert failed ' . mysql_errno() . ' ' . mysql_error() );                 } 

edit

to add second row, can change (note, don't see defining select 1/2, put $user_select) -

$voltage = array(); $duration = array(); $number = array();  foreach($asma $key => $value)  {     foreach ( $value $ind => $hobject ) {       $voltage[] = $hobject->voltage;       $duration[] = $hobject->duration;       $number[] = $hobject->number;               ,    }  } // endforeach $q = "insert ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) values (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')"; $qs = sprintf( $q, $voltage[0],$duration[0],$number[0],                    $voltage[1],$duration[1],$number[1],                    $voltage[2],$duration[2],$number[2],                    date("y-m-d h:i:s") ); if($user_select==2){  // if user select 2     $q = ", (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";     $qs = sprintf( $qs.$q, $voltage[3],$duration[3],$number[3],                              $voltage[4],$duration[4],$number[4],                              $voltage[5],$duration[5],$number[5],                              date("y-m-d h:i:s") ); } $result = mysql_query($qs); if ( ! $result ) {      die( 'insert failed ' . mysql_errno() . ' ' . mysql_error() );                 } 

edit #2
here version. loop not valid php for(i=0;i<row_count;i++). each of should have $ - for($i=0;$i<$row_count;$i++). also, sprintf has wrong amount of variables. in $q have 9 %d, in $qs giving 3. expect other 6 come from. in have changed loop increase 3 - for($i=0;$i<$row_count;$i+=3), , used $voltage[$i+1], $duration[$i+1], $number[$i+1], $voltage[$i+2], $duration[$i+2], $number[$i+2], fill out remaining 6 %ds

$voltage = array(); $duration = array(); $number = array();  foreach($asma $key => $value)  {     foreach ( $value $ind => $hobject )     {       $voltage[] = $hobject->voltage;       $duration[] = $hobject->duration;       $number[] = $hobject->number;                 } }// endforeach for($i=0;$i<$row_count;$i+=3) {    $q = "insert ga (fe, fe1, fe2,fe3,fe4,fe5,fe6,f7,f8, timestamp,username ) values (%d, %d, %d,%d, %d, %d,%d, %d, %d, '%s' ,'$login_session')";   $qs = sprintf( $q, $voltage[$i],$duration[$i],$number[$i],                      $voltage[$i+1],$duration[$i+1],$number[$i+1],                      $voltage[$i+2],$duration[$i+3],$number[$i+3],                     date("y-m-d h:i:s") );   $result = mysql_query($qs);   if ( ! $result ) {      die( 'insert failed ' . mysql_errno() . ' ' . mysql_error() );                  }}} ?> 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -