i am working on dynamic array i need to insert these array in the database.when i insert dynamic array into the database instead of displaying in one row its cover three row.
coding for insertion
this is my array
$asma[]=GA::select($ga->population,'total',3);
this is insertion query i need to insert the detail in six column as my output comprises of six values .
$Voltage = array();
$Duration = array();
$Number = array();
foreach($asma as $key => $value)
{
foreach ( $value as $ind => $hObject )
{
$Voltage[] = $hObject->Voltage;
$Duration[] = $hObject->Duration;
$Number[] = $hObject->Number;
} }// endforeach
for(i=0;i<row_count;i++)
{
$q = "INSERT INTO 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 to store six values in one
row if user select 1
from the option. if user select two
from the option then two
rows cover in the database.
here is my 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 is store in the database like 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 to store like this
Volatge Duration Number Volatge Duration Number Volatge Duration Number
18 6 1 38 4 14 38 14 4
plz help my in this
You need to move your query out of the foreach loop. Here is one way to do it -
$Voltage = array();
$Duration = array();
$Number = array();
foreach($asma as $key => $value)
{
foreach ( $value as $ind => $hObject ) {
$Voltage[] = $hObject->Voltage;
$Duration[] = $hObject->Duration;
$Number[] = $hObject->Number; ,
}
} // endforeach
$q = "INSERT INTO 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 a second row, you can change it to something like this (note, I don't see where you are defining the select for 1/2, so I just put $user_select
) -
$Voltage = array();
$Duration = array();
$Number = array();
foreach($asma as $key => $value)
{
foreach ( $value as $ind => $hObject ) {
$Voltage[] = $hObject->Voltage;
$Duration[] = $hObject->Duration;
$Number[] = $hObject->Number; ,
}
} // endforeach
$q = "INSERT INTO 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 is another version. Your for loop is not valid php for(i=0;i<row_count;i++)
. each of those should have a $
- for($i=0;$i<$row_count;$i++)
. Also, your sprintf has the wrong amount of variables. In your $q
you have 9 %d
, but in $qs
you only are giving 3. Where do you expect the other 6 to come from. In this I have changed you loop to increase by 3 - for($i=0;$i<$row_count;$i+=3)
, and used $Voltage[$i+1], $Duration[$i+1], $Number[$i+1], $Voltage[$i+2], $Duration[$i+2], $Number[$i+2],
to fill out the remaining 6 %d
s
$Voltage = array();
$Duration = array();
$Number = array();
foreach($asma as $key => $value)
{
foreach ( $value as $ind => $hObject )
{
$Voltage[] = $hObject->Voltage;
$Duration[] = $hObject->Duration;
$Number[] = $hObject->Number;
} }// endforeach
for($i=0;$i<$row_count;$i+=3)
{
$q = "INSERT INTO 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() );
}}}
?>
<?php
//code not tested check it
//Logic is changed instead of for looping many times
$data = array();
$j = 0;
foreach($asma as $key => $value)
{
$i = 0;
foreach ( $value as $ind => $hObject )
{
if($i==0)
{
$data[$j]['fe'] = mysql_escape_string($hObject->Voltage);
}else{
$data[$j]['fe'.$i] = mysql_escape_string($hObject->Voltage);
}
$i++;
$data[$j]['fe'.$i] = mysql_escape_string($hObject->Duration);
$i++;
$data[$j]['fe'.$i] = mysql_escape_string($hObject->Number);
$i++;
}
$j++;
}// endforeach
//multiple array
foreach($data as $array)
{
//unique array
//$array3 = array_merge($Voltage,$Duration,$Number);
$fields = implode(',',array_keys($array));
//if you want append any new field append it
$fields .= ','.'timestamp,username';
$vals = "'".implode("','",array_values($array))."'";
//if you want append any new values append it
$vals .= ",'".time()."','".$login_session."'";
$q = "INSERT INTO ga (".$fields.") VALUES(".$vals.")";
$result = mysql_query($q);
if ( ! $result ) {
die( 'Insert failed ' . mysql_errno() . ' ' . mysql_error() );
}
}