SitePoint Sponsor

User Tag List

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

    Querying 1st three letters of a variable

    I've been working with table that have ID tags (field IDArea) for the world's nations. If I insert $mycode = 'fra' in a page's head section, then write a database query that includes, "Where IDArea = $mycode, then that table displays data for France (ID = fra).

    Now I'm working on something similar for animals and plants, but there's a catch. I'm using six-character ID's - the first three letters of the genus and species names. For example, the wolf - Canus lupus - is can lup.

    But consider the following values:

    ros | Rosa |
    rosbla | Rosa | blanda
    rosche | Rosa | cherokee

    The first row represents a generic designation - ALL roses. So could I display all three designations on a page by querying just the first three letters of ros, rosbla, and rosche? In other words, any code that begins with ros would be fair game.

    I may be leaving something out. This is kind of confusing, and I'm sure I'll have to work out a number of kinks as I encounter them. One problem I anticipate is listing the general designation first and only once in the page's title.

    For example, consider the following:

    United Kingdom | ros | Rosa |
    Canada | ros | Rosa |
    Arkansas | rosbla | Rosa | blanda
    North Dakota | rosche | Rosa | cherokee

    If the page's title = $mycode, I wouldn't want the title to read "Rosa blanda," when the page focuses on ALL roses. Nor would I want it to read Rosa Rosa for both the UK and Canada. I'd want it to simply read Rosa, or perhaps Rosa species.

    Do you know how I can handle that?

    Thanks.

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    for the first part (and for MySQL):

    SELECT LEFT(column, 3) FROM table;
    resp.
    SELECT * FROM table WHERE LEFT(column, 3) = '$mycode';
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So could I display all three designations on a page by querying just the first three letters of ros, rosbla, and rosche? In other words, any code that begins with ros would be fair game.
    WHERE IDArea LIKE 'ros%' would do that (WHERE IDArea LIKE '$mycode%')

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips. I've got it working now.


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
  •