> -----Original Message-----
> From: Chad Winger [mailto:[EMAIL PROTECTED]]
> Sent: 19 September 2002 06:25
> 
> Still using my text file example, which has lines such as:
> 
> 0706010102|01.01.02|16:00|Serie C2|02|Forlė|Florentia Viola|
> 0610010102|01.07.02|16:00|Serie C2|05|Florentia Viola|Gubbio|
> 1806190702|19.07.02|16:00|Serie C2|05|Savona|Florentia Viola|
> 
> so Now i wanted to compare $getdate[1] to $datetest and if any of the
> exploded arrays from $getdate[1] matched up with $getdate, 
> i.e. what is
> being submitted from the form, then it would not go to write the file.
>

A quick inspection prompts the following comments:

(1) *please* indent your code to reflect its structure -- makes it so much
easier to read!

(2) Why re-calculate $datetest on each iteration of the loop? Do it once
before you start looping.

(3) There's no need to use something as complex as ereg: as you're checking
for simple equality, a simple == will do!

(4) It's bad practice to open the file for writing whilst you've still got
it open for reading: I'd suggest completing the scan of existing entries
(taking care to remember whether it found a duplicate or not!), then close
the file, re-open it in *append* mode (not write) and just add the single
new line.  (The other option would be to use read-write mode ('a+') and
fseek() as appropriate; personally I would regard this as more error prone
for very little performance gain, but YMMV.)

A quick rewrite taking these points into account gives:
 
 $fd = fopen ($schedule, "r");
 $datetest = ("$day.$month.$year");
 $is_new_date = TRUE;  // assume this to begin with

 while (!feof ($fd)):
    $currentlines = fgets($fd, 4096);
    $getdate = explode("|", $currentlines);
    if ($getdate[1] == $datetest):
       echo 'date already listed';
       $is_new_date = FALSE;
       break;    // break out of while loop
    endif;
 endwhile;
 fclose($fd);

 if ($is_new_date):
    $fp = fopen($schedule, "a");
    fwrite($fp,
"\n$homename[0]$awayname[0]$day$month$year|$day.$month.$year|$matchtime|$com
petition|$round|$homename[1]|$awayname[1]|");
    fclose($fp);
    echo 'success';
 endif;

Hope this helps.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to