Results 1 to 7 of 7
  1. #1
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    PHP Functions
    Ok I am trying to create a function which can be included on multiple datafeed import scripts. The function should look at a string of text passed to it, lets call it $food, and return the food category $Food_Category. Here is what I have:

    in the main script
    PHP Code:
    require('categories-function.php');  // at the top of the script
    .
    .
    Start of a loop

    food_category
    ($meal);
                echo 
    $Food_Category;
    //.
    //.
    //.
    //  Use or Process $Food_Category at somepoint
    End of a loop 
    the included function is

    PHP Code:
    function food_category($SearchText) {
        if (
    preg_match('/beef/i',$SearchText) ||
            
    preg_match('/chicken/i',$SearchText)) {$Food_Categoryy "meat";}
            return 
    $Food_Category;
    // Fruit
        
    if (preg_match('/apples/i',$SearchText) ||
            
    preg_match('/apple pie/i',$SearchText) ||
            
    preg_match('/oranges/i',$SearchText) ||
            
    preg_match('/blue berries/i',$SearchText) ||
            
    preg_match('/banana/i',$SearchText) ||
            
    preg_match('/peach/i',$SearchText) ||
            
    preg_match('/peach cobler/i',$SearchText)) {$Food_Category "fruit";}
            return 
    $Food_Category;

    Any one have any ideal why I am not getting a food category back from the function or if I am why I can't see or use it?

    Thanks!
    Thanks,
    Doug

  2. #2
    Full Member
    Join Date
    January 18th, 2005
    Posts
    396
    Does the upper/lower case sensitivity of PHP catch you between Food_Category and food_category?

  3. #3
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    ahhh let me look at my code. Thanks
    Thanks,
    Doug

  4. #4
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    You forgot to assign $Food_Category:

    $Food_Category = food_category($meal);

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

  5. #5
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    ahhh doh... thanks snib. This is the first time I am trying to use function. I'm starting to see how functions can really help development.
    Thanks,
    Doug

  6. #6
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Man still no dice... I basiclly want to take a series of IF statements and put them in a function.

    In the script where I want to use the function.
    PHP Code:
        require('categories-function.php');  // the file with the function
    .
    .
    .
                
    $iCategory ""//this line clears the variable from last product
                
    $iCategory find_category($iName);  
    .
    .

    Here is the function with is included

    PHP Code:
    function find_category($SearchText) {
    //meat
        
    if (preg_match('/beef/i',$SearchText) ||
            
    preg_match('/chicken/i',$SearchText)) {$iCategory "Meat";}
            return 
    $iCategory;
    // fruit
        
    if (preg_match('/apples/i',$SearchText) ||
            
    preg_match('/apple pie/i',$SearchText) ||
            
    preg_match('/banana/i',$SearchText) ||
            
    preg_match('/peach/i',$SearchText) ||
            
    preg_match('/peach cobler/i',$SearchText) ||
            
    preg_match('/oranges/i',$SearchText) ||
            
    preg_match('/orange juice /i',$SearchText) ||
            
    preg_match('/grapes/i',$SearchText) ||
            
    preg_match('/grape juice/i',$SearchText) ||
            
    preg_match('/coconunt/i',$SearchText)) {$iCategory "fruit";}
            return 
    $iCategory;
                    } 
    Thanks,
    Doug

  7. #7
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Got it Figured out!

    In the the function I had the return $iCategory outsite the action of the IF statements. So the function was running getting to the fist return statement and returning an empty string.
    Thanks,
    Doug

  8. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. GAN Sorting Functions Don't Work
    By Byagi in forum Google Affiliate Network - GAN
    Replies: 3
    Last Post: January 10th, 2012, 10:58 PM
  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. "Copying" functions in cpanel/whm
    By davidh in forum Domains & Hosting
    Replies: 1
    Last Post: May 12th, 2009, 11:03 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
  •