2016-10-17 10 views
1

私は助けが必要です。 PHPとMySQLを使用して、Jsonオブジェクトの値ごとに複数のデータをテーブルに挿入する必要があります。私は以下のコードを説明しています。PHPとMySQLを使用してJsonオブジェクト値ごとにテーブルにデータを挿入する方法

$commnt=[{ 
    'day_id':2, 
    'comment':'vodka1' 
},{ 
    'day_id':3, 
    'comment':'vodka2' 
} 
] 

$result=[{ 
    'day_id':1, 
    'restaurant':'193' 
},{ 
    'day_id':2, 
    'restaurant':'193' 
},{ 
    'day_id':3, 
    'restaurant':'193' 
} 
] 

は、ここで私はday_idごとにこの下の表にJSONオブジェクトの両方からすべてのデータを入力する必要があります。私は下の表の列を説明しています。

db_details:

id day_id restaurant comment 

ここに私の要件は、day_idが同じテーブルにそれぞれのcommentフィールド値の意志エントリは他の賢明comment提出値がblank.Theはプットが与えられて期待ままになるときであります以下。

id day_id restaurant comment 

1  1  193    

2  2  193   vodka1 

3  3  193   vodka3 

私の質問は以下の通りです。

$insertintodetails=mysqli_query($connect,'INSERT INTO db_details 
(day_id,restaurant,comment) values ("'. $result.'","'.$result[$i]['restaurant'].'","'.$commnt[$i]['comment'].'")'); 

JSONオブジェクトの長さの両方が同じでも異なっていてもよいが、コメントは、それ以外の場合は空白のままになりますday_idごとに挿入する必要がありますようにここでは、多くのユースケースがあるかもしれません。私の質問では、私は必要に応じて挿入することはできません。この問題を解決するのを手伝ってください。

+0

$ commntは配列ではなく、そのjsonオブジェクト –

+0

はあなたの配列です() – Karthi

+0

残念ながら、私の更新された投稿をチェックしてください。 – subhra

答えて

0

私は上記の質問の詳細から例を作成します。以下のコードを試してみてください。そして、それはあなたのために働くはずです...

//For example taken array and convert it to JSON  
$comment = json_encode(array(array('day_id' => '2', 'comment' => 'vodka1'), array('day_id' => '3', 'comment' => 'vodka2'))); 
$result = json_encode(array(array('day_id' => '1', 'restaurant' => '193'), array('day_id' => '2', 'restaurant' => '193'), array('day_id' => '3', 'restaurant' => '193'))); 

//Convert JSON to array... 
$arrComment = json_decode($comment, true); 
$arrResult = json_decode($result, true); 

foreach($arrResult AS $keyResult => $dataResult){ 
    $day_id = $dataResult['day_id'];//day_id 
    $restaurant = $dataResult['restaurant'];//rasturant 
    $strComment = '';//comment 
    //Check and extract comment value from multi dimensional comment($arrComment) array... 
    if($getComment = find_comment_with_dayid($arrComment, $day_id)){ 
     $strComment = $getComment; 
    } 

    //Insert records... 
    $insertintodetails=mysqli_query($connect,'INSERT INTO db_details 
(day_id, restaurant, comment) values ("'. $day_id .'","'. $restaurant .'","'. $strComment .'")'); 
} 


//Function will return comment for matched day_id 
function find_comment_with_dayid($arrComment, $fieldKey) { 
    foreach($arrComment as $indCommenr => $dataComment) { 
     //Check for day_id, matched then return comment... 
     if($dataComment['day_id'] == $fieldKey) return $dataComment['comment']; 
    } 
    return FALSE; 
} 

ここでは、あなたの質問で報告されたJSONの問題を避けるための例の配列を取った。この仕事がうまくいきたい!

+0

いいえ、正常に動作しています。 '{" day_id ":" 2 "、" restaurant ":" 193 "、" comment ":"}}は空白のコメント値で来ています。 – subhra

+0

はい、私は自分の答えを変えます。これはあなたのために動作するはずです... –

+0

NOwは正常に動作しています。 – subhra

0
$newArray = $insertintodetails = array(); 
foreach($result as $rs) 
{ 
    $newArray[$rs->day_id] = [ 
    "`day_id`=>'{$rs->day_id}'", 
    "`restaurant`=>'{$rs->restaurant}'" 
]; 
} 
foreach($commnt as $rs){ 
    $newArray[$rs->day_id][] = "`comment`='{$rs->comment}'"; 
} 
foreach($newArray as $rs){ 
    $insertintodetails[]=mysqli_query($connect,'INSERT INTO db_details SET '.implode(',',$rs)); 
} 
+0

私はテストしてお知らせします。 – subhra

+0

@ hackout:あなたのコードにはforeachに '{}'がありません。 – subhra

+0

うん、しかし、その仕事:D – hackout

関連する問題