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
 
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))
Code misses out when data has spaces

Posted by Chris_Isaac (Chris Isaac), 23 July 2003
Hi

The following code almost works correctly.

It performs a search on a given town name and produces a list of matching towns and their countys.  fine so far but it seems to be missing the counties if there are spaces in the town name, eg, Newport Pagnell then misses out Buckinghamshire.  Also Newport, Isle of White is missed out completely.

If I change the:
  array_push($county,$row[0]); line to
  array_push($county,$row[1]);
It produces a list of counties fine, but no town names.

I've also tried exploding the following line, but that didn't work:
 list($t,$c) = split(",",$current);

Any ideas appreciated.

Chris


Code:
if ($town) {
$searches = array("^$town");
$reports = array("$town");            
$newarr=array();
$county=array();
$unilist=array();
sql_connect();
$sql="select town, county from locations where town   rlike \"$town\"";
$query=mysql_query($sql);
while ($row=mysql_fetch_array($query)){
   array_push($county,$row[0]);
  }

$unilist=array_unique($county);
$next = 9;
}
if ($next==9) {                  
print "Please choose the county associated with the town<br>";
print "or try another search<br><br>";

foreach ($unilist as $current) {

 list($t,$c) = split(",",$current);

print "<a href=\"http://www.limited.org.uk/testfinder.php4?nexty=5&town=$t&county=$c\">Town/County : $current</a>";
print "<br>";
$next=$_GET[nexty];
}
}


Posted by admin (Graham Ellis), 23 July 2003
Try doing a "view source" on the page that's returned to you; I suspect that you're generating HTML that includes space characters within an HREF parameter which you should replace with + characters (URL encoding!).

Posted by Chris_Isaac (Chris Isaac), 24 July 2003
Sorry, the code wasn't quite doing what I thought it was, row[0] was only pulling in the town field, but it had Newport, Gwent down as the town which was confusing me.

I've changed the script as per below, again I'm close, its printing all as it should, but it prints it out 3 times. I think the problem is in the loop, but a can't seem to keep the county field if I take/move the print statement from its current position.

any hints please?

Thanks

Chris

Code:
     if ($town) {
           $searches = array("^$town");
           $reports = array("$town");            
           $newarr=array();
           $eee=array(0);
           $combined = array(0);
           $unilist=array();
           sql_connect();
           $sql="select town, county from locations where town rlike \"$town\"";
           $query=mysql_query($sql);
           while ($row=mysql_fetch_array($query)){
                 array_push($county,$row[1]);
           }
           $unilist=array_unique($county);
           $next = 9;
           }
if ($next==9) {                  
     print "Please choose the county associated with the town<br>";
     print "or try another search<br><br>";

foreach ($unilist as $current) {
           $sql="select town from locations where town rlike \"$town\" and county rlike \"$current\"";
           $query=mysql_query($sql);
           while($row=mysql_fetch_array($query)){
                 array_push($newarr,$row[0]);
           }
           $eee=array_unique($newarr);
                 foreach ($eee as $this) {
           $sql="select county from locations where town rlike \"$this\"";
           $query=mysql_query($sql);
                 }
           while ($row=mysql_fetch_array($query)){

                 print "<a href=\"http://www.limited.org.uk/finder.php4?nexty=2&town=$this&county=$row[0]\">Town: $this  -- County : $row[0]</a>";
                 print"<br>";
                 $next=$_GET[nexty];      
                       }
}
}




Posted by admin (Graham Ellis), 24 July 2003
You haven't by any chance loaded the towns file into your database three times have you  

Do you get each town coming out three times in sucession, or the whole list three times over?

Posted by Chris_Isaac (Chris Isaac), 24 July 2003
No, I did do that, but then corrected it before trying these scripts  

Here is the output from the script if you search on the word newport:


Town: Newport -- County : County Down
Town: Newport -- County : Isle of Wight
Town: Newport -- County : Gwent
Town: Newport -- County : Buckinghamshire
Town: Newport -- County : Pembrokeshire
Town: Newport -- County : Shropshire
Town: Newport -- County : County Down
Town: Newport -- County : Isle of Wight
Town: Newport -- County : Gwent
Town: Newport -- County : Buckinghamshire
Town: Newport -- County : Pembrokeshire
Town: Newport -- County : Shropshire
Town: Newport -- County : County Down
Town: Newport -- County : Isle of Wight
Town: Newport -- County : Gwent
Town: Newport -- County : Buckinghamshire
Town: Newport -- County : Pembrokeshire
Town: Newport -- County : Shropshire
Town: Newport Pagnell -- County : Buckinghamshire
Town: Newport, Pembrokeshire -- County : Pembrokeshire
Town: Newport, Shropshire -- County : Shropshire


and here is the output if you do a select on the relevant table:


+------+------------------------+------------------+---------+-----------+
| id   | town                   | county           | country | saycounty |
+------+------------------------+------------------+---------+-----------+
| 1080 | Newport                |  County Down     | UK      |         1 |
| 1090 | Newport                |  Isle of Wight   | UK      |         1 |
| 1091 | Newport                |  Gwent           | UK      |         1 |
| 1092 | Newport Pagnell        |  Buckinghamshire | UK      |         1 |
| 1095 | Newport, Pembrokeshire |  Pembrokeshire   | UK      |         1 |
| 1096 | Newport, Shropshire    |  Shropshire      | UK      |         1 |
+------+------------------------+------------------+---------+-----------+
6 rows in set (0.00 sec)


Thanks

Chris

Posted by admin (Graham Ellis), 24 July 2003
OK - I think I spot it now.  I think you're generating a list of all the "Newport" towns - six of them. For each of the towns you got, you are doing a query that looks for the town name again and pulls in the county.   For "Newport", it gets six matches (and it does that three times), then for the three places that have longer names you're getting just one response.

Sorry - Chris - I think your logic is more comples that it needs to be - I think you only need to do one query which looks for "Newport". If it gets just one row back that's great - you have a unique town; if it gets more than one row back, simply collect the town and country fields ....

Posted by Chris_Isaac (Chris Isaac), 25 July 2003
the original script (please see below) produced a county list as per:


Town: newport -- County : County Down
Town: newport -- County : Isle of Wight
Town: newport -- County : Gwent
Town: newport -- County : Buckinghamshire
Town: newport -- County : Pembrokeshire
Town: newport -- County : Shropshire


which is what I want.  the problem is newport above is pulled from $town which in turn is basically what is entered in in the in the text box, this is fine so far, but if someone puts in a partial word, eg, new and does a search, and does pull all the correct counties, but doesn't pull the complete town name, so I get a list like:

new, Gwent
new, IOW  blah blah.....

how do I get the town name for each individual county listed?

Thanks

Chris

Code:
     if ($town) {
           $searches = array("^$town");
           $reports = array("$town");            
           $county=array();
           $unilist=array();
           sql_connect();
           $sql="select town, county from locations where town rlike \"$town\"";
           $query=mysql_query($sql);
           while ($row=mysql_fetch_array($query)){
                 array_push($county,$row[1]);
           }
           $unilist=array_unique($county);
           $next = 9;
           }
if ($next==9) {                  
     print "Please choose the county associated with the town<br>";
     print "or try another search<br><br>";

foreach ($unilist as $current) {

                 print "<a href=\"http://www.limited.org.uk/finder.php4?nexty=2&town=$town&county=$current\">Town: $town  -- County : $current</a>";
                 print"<br>";
                 $next=$_GET[nexty];
     }
}




Posted by admin (Graham Ellis), 25 July 2003
use $row[0] back from your SQL request as the town name in the subsequent report / pulldown menu, rather than the value entered by the user into the variable $town.   Just as you push the counties onto an array, you can push the towns actually found onto an array  

Posted by Chris_Isaac (Chris Isaac), 25 July 2003
Just a quick note to say thanks

I changed the array_push line to :

Code:
                 array_push($county,$row[0],$row[1]);



and the loop to :

Code:
for ($n=0; $n<count($county);$n=$n+2) {
                 
                 $x=$n+1;
                 print "<a href=\"http://www.limited.org.uk/finder.php4?nexty=2&town=$county[$n]&county=$county[$x]\">Town: $county[$n]  -- County : $county[$x]</a>";
                 print"<br>";
                 $next=$_GET[nexty];
     }



and it works like a dream.

Again thanks for the pointers... Chris



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., 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