Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
iffy if statement ?

Posted by Chris_Isaac (Chris_Isaac), 4 June 2003
Hi

I'm trying to get the following script to 1st, pick out all the enties out of a particular file with Gwent in the address, then all of those entries within Newport.  It starts printing when it reached the first Newport/Gwent, but then print everything after it.  Any ideas gratefully received.

<?php
$fh = fopen("/usr/home/testdata.txt","r");
$count =0;
while (list($place,$name,$phone,$addy,$email) = fgetcsv($fh,10000))  {

ereg('Gwent',$addy,$got);
  if($got){
   if ($place=="Newport")  {
      print "$accom, $addy, $got[0]<br>\n";
      $count++;
   }
 }
}
print $count;
?>      

Thanks

Chris

Posted by admin (Graham Ellis), 5 June 2003
Have you trimmed some code out of the example? Where does $accom come from??  I'm on the road - so only able to have a brief look today.  I also wonder ... I think there's somthing missing or that I haven't spotted.  I would tend to cut /paste/ modify the code to get more metrics for the problem.   I looked at the fgetcsv call - if it fails to match, it probably doesn't overwrite each of the list items but merely returns a "false" leaving old values in the variables including $place.   That could be a pointer, except that in the sample code the problem appears to be within the loop.

Posted by Chris_Isaac (Chris Isaac), 5 June 2003
Sorry, $accom should be $name, other than that all the code is there.

I've tested the script without the if ($got) { ....  loop and it does print out all places with Newport in $place, so I know this bit works, also it confirms that $got contains Gwent.

I just can't seem to then filter down for places with Gwent in the Address($addy).  Although I appreciate the script as it stands does it in reverse order.

Posted by admin (Graham Ellis), 6 June 2003
Ah - I've spotted something.

ereg returns a true / false value if it matches.  If it matches, it saves its "interesting bits" into the third parameter variable, $got in this case.  You want to write
       $isit = ereg( .....
then test $isit rather than $got.

The problem you've hit is that if a match fails, $got isn't changed  - thus the data leaks through from sucessful matches to failed matches the way it's written at the moment.


Posted by Chris_Isaac (Chris Isaac), 6 June 2003
Cool, thanks.

Its working perfectly now.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho