Junhee
Junhee

Reputation: 35

php - fwrite edit from line X to line X

I created a calendar with its data stored in a text file. For the moment, I can only edit one line/day at the same time, but I would like to edit many days (same content from sept 15th to sept 19th for example). How to do this ?

My text file with the data looks like this (with $id going from 1 to 31) and $exp is the content I edit.

        $id|$exp|$color|$color2

And the php file allowing the editing looks like this:

        $month_file = $file . ".txt"; 
        $month_db = file("$month_file");
        $call = fopen("$month_file","w");

        foreach($month_db as $month_line) {
            $month_line_arr = explode("|",$month_line);
            $month_line_id = $month_line_arr[0];
        if($month_line_id == $id) {
        fwrite($call,"$month_line_arr[0]|$exp|$color|$color2\n");
        }else {
        fwrite($call,"$month_line");
        }
        }


        fclose($call);

Upvotes: 1

Views: 144

Answers (1)

Barmar
Barmar

Reputation: 781300

Change

if($month_line_id == $id) {

to:

if ($month_line_id >= $min_id && $month_line_id <= $max_id) {

where $min_id and $max_id contain 15 and 19 in your example.

BTW, you can use fgetcsv() to read the line and split it into columns in one step, and then fputcsv() to write it back that way.

Upvotes: 1

Related Questions