如何编写符合条件使用PHP文本文件 [英] How to write a textfile with a condition using PHP

查看:88
本文介绍了如何编写符合条件使用PHP文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在code有问题。我的code是所有有关更新/被发现在删除灌胃文本文件文本文件的行。我有它的麻烦。起初,我可以添加一个新的文本文件,并与一起我也可以更新此文件。但它是对1行。如果我更新另一行。它只是在文本文件的末尾添加更新的值。我要的是更新这个或删除和插入新的。但我不知道怎么办。我在更新阵列过程array_replace()。首先,我需要找出如果我的数据的ID在文本文件中找到。如果找到了,我会简单的更新/删除和现有数据更换成我的新更新的数据。如果没有发现,只有只添加。

下面是我的code表示。

  $ restaurant_id = $ post_data ['COMPANY_ID'];
        $ new_lat_entry = $ post_data ['new_lat'];
        $ new_long_entry = $ post_data ['new_long'];        / ****这是我的新的更新阵列**** /
        $ data_add =阵列(
            restaurant_id'=> $ restaurant_id,
            new_lat'=> $ new_lat_entry,
            new_long'=> $ new_long_entry,
            日期date_updated'=>日期('Y-M-D H:I:S')
        );
        / ****这是从现有的文本文件的基本阵列**** /
        $数据=反序列化(的file_get_contents('addresses.txt'));        目标$ = $ data_add ['restaurant_id'];        为($ I = 0; $ I<计数($的数据); $ I ++){            $ get_id = $数据[$ i] ['restaurant_id'];            如果($ get_id == $目标){                //如果找到ID - 更新                $ add_data工具=阵列();                $ add_data工具=阵列(
                    $ I => $ data_add
                );                $ new_array =阵列();
                $ new_array = array_replace($数据,$ add_data工具);                $ serialize_data =连载($ new_array);
                                $ file_input_txt =的fopen(addresses.txt,W +);
            FWRITE($ file_input_txt,$ serialize_data);
            FCLOSE($ file_input_txt);            }其他{                $ new_array =阵列(
                    $ I => $ data_add
                );                $ serialize_data =连载($ new_array);                $ file_input_txt =的fopen(addresses.txt,W +);
                                FWRITE($ file_input_txt,$ serialize_data);
                FCLOSE($ file_input_txt);
            }    }

我的文本文件的输出是序列化的形式。

<$p$p><$c$c>a:1:{i:0;a:4:{s:13:\"restaurant_id\";s:4:\"1519\";s:7:\"new_lat\";s:8:\"14.63823\";s:8:\"new_long\";s:9:\"121.02999\";s:12:\"date_updated\";s:19:\"2013-11-15十二时42分59秒;}}

这是所有球员请帮助我。我现在有一个最后期限。而且我坚持了下来。 :-(这是我第一次以一个文本文件,这就是为什么我有麻烦调试它创建一个CRUD。


解决方案

能否请您试试这个,

 &LT; PHP        / ****这是从现有的文本文件的基本阵列**** /
        $数据=反序列化(的file_get_contents('addresses.txt'));        $ restaurant_id ='1519';
        $ new_lat_entry = '14 0.64823';
        $ new_long_entry ='121.45999';        / ****这是我的新的更新阵列**** /
        $ data_add =阵列(
                restaurant_id'=&GT; $ restaurant_id,
                new_lat'=&GT; $ new_lat_entry,
                new_long'=&GT; $ new_long_entry,
                日期date_updated'=&GT;日期('Y-M-D H:I:S')
        );        目标$ = $ data_add ['restaurant_id'];        $计数=计数($的数据);
        $ new_array =阵列();
        为($ I = 0; $ I&LT; $计数; $ I ++){             $ get_id = $数据[$ i] ['restaurant_id'];
                //如果找到ID - 更新                $ add_data工具=阵列(
                        $ I =&GT; $ data_add
                );                如果($ get_id == $目标){
                    $ new_array = array_replace($数据,$ add_data工具);
                }        }
        $ serialize_data =连载($ new_array);        的print_r($ serialize_data);
        $ file_input_txt =的fopen(addresses.txt,W +);
        FWRITE($ file_input_txt,$ serialize_data);
        FCLOSE($ file_input_txt);
    ?&GT;

I have a problem in my code. My code is all about updating/deleting rows in textfile ig the textfile is found. I am having a trouble with it. At first I can add a new textfile and together with it I can also update this file. But it is for 1 row. If I update another row. It will just append the updated value at the end of the textfile. What I want is to update this or delete and insert the new one. But I don't know how. My process in updating the array is array_replace(). First I need to find out if the ID of my data is found in the textfile. If found I will simple update/delete and replace the existing data into my new updated data. If not found just add only.

Here's my code for that.

        $restaurant_id = $post_data['company_id']; 
        $new_lat_entry = $post_data['new_lat']; 
        $new_long_entry = $post_data['new_long']; 

        /****Here's my new updated array ****/
        $data_add = array( 
            'restaurant_id' => $restaurant_id, 
            'new_lat' => $new_lat_entry, 
            'new_long' => $new_long_entry, 
            'date_updated' => date('Y-m-d H:i:s') 
        ); 
        /****This is the BASE array from the existing textfile ****/
        $data = unserialize(file_get_contents('addresses.txt'));

        $target = $data_add['restaurant_id'];

        for ($i = 0; $i < count($data); $i++) {

            $get_id = $data[$i]['restaurant_id'];

            if($get_id == $target){

                //If ID is found - UPDATE

                $add_data = array();

                $add_data = array(
                    $i => $data_add
                );

                $new_array = array();
                $new_array = array_replace($data,$add_data);

                $serialize_data = serialize($new_array); 
                                $file_input_txt = fopen("addresses.txt","w+"); 
            fwrite($file_input_txt,$serialize_data); 
            fclose($file_input_txt); 

            }else{

                $new_array = array(
                    $i => $data_add
                );

                $serialize_data = serialize($new_array); 

                $file_input_txt = fopen("addresses.txt","w+"); 
                                fwrite($file_input_txt,$serialize_data); 
                fclose($file_input_txt); 


            }

    }

The output of my text file is in serialized form.

a:1:{i:0;a:4:{s:13:"restaurant_id";s:4:"1519";s:7:"new_lat";s:8:"14.63823";s:8:"new_long";s:9:"121.02999";s:12:"date_updated";s:19:"2013-11-15 12:42:59";}}

That's all guys please help me. I have a deadline now. And I am stuck with it. :-( This is the first time I am creating a CRUD based on a text file that's why I am having a trouble debugging it.

解决方案

Can you please try this,

        <?php   

        /****This is the BASE array from the existing textfile ****/
        $data = unserialize(file_get_contents('addresses.txt'));

        $restaurant_id = '1519';
        $new_lat_entry = '14.64823';
        $new_long_entry = '121.45999';

        /****Here's my new updated array ****/
        $data_add = array(
                'restaurant_id' => $restaurant_id,
                'new_lat' => $new_lat_entry,
                'new_long' => $new_long_entry,
                'date_updated' => date('Y-m-d H:i:s')
        );

        $target = $data_add['restaurant_id'];

        $Count =count($data);
        $new_array =array();
        for ($i = 0; $i < $Count; $i++) {

             $get_id = $data[$i]['restaurant_id'];


                //If ID is found - UPDATE   

                $add_data = array(
                        $i => $data_add
                );

                if($get_id == $target){
                    $new_array = array_replace($data,$add_data);    
                }

        }
        $serialize_data= serialize($new_array);

        print_r($serialize_data);
        $file_input_txt = fopen("addresses.txt","w+");
        fwrite($file_input_txt, $serialize_data);
        fclose($file_input_txt);


    ?>

这篇关于如何编写符合条件使用PHP文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆