Training, Open Source computer languages

This is page http://www.wellho.net/forum/Writing-PHP/iffy-if-statement.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

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