SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Removing third character with substr command

    OK, I have a database that identifies states and provinces with five-character codes (in a field named IDArea), like this:

    California = us-ca
    Alberta = ca-ab

    I want to be able to process ID's with both prefixes - us and ca - with a single script, like this:

    PHP Code:
    $res mysql_query ("SELECT * FROM pxstates where IDArea = 'mycode'") or die (mysql_error()); 
    I can convert us-az (Arizona) to az by simply placing us- before $mycode:

    PHP Code:
    $res mysql_query ("SELECT * FROM pxstates where IDArea = 'us-$mycode'") or die (mysql_error()); 
    Or I can convert ca-ab to ab by adding ca-:

    PHP Code:
    $res mysql_query ("SELECT * FROM pxstates where IDArea = 'ca-$mycode'") or die (mysql_error()); 
    To do both with one script, I have an almost solution. A field named ParentID features three-letter abbreviation's for the parent of each state and province. So a script similar to the one below ought to yield usa-az on the Arizona page, or can-ab on the Alberta page:

    PHP Code:
    $res mysql_query ("SELECT * FROM pxstates where IDArea = '$data_details[IDParent]-$mycode'") or die (mysql_error()); 
    Now I just have to get rid of the third character, transforming usa-az into us-az. But how can I do that?

    I think I've found the answer on http://us4.php.net/manual/en/function.substr.php, but I'm not sure how to write it. Can anyone help?

    Thanks.

  2. #2
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please disregard this thread; I actually figured this one out myself!

    PHP Code:
    $data_details[IDParent] = substr("$data_details[IDParent]"02); // returns "1st 2 characters, turning usa into us"
    $res mysql_query ("SELECT * FROM pxstates where IDArea = '$data_details[IDParent]-$mycode'") or die (mysql_error()); 


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
  •