Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    php function help please how to use a function with $i in it?

    Hi all,

    ok i have a site which displays a number of different feeds and a total of around 5 different feed settings to display the correct info,

    the php pages look up the databse to see which setting needs to be applied to show all the feed data so to speak. and the top 3 feeds are show in detail at the top end of my entertainment3 page which is what the for loop is for

    so in my entertainment3.php page i have
    PHP Code:
    for ($i 0$i 3$i++) {
    feedinfo();

    feedinfo is a function in my feeds_function.php page
    PHP Code:
    <?php function feedinfo (){;
    if (
    $rssIndividual[$i] == ''){
                print 
    "Sorry No Information Currently Available";
            }
        
                elseif (
    $feedID[$i] == '1'){
            
                
    $feedurl $rssIndividual[$i];
                
    $xml=simplexml_load_file($feedurl);
                
    $count 1;    
                
                    foreach (
    $xml->merch->prod as $item){
                    
    $desc html_entity_decode($item->desc);
                        
                        if (
    in_array($item->pId,$test[$i])){
                            if(
    $count 0){
                            
                            echo
    "
                            <div class='inditemheader'>
                            <a href='{$item->awLink}'>{$item->name}</a></div><br />
                    
                            <div class='inditemimg'>
                            <a href='{$item->awLink}'>
                            <img src='{$item->mImage}' 
                            alt='{$item->name}' 
                            height='100' 
                            border='0' 
                            vspace='0'/></a>
                            </div>
                    
                            <div class='inditemdesc'>$desc</div><br />
                            <div class='inditemprice'>Buy Now For £{$item->price->search}</div>
                            <div class='inditeminfo'><a href='{$item->awLink}'>More Info</a></div>"
    ;  
                            
    $count--;        
                                 }
                          }
                    }
                }
              
                  elseif (
    $feedID[$i] == '2'){
            
                
    $feedurl $rssIndividual[$i];
                
    $xml=simplexml_load_file($feedurl);
                
    $count 1;    
                
                    foreach (
    $xml->product as $item){

                        if (
    in_array($item->TDProductId,$test[$i])){
                            if(
    $count 0){
                                echo
    "
                                <div class='inditemheader'>
                                <a href='{$item->deepLink}'>{$item->productname}</a></div><br />
                    
                                <div class='inditemimg'>
                                <a href='{$item->deepLink}'>
                                <img src='{$item->imageURL}'
                                alt='{$item->productname}' 
                                height='100' 
                                border='0' 
                                vspace='0'/></a>
                                </div>
                    
                                <div class='inditemdesc'>{$item->description}</div><br />
                                <div class='inditemprice'>Buy Now For £{$item->Price}</div>
                                <div class='inditeminfo'><a href='{$item->deepLink}'>More Info</a></div>"
    ;                              $count--;  
                            }  
                        }
                    }                    
                }
                
                              elseif (
    $feedID[$i] == '3'){
            
                
    $feedurl $rssIndividual[$i];
                
    $xml=simplexml_load_file($feedurl);
                
    $count 1;    
                
                    foreach (
    $xml->merchant->prod as $item){

                        if (
    in_array($item->pId,$test[$i])){
                            if(
    $count 0){
                                echo
    "
                                <div class='inditemheader'>
                                <a href='{$item->uri->awTrack}'>{$item->text->name}</a></div><br />
                    
                                <div class='inditemimg'>
                                <a href='{$item->uri->awTrack}'>
                                <img src='{$item->uri->mImage}'
                                alt='{$item->text->name}' 
                                height='100' 
                                border='0' 
                                vspace='0'/></a>
                                </div>
                    
                                <div class='inditemdesc'>{$item->text->promo}</div><br />
                                <div class='inditemprice'>Buy Now For £{$item->price->buynow}</div>
                                <div class='inditeminfo'><a href='{$item->uri->awTrack}'>More Info</a></div>"
    ;                              $count--;  
                            }  
                        }
                    }                    
                }

    }
    i have also included this page on my entertainment3 page

    but i am getting some notices here

    and im not sure how to solve them.

    please advise me

    many thanks
    Luke
    Last edited by LJackson; 03-25-2009 at 07:09 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I think it should be like
    PHP Code:
    for ($i 0$i 3$i++) {
    feedinfo($i);
    }  

    function 
    feedinfo ($i){
    if (
    $rssIndividual[$i] == ''){
                print 
    "Sorry No Information Currently Available";
            }
     ............. 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Scope problem.
    Functions create their own stack in memory. So, any variable declared outside of a function (with the exception of superglobals) are unavailable within the actual function.
    That's this guy here: $rssIndividual. Pass this value into the method as a parameter in order to make use of it, or you can globalize it which is not recommended.

    PHP Code:
    function feedInfo(array $rssIndividual); 
    should be you're function signature.

    Edit:
    I lol'd. Neither mine or abduraooft's post's are correct, but together they make the whole. I was missing the $i variable, and he was missing the $rssIndividual variable.
    Edit:
    Another one. I guess since I was also missing the $i, the signature will change:
    PHP Code:
    function feedInfo(array $rssIndividual$i);
    // OR
    function feedInfo($rssIndividual); 
    Where the second function is likely what you want, but you pass in the value at the corresponding $rssIndivdual[$i] into the feedInfo method.

    Last edited by Fou-Lu; 03-25-2009 at 05:14 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok im confused lol

    the function signature is that what i would use to call the function in my entertainment3 page or what i use to define the function in the feeds_function page ?

    sorry for missunderstanding

    cheers
    Luke

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i have added it to my feeds_function page like so
    PHP Code:
    <?php function feedinfo(array $rssIndividual$i){;
    if (
    $rssIndividual[$i] == ''){
                print 
    "Sorry No Information Currently Available";
            }
        
                elseif (
    $feedID[$i] == '1'){
            
                
    $feedurl $rssIndividual[$i];
                
    $xml=simplexml_load_file($feedurl);
                
    $count 1;    
                
                    foreach (
    $xml->merch->prod as $item){
                    
    $desc html_entity_decode($item->desc);
                        
                        if (
    in_array($item->pId,$test[$i])){
                            if(
    $count 0){
                            
                            echo
    "
                            <div class='inditemheader'>
                            <a href='{$item->awLink}'>{$item->name}</a></div><br />
                    
                            <div class='inditemimg'>
                            <a href='{$item->awLink}'>
                            <img src='{$item->mImage}' 
                            alt='{$item->name}' 
                            height='100' 
                            border='0' 
                            vspace='0'/></a>
                            </div>
                    
                            <div class='inditemdesc'>$desc</div><br />
                            <div class='inditemprice'>Buy Now For £{$item->price->search}</div>
                            <div class='inditeminfo'><a href='{$item->awLink}'>More Info</a></div>"
    ;  
                            
    $count--;        
                                 }
                          }
                    }
                }
              
                  elseif (
    $feedID[$i] == '2'){
            
                
    $feedurl $rssIndividual[$i];
                
    $xml=simplexml_load_file($feedurl);
                
    $count 1;    
                
                    foreach (
    $xml->product as $item){

                        if (
    in_array($item->TDProductId,$test[$i])){
                            if(
    $count 0){
                                echo
    "
                                <div class='inditemheader'>
                                <a href='{$item->deepLink}'>{$item->productname}</a></div><br />
                    
                                <div class='inditemimg'>
                                <a href='{$item->deepLink}'>
                                <img src='{$item->imageURL}'
                                alt='{$item->productname}' 
                                height='100' 
                                border='0' 
                                vspace='0'/></a>
                                </div>
                    
                                <div class='inditemdesc'>{$item->description}</div><br />
                                <div class='inditemprice'>Buy Now For £{$item->Price}</div>
                                <div class='inditeminfo'><a href='{$item->deepLink}'>More Info</a></div>"
    ;                              $count--;  
                            }  
                        }
                    }                    
                }
    }
    but its throwing up an error,

    Catchable fatal error: Argument 1 passed to feedinfo() must be an array, none given, called in /customers/kernow-connect.com/kernow-connect.com/httpd.www/entertainment3.php on line 248 and defined in /customers/kernow-connect.com/kernow-connect.com/httpd.www/feeds_function.php on line 1

    cheers for you help so far guys much appreciated
    Luke
    Last edited by LJackson; 03-25-2009 at 05:45 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Post how you're calling the feedInfo function.
    Oh, and take that ; off the line with the function signature on it. That does not belong there.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    its ok now thanks mate i removed that ; and added another two varibles to the function which i required

    the function
    PHP Code:
    <?php function feedinfo(array $rssIndividual$feedID$test$i){;
    and i called it by
    PHP Code:
    feedinfo($rssIndividual,$feedID$test$i); 
    which is working

    thank you both for all your help, you learn something new every day

    cheers
    Luke


  •  

    Posting Permissions

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