SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Noobie Question: Being able to recognise the type of content in a form

    I'm not sure that's the best title for my question, but it pretty much sums up something that I've been seeing on a few websites recently. Especially property sites.

    If you look on something like PropertyFinder, they have a search box which asks you to "Enter a Property ID, Location or Postcode"

    My question is, when a user hits the submit button, how does the processing script know which DB table to search through in order to get the relevant information. How does the processing script know whether the user has inputted an ID, a location or a postcode?

    Thanks very much

  2. #2
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple. Check what type of data it is then the length.

    PHP Code:
    $data $_POST['data'];
    if(
    is_numeric($data)){
        
    // Is property ID or postcode
        
    if(strlen($data) == 4){
            
    // is postcode
        
    }else{
            
    // is property id
        
    }
    }else{
        
    // is location

    Hope I haven't made any typos
    PHP | MySQL | (X)HTML | CSS

  3. #3
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ok - thanks Brent.

    One more quick question though - seeing as PropertyFinder generally deals with UK properties, the UK postcode would be made up of letters as well as numbers (eg MK40 1BH) ... how would you test for that?

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well on a property site, I would imagine there is only one table used to hald the address for all the properties, i.e

    House Number
    Road
    Postcode

    Then the form is just referencing one table and doesn't have to calculate what table to query.

    However, if you cant store all the stuffs you need in one table then you could take the input and do a "like query" to several tables in the DB or like brent said do some query to decide what info the user has entered and do a query based on your results.

    Any particular thing you are trying to achieve?
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  5. #5
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webnoob View Post
    Any particular thing you are trying to achieve?
    It's just something that I've noticed recently and got me thinking. After all it's quite a good way of getting around asking a user to fill in long forms to get where they want to be.

    As far as your comment about the address details all being held in the same table goes, I guess that would be correct. But you'd still need to know which field in that table the input relates to before you could pull out the relevant information wouldn't you?

  6. #6
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simontheak View Post
    Ah ok - thanks Brent.

    One more quick question though - seeing as PropertyFinder generally deals with UK properties, the UK postcode would be made up of letters as well as numbers (eg MK40 1BH) ... how would you test for that?
    Oh ok. Hmmm...
    PHP Code:
    $data $_POST['data'];
    if(
    is_string($data)){
        
    // is location
    }else{
        
    // Is property ID or postcode
        
    if(is_numeric($data)){
            
    // is property id
        
    }else {
            
    // is postcode
        
    }

    That should work

    Edit: Sorry, that wont work. Postcode will appear as a string...
    Last edited by brent5392; Jan 17, 2007 at 05:47. Reason: code typo
    PHP | MySQL | (X)HTML | CSS

  7. #7
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brent5392 View Post

    That should work
    Cheers brent ... one last quick question about is_string() as I've been having a look at it on PHP net.

    If I had a something like F54751 was input into the form (as a reference number), would that evaluate to a string because of the letter "F" at the begging of it?

    Just curious

  8. #8
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $data $_POST['data'];
    if(
    is_numeric($data)){
        
    // is property id
    }else{
        
    // Is location or postcode

    You could create a mysql table containing all US postcodes and check if the $data is equal to a row in that table. If a row is returned you know that $data is a postcode, otherwise it is a location :P
    Hope that solves your problem :P

    Edit: Sorry, take note of my edit in my post above :P That will answer your post above :P
    PHP | MySQL | (X)HTML | CSS

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simontheak View Post
    It's just something that I've noticed recently and got me thinking. After all it's quite a good way of getting around asking a user to fill in long forms to get where they want to be.

    As far as your comment about the address details all being held in the same table goes, I guess that would be correct. But you'd still need to know which field in that table the input relates to before you could pull out the relevant information wouldn't you?
    example

    PHP Code:
    <?php

    $result 
    mysql_query("SELECT HouseName, HouseNumber, HousePostcode FROM MyHouses WHERE HouseName='$string' OR HouseNumber='$string' OR HousePostcode='$string'")

    while (
    $row mysql_fetch_array($result)) {

        echo 
    $row['HouseName'];
        echo 
    $row['HouseNumber'];
        echo 
    $row['HousePostcode'];

    }

    ?>
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  10. #10
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webnoob View Post
    example

    PHP Code:
    <?php

    $result 
    mysql_query("SELECT HouseName, HouseNumber, HousePostcode FROM MyHouses WHERE HouseName='$string' OR HouseNumber='$string' OR HousePostcode='$string'")

    while (
    $row mysql_fetch_array($result)) {

        echo 
    $row['HouseName'];
        echo 
    $row['HouseNumber'];
        echo 
    $row['HousePostcode'];

    }

    ?>
    That should work fine as far as I can see. Well done
    PHP | MySQL | (X)HTML | CSS

  11. #11
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh ok - the penny has finally dropped.

    Thanks alot both of you for your help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •