Results 1 to 6 of 6
  1. #1
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    PHP switch() help
    I am trying to search through $subject, which is the category field in the data feed. If I match certain words I want to assign a category. Later in the script I will import everything into my DB. After I run the import script the only category which is created is the last one "home and office". If i switch a case section and make the apparel last then that will be what is entered into the category field.

    You might be wondering why I just don't import what is in the merchant's datafeed? Well the merchant just has too much other stuff in the category field and I and trying to make cleaner categories.

    PHP Code:
      $subject $data[13];
                switch (
    $subject) {
                    case 
    preg_match("/[A],[a]utographed/",$subject):
                         
    $iCategory "autographed memorabilia";
                    case 
    preg_match("/[P],[p]ants/",$subject):
                         
    $iCategory "apparel";
                            case 
    preg_match("/[H],[h]ome and [O],[o]ffice/",$subject):
                         
    $iCategory "home and office";
                         } 
    Any and all help is greatly appreciated.
    Thanks,
    Doug

  2. #2
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Hi Doug,

    Hey I don't do much with switch statements but I think that you need a break statement in between each or it executes all cases that follow a matching case. So, and this is best guess here, I think that it should read:

    PHP Code:
     $subject $data[13];
                switch (
    $subject) {
                    case 
    preg_match("/[A],[a]utographed/",$subject):
                         
    $iCategory "autographed memorabilia";
                          break;
                    case 
    preg_match("/[P],[p]ants/",$subject):
                         
    $iCategory "apparel";
                          break;
                            case 
    preg_match("/[H],[h]ome and [O],[o]ffice/",$subject):
                         
    $iCategory "home and office";
                          break;
                         } 

    Otherwise, it will just keep reassigning the variable with the next value.

    Give it a try. Let me know if it helps.
    Last edited by bettylou; May 4th, 2008 at 09:40 PM. Reason: Clarity

  3. #3
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    I don't know if you can use preg_match in a switch like that. Switches are generally for checking absolute values like if($x == 1) {} else if($x == 2) {}, etc.

    I would just use normal if-statements.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  4. #4
    Full Member 212TomTom's Avatar
    Join Date
    May 31st, 2007
    Location
    Canton, GA
    Posts
    203
    BettyLou is right. You need a break statement on your conditional actions. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement.

  5. #5
    Newbie
    Join Date
    June 3rd, 2007
    Location
    the Netherlands
    Posts
    46
    break should work.
    An other way is put it all in a temp table and use the power of mysql to select only those you want for a certain category and put that in your final table.

  6. #6
    Member
    Join Date
    July 13th, 2008
    Posts
    57
    you'll have to give a break after every case. lets say u have five cases, and the third case matches. PHP (other languages too, its the same at least in Java and C) will execute third, fourth and fifth cases, if there is no break in between them.

    snib: PHP allows using text and other complicated expressions in case statements, unlike Java for example, which only allows integer values.

  7. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Router vs Switch??
    By leeann in forum Midnight Cafe'
    Replies: 30
    Last Post: April 22nd, 2010, 01:32 AM
  2. PHP question - include(file.php) with variables
    By teezone in forum Programming / Datafeeds / Tools
    Replies: 8
    Last Post: September 4th, 2009, 04:54 PM
  3. Bait and Switch
    By jerseyjim in forum Virtual Family and Off-Topic
    Replies: 0
    Last Post: June 21st, 2005, 11:59 PM

Posting Permissions

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