Hi

Please go easy I'm new 🙂

As per my subject I would like to perform a task based on today's date and vaules read from a flat file.

eg:

flat text file or csv containing every date of the year, descriptionA, decriptionB, and action.

[B]Date,DescA,DescB,Action[/B]
01/01/2009,open,only once,A
02/01/2009,closed,today,B
03/01/2009,limited,once,C 

This flat file would have every day listed, the decription is just a text field and the action could be A,B,C,D or E..

The script would run by a cron at midnight and check today's date, find the correct entry in the flat file and then store it's values.

If 'flatfile' action == A then DO THIS
else if 'flatfile action ==B then DO THAT

ETC..

Is that possible ?? can anyone give me any pointers ??

Thanks

    Yes. It's possible.

    [man]fgetcsv[/man] can be used to read CSV files.

    I'd recommend using a different date format - one that's easier to parse and/or work with. The international standard is yyyy-mm-dd. If you use [man]date[/man] to convert the present date into the same format, "2009-03-01" < "2009-03-11" as it should be.

      Thanks..

      I will change the dates to this 2009-03-11 format.

      I've had a look at fgetcsv but can't see how I would search the CSV for the action based on the correct date..

      Can you help ??

        Someone feel free to correct me, but:

        Modifying the example from: http://www.php.net/fgetcsv.

        <?php
        $row = 1;
        $handle = fopen("file.csv", "r");
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data); 
            if ($num < 4) { // Check to ensure there are 4 values
                echo "Invalid Data found in CSV file, incomplete";
            } 
            $run_date = $data[0];
            $descA = $data[1];
            $descB = $data[2];
            $action = $data[3];
        
           if ($run_date == date(Y-m-d)) {
             // Run $action;
           } // end if
        } // end while
        fclose($handle);
        ?>
        

        Not sure if this will work for what you want... but should work, havn't tested it.

          Thanks. Since my last post I had a play and found this..

          <?php
          $file = "data.csv";
          
            $_today = date("Y-m-d");
            echo $_today . "<br>";
          
            $file_content = file_get_contents ( $file );
            $file_arr = preg_split ( '/\n/' , $file_content );
            $found = false;
          
            foreach ( $file_arr as $item ) {
                  $data = explode ( ',' , $item );
                  if ( $data[0] === $_today ) {
                        $found = true;                  
                        $_action = $data[1];
                  							}
            								 }
            if ( $found )   	
                	echo $_action;
          
          		if ( $_action == A )
          			DO THIS
          		elseif ( $_action == B )
          			DO THAT
          		elseif ( $_action ==C )
          			DO THIS BIT
          		else
          			echo "NOT WORKING";
          			}
          ?>

          This works and echo's the correct date ($today) and the correct action ($action).

          But it fails at the if ( $_action ==A ) section.. I always get NOT WORKING...
          Even though I can see the action is echoed as A, B or C..

          Sample data.csv: ( I've decided to drop the descA & descB for now)

          2009-03-11,A
          2009-03-12,B
          2009-03-13,A
          2009-03-14,C
          2009-03-15,B
          2009-03-16,A
          2009-03-17,A

          If I run the page I get this as the output based on 2009-03-11

          2009-03-11
          A NOT WORKING

          Where have I gone wrong ??
          Thanks 🙂

            use apostrofe if you compare two value:

            if ($a=='1')
            echo "Do this";
            elseif( $a=='2')
            echo "Do this";
            else
            print "Nothing...";

            And switchcase is a better way to make things depending from several cases.

              Where have I gone wrong ??

              don't you think that using a database is a better way to handle events and properties in an organised way ? i think so.

              in 3 lines you can reach the important events for today.

              you can use import functions to feed the events (in phpyadmin)

              if you want to modify something, its one click.

                hmm.. changing to:

                            if ($_action=='A')
                                echo "DO THIS";
                            elseif ($_action=='B')
                                echo "DO THAT";
                            elseif ($_action=='C')
                                 echo "DO THIS BIT";
                            else
                                 echo "NOT WORKING"; 

                Hasn't helped..

                I still get :

                2009-03-11
                A NOT WORKING

                Any ideas ?

                  djjjozsi;10906665 wrote:

                  don't you think that using a database is a better way to handle events and properties in an organised way ? i think so.

                  in 3 lines you can reach the important events for today.

                  you can use import functions to feed the events (in phpyadmin)

                  if you want to modify something, its one click.

                  I totally agree HOWEVER !!

                  This is being hosted on a device that has no DB support and no option to have it installed.

                  The csv file is 8KB and will only be changed 4 -6 times a year so manually editing it and ftp'ing it back is not an issue.

                  Thanks for the advise thou ! 🙂

                    okay.
                    then make (date) folders with todo.txt files 🙂

                    if the today folder is exists, read the todo.txt.

                      Currently got it working with the exception of the If -- ElseIf statement

                      This is what I have that doesn't work !!

                         if ($_action=='A')
                                      echo "DO THIS";
                                  elseif ($_action=='B')
                                      echo "DO THAT";
                                  elseif ($_action=='C')
                                       echo "DO THIS BIT";
                                  else
                                       echo "NOT WORKING"; 

                      I've tried:

                      			     switch ($_action) {
                          				case "A":
                              				echo "Action = A";
                              				break;
                          				case "B":
                              				echo "Action = B";
                              				break;
                          				case "C":
                      				        echo "Action = C";
                              				break;
                              			default:
                              				echo "NOT WORKING";
                      									}

                      Same doesn't work ? What have I got wrong ??

                      echo $_action; results in A, but both of the above return NOT WORKING.

                      Any ideas ??

                        lets put the IF inside tha foreach cycle.

                        indent your code, and see where this foreach ends.

                        did you tried trim?

                             [code=php]        switch (trim($_action)) {
                                        case "A":
                                            echo "Action = A";
                                            break;
                                        case "B":
                                            echo "Action = B";
                                            break;
                                        case "C":
                                            echo "Action = C";
                                            break;
                                        default:
                                            echo "NOT WORKING";
                                                        }[/code]

                          SORTED 🙂

                          The output kept showing:

                          2009-03-11
                          A NOT WORKING

                          Yet I wasn't adding the space after the A or before the NOT WORKING.

                          So I added:

                          $_action = trim($_action);

                          Before the if statement and now it works !!

                          Thanks for your help.

                            Write a Reply...