• 07-09-2021, 10:55:36
    #1
    Merhaba, aşağıda yaptığım bir küçük hata nedeniyle yapmak istediğimi yapamıyorum...

        $user_id = 101;
                $file = "user-".$user_id;
                $db_name = $file;
                    $new = array('sure'=>5, 'catid'=>2,'type'=>"t2", 's'=>"s2", 'amount'=>6);
                    echo UpdateCart($db_name,$new);
                    
       
    
                 function UpdateCart($db_name,$data){
                        $values = jsondb_get($db_name);
       
    
        //$values
         /*
            {"data":
                [
            {"sure":2,"catid":1,"type":"t1","s":"s1","amount":1},
            {"sure":2,"catid":2,"type":"t2","s":"s2","amount":1},
            {"sure":2,"catid":3,"type":"t3","s":"s3","amount":1}
                ]
            }
            */
                    $db_names = $db_name . ".json";
                    $filename = $db_names;
                    //data
                    $n_id = $data['catid'];
                    $n_type = $data['type'];
                    $n_s = $data['s'];
                            # check file
                            if(is_file($filename)){
                                foreach($values['data'] as $value){
                            //id
                            $o_id = $value['catid'];
                            
                            //type
                            $o_type = $value['type'];
                            
                            //s
                            $o_s = $value['s'];
                            
                            //amount
                            $o_split = $value['amount'];
                            $n_split = $data['amount'];
                            $updateAmount =  $value['amount']+$data['amount'];
                        
                            if($o_id == $n_id && $o_type == $n_type && $o_split == $n_split){
                                
                                $values['amount'] = $updateAmount;
                                
                            
                                 $values['data']['amount'] = $updateAmount;
                                    
                                        }
                            } 
                                file_put_contents($db_names, json_encode($values));
                                $filefound = "data is update-".$updateAmount;
                                    } else {
                                $filefound = add_new($data,$db_name);
                                
                            }
                        return $filefound;
                }
    oluşan/varolan dosya dosya...

     {"data":
            [
        {"sure":2,"catid":1,"type":"t1","s":"s1","amount":1},
        {"sure":2,"catid":2,"type":"t2","s":"s2","amount":1},
        {"sure":2,"catid":3,"type":"t3","s":"s3","amount":1}
            ]
        }
    Olması gereken dosya.


     {"data":
            [
        {"sure":2,"catid":1,"type":"t1","s":"s1","amount":1},
        {"sure":2,"catid":2,"type":"t2","s":"s2","amount":7},
        {"sure":2,"catid":3,"type":"t3","s":"s3","amount":1}
            ]
        }
  • 08-09-2021, 19:05:13
    #2
    bayağı bir karştırmışsınız
    şu yaptğım örneği çalıştırıp inceleyin
    <?php
     
    $_temp = array();
    $user_id = 923;
    
    $_temp[] = array(
        'sures' => 25,        
        'vidid' => 8888888888,
        'type' => 'match',
        'tips' => 'kesirler',
        'total' => 25
    ); 
    
    $json_file = dirname(__FILE__).'/new/'.$user_id.'-'.$vidid.'.json';
    if(file_exists($json_file)){
        $json = json_decode(file_get_contents($json_file),true);
        $json = array_merge($json,$_temp);
    }
    file_put_contents($json_file,json_encode($json));
    
    ////
    
    $_temp2 = array();
    $_temp2[] = array(
        'sures' => 225,        
        'vidid' => 28888888888,
        'type' => '2match',
        'tips' => '2kesirler',
        'total' => 225
    ); 
    //hepsinin basina 2 ekleidm
    
    if(file_exists($json_file)){
        $json = json_decode(file_get_contents($json_file),true);
        //dosyadaki json string verilerini alip array a donsturduk
        $json = array_merge($json,$_temp2); 
        //olusturdugumuz _temp2 arrayi ile dosyadaki kayitli arrayimizi birlestirdik
    }
    file_put_contents($json_file,json_encode($json));
    
    print_r(json_decode(file_get_contents($json_file),true));
  • 09-09-2021, 17:59:57
    #3
    İlginiz için çok teşekkür ederim, evet biraz karmaşık bir koddu onu düzenledim, başlığa da düzenlediğimi ekliyorum. Sizin gönderdiğiniz sadece var olan veriye yeni bir veri ekliyor. benim yapmak istediğim aynı id varsa eğer o idi güncellesin.
    kodları güncelledim bir incelerseniz anlarsınız sanırım.

    tekrar teşekkür ederim, ben de r10'a ne olmuş diyordum...