Justtechjobs.com Find a programming school near you






Online Campus Both


php3-list | 199903

Re: [PHP3] Simple calendar functions From: Rasmus Lerdorf (rasmus <email protected>)
Date: 03/18/99

> 1000:1045:staff meeting:::angela:!staff
> 1025:1035:cigarette break:::jamesc:jamesc
>
> ..would end up in $hour[10], which I could then pull out later and split(:).
>
> Lack of caffeine, or something similar, is stopping me from being able to
> do this though.
>
> Any thoughts, anyone?

First, let me re-re-re-iterate that you should be using a database for
this. No matter how you might try justifying your own flat files, listen
to some of the people here who have been building systems like this for
years when they tell you that you are better off using a database.

Anyway, to answer your question.

If I was reading in this datafile and wanted to split things into a
2-dimensional array such that the above 2 entries you listed ended up
being:

  $hour[10][0] = "1000:1045:staff meeting:::angela:!staff";
  $hour[10][1] = "1025:1035:cigarette break:::jamesc:jamesc";

I would use a loop like this:

  $fp = fopen($datafile,"r");
  while(!feof($fp)) {
    $line = fgets($fp,256); // 256 char max line length
    $hour[(int)substr[$line,0,2]][] = $line;
  }
  fclose($fp);

Pretty simple. There are two concepts that are important. First, to pick
out the first two characters of the line read from the file I just used
substr(). Then I took these 2 characters and converted them to an integer
using the (int) cast. The [] at the end of the expression tells php to
put the item in the next available array position. This one tends to
confuse people. A simple example:

  $a[] = 1; $a[] = 2; $a[] = 3;

Would result in an array that looks like this:

  $a[0]=1
  $a[1]=2
  $a[2]=3

-Rasmus

--
PHP 3 Mailing List   http://www.php.net/
To unsubscribe send an empty message to php3-unsubscribe <email protected>
To subscribe to the digest list:  php3-digest-subscribe <email protected>
For help: php3-help <email protected>  Archive:  http://www.php.net/mailsearch.php3
List administrator:  zeev-list-admin <email protected>