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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Problem trying to create rss_file in a loop.

    Hi,

    I am trying to generate a sepearte rss feed for each blog for which the data is held in my "platforms" table.

    So I am trying to step through the table and changing the name of my file handle each time.

    I have a very odd outcome

    The test output to my feed file is actually coming out on my log file.
    The loop should run FOUR times as there are that number of platforms (blog) but it only runs ONCE.

    This is my code

    PHP Code:
    $sql_plat "SELECT platform_id, plat_name,plat_url,plat_side,site_url,rss_cat FROM platforms";
    $result_plat mysql_query($sql_plat) or write_error("Could not find any PLATFORMS.".mysql_error()."\r\n");
    $num_plat mysql_num_rows($result_plat);

    if (
    $num_plat == ) {
        
    write_log("No Feed Available \r\n");
        
    fclose($handle);
        exit;  
        }  
    // endif    

    write_log("Number of Platforms: $num_plat\r\n");
        
    $feed_no=1;
    while (
    $row_plat mysql_fetch_assoc($result_plat)) {
        
    extract($row_plat);    
        
        
    write_log("Plat_name: $plat_name\r\n");
        
        
        
    $feed_name $plat_url."-feed.xml"
            
        
    $rss_file "/home/com567b/public_html/rss/".$feed_name
        
        ${
    'handle'.$plat_name}  = fopen("$rss_file""wb"); 
        
        
    write_log("Handle_rss: ${'handle'.$plat_name}\r\n");
        
        function 
    write_rss($content) {
            global ${
    'handle'.$plat_name};
            
    fwrite(${'handle'.$plat_name}, $content);        
        } 
        
        
    write_log("Feed_name: $feed_name\r\nrss_file: $rss_file\r\nHandle_rss: ${'handle'.$plat_name}\r\n");
        
        
    write_log("Start Platform No.$platform_id: $plat_name\r\nCreated New Feed: $site_url/$feed_name\r\n");
        
        
    write_rss("Test : $plat_name\r\n");

            
    fclose(${'handle'.$plat_name});
            
            
    $feed_no $feed_no+1;
        }  
    // end while Finished Main

        
    write_log("Finished Total $feed_no Feeds.\r\n");    
    fclose($handle); 
    And the output to the log file is:

    New record - Time Stamp: 06:38:51 Sunday, 19 May 2013
    This script: rss_feeds.php, This file: /home/com567b/public_html/rss/a_log_RSS_feed.txt
    Site no: 1, Site Name: Expert-World.com
    Site no: 2, Site Name: ZZ-Reviews
    End of Site Listing

    Start Platform Listing
    Number of Platforms: 4
    Plat_name: Meaty Monster Bikes
    Handle_rss: Resource id #10
    Feed_name: meaty-monster-bikes-feed.xml
    rss_file: /home/com567b/public_html/rss/meaty-monster-bikes-feed.xml
    Handle_rss: Resource id #10
    Start Platform No.1: Meaty Monster Bikes
    Created New Feed: zz-reviews.com/meaty-monster-bikes-feed.xml
    Test : Meaty Monster Bikes
    Plat_name: Sail Boat Kits
    Handle_rss: Resource id #11

    I have used a dynamic variable for the file handle, which should work.

    Is there something wrong with my use of dynamic variable in my function ??

    Can any one see why it only completes the first loop and why is has written
    "Test : Meaty Monster Bikes" to the log file and not to the file:
    "rss_file: /home/com567b/public_html/rss/meaty-monster-bikes-feed.xml"


    Many thanks.
    David.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,

    I tried changing :

    PHP Code:
    ${'handle'.$plat_name}  = fopen("$rss_file""wb"); 
    to

    PHP Code:
    ${handle.$plat_name}  = fopen("$rss_file""wb"); 
    BUT it did not make any difference.


    Any suggestions ???



    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    can you paste the code for your functions write_log() and write_rss()
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Umm:
    PHP Code:
    while ($row_plat mysql_fetch_assoc($result_plat)) {
        
    //. . .
        
    function write_rss($content) {
            global ${
    'handle'.$plat_name};
            
    fwrite(${'handle'.$plat_name}, $content);        
        } 
    You cannot redeclare a function. Check your error logs, that will issue a fatal error. So its not the looping that's the problem, it never returns to the loop to evaluate the third condition.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK - I thought that it may be the function inside the loop causing a problem

    But I do not know how else I am goıing to define the flie and file handle.

    I need to change the file name so that my script can write
    the different rss.xml files

    My aim is to go through the tables and create ( write ) all the necessary
    rss.xml files - this number will increase gradually each week and I don't know
    which they will be - I just want to have this script write them automatically.

    It will run on a cron job once a week.

    Any ideas ?


    Thanks.

    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I'm not sure why you think that'll help you in defining them. The scope of the function would dictate that $plat_name doesn't exist anyway, so every file pointer it uses would be $handle and not $handleXXX where XXX is the $plat_name. If you ran a global of $plat_name and then a global of the $handle pull, than that would work. If it actually works on performing any write, that indicates that $plat_name is not in use. Not that that matters anyway, there is no reason to use a variable variable style naming here since only a single filepointer is open at any given time. The only thing I can see here for 100% certainty is that plat_name has absolutely no use when constructing a variable: Plat_name: Meaty Monster Bikes. Variables cannot have spaces in them.

    And this is one of the very good reasons why 'global' should never be used. So here's what you do:
    - remove the use of combined string and variable to create a variable variable
    - remove the uee of global
    - modify the function signature to accept a filepointer resource and call as such
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    $sql_plat = "SELECT platform_id, plat_name,plat_url,plat_side,site_url,rss_cat FROM platforms";
    $result_plat = mysql_query($sql_plat) or write_error("Could not find any PLATFORMS.".mysql_error()."\r\n");
    $num_plat = mysql_num_rows($result_plat);

    if ($num_plat == 0 ) {
    write_log("No Feed Available \r\n");
    fclose($handle);
    exit;
    } // endif

    write_log("Number of Platforms: $num_plat\r\n");

    $feed_no=1;
    while ($row_plat = mysql_fetch_assoc($result_plat)) {
    extract($row_plat);

    write_log("Plat_name: $plat_name\r\n");


    $feed_name = $plat_url."-feed.xml";

    $rss_file = "/home/com567b/public_html/rss/".$feed_name;

    $Dplat_name = str_replace(" ","-",$plat_name);
    ${handle.$Dplat_name} = fopen("$rss_file", "wb");
    ${'handle'.$plat_name} = fopen("$rss_file", "wb");

    write_log("Handle_rss: ${'handle'.$plat_name}\r\n");

    function write_rss($content) {
    global ${'handle'.$plat_name};
    fwrite(${'handle'.$plat_name}, $content);
    }

    write_log("Feed_name: $feed_name\r\nrss_file: $rss_file\r\nHandle_rss: ${'handle'.$plat_name}\r\n");

    write_log("Start Platform No.$platform_id: $plat_name\r\nCreated New Feed: $site_url/$feed_name\r\n");

    write_rss("Test : $plat_name\r\n");

    fclose(${'handle'.$plat_name});

    $feed_no = $feed_no+1;
    } // end while Finished Main


    write_log("Finished Total $feed_no Feeds.\r\n");
    fclose($handle);
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,

    Thanks for you help.

    Not sure that I fully understood it, but I did the following:

    1) Changed the variable so it doesn't have spaces
    using : $Dplat_name = str_replace(" ","-",$plat_name);

    2) Took out the global statement.


    PHP Code:
    $sql_plat "SELECT platform_id, plat_name,plat_url,plat_side,site_url,rss_cat FROM platforms"
    $result_plat mysql_query($sql_plat) or write_error("Could not find any PLATFORMS.".mysql_error()."\r\n"); 
    $num_plat mysql_num_rows($result_plat); 

    if (
    $num_plat == ) { 
        
    write_log("No Feed Available \r\n"); 
        
    fclose($handle); 
        exit;   
        }  
    // endif     

    write_log("Number of Platforms: $num_plat\r\n"); 
         
    $feed_no=1
    while (
    $row_plat mysql_fetch_assoc($result_plat)) { 
        
    extract($row_plat);     
         
        
    write_log("Plat_name: $plat_name\r\n"); 
         
         
        
    $feed_name $plat_url."-feed.xml";  
             
        
    $rss_file "/home/com567b/public_html/rss/".$feed_name;  
         
        
    $Dplat_name str_replace(" ","-",$plat_name);

        ${
    handle.$Dplat_name}  = fopen("$rss_file""wb"); 
         
        
    write_log("Handle_rss: ${'handle'.$Dplat_name}\r\n"); 
         
        function 
    write_rss($content) { 
            
    fwrite(${'handle'.$Dplat_name}, $content);         
        }  
         
        
    write_log("Feed_name: $feed_name\r\nrss_file: $rss_file\r\nHandle_rss: ${'handle'.$Dplat_name}\r\n"); 
         
        
    write_log("Start Platform No.$platform_id: $plat_name\r\nCreated New Feed: $site_url/$feed_name\r\n"); 
         
        
    write_rss("Test : $Dplat_name\r\n"); 

            
    fclose(${'handle'.$Dplat_name}); 
             
            
    $feed_no $feed_no+1
        }  
    // end while Finished Main 

         
    write_log("Finished Total $feed_no Feeds.\r\n");     
    fclose($handle); 
    Still doesn't work

    If I take the write_rss() function definition out of the loop
    how can I change the file name ??

    Really not sure how to do this


    Thanks.


    .
    Last edited by jeddi; 05-23-2013 at 03:44 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    could you do more inside the function?

    PHP Code:
     function write_rss($content$file) { 
            
    fwrite($file$content);
            
    fclose($file);
        }  


         
        
    write_log("Feed_name: $feed_name\r\nrss_file: $rss_file\r\nHandle_rss: ${'handle'.$Dplat_name}\r\n"); 
         
        
    write_log("Start Platform No.$platform_id: $plat_name\r\nCreated New Feed: $site_url/$feed_name\r\n"); 
         
        
    write_rss("Test : $Dplat_name\r\n", {'handle'.$Dplat_name}); 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #10
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Well, I am not sure ...

    you mean like changing the file name ??

    How do I do that ?

    Thanks.

    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #11
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    Like the code i posted in my last post
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #12
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Something like this : ???

    PHP Code:
        
    $log_file 
    "/home/com567b/public_html/rss/a_log_RSS_feed.txt"
    $handle fopen("$log_file""wb"); 
    $logstamp date('H:i:s l, j F Y'$today);

    function 
    write_log($content) {
        global 
    $handle;
        
    fwrite($handle$content);        


    function 
    write_error($content) {
        global 
    $handle;
        
    fwrite($handle$content);     
       exit;
    }

    write_log("New record - Time Stamp: $logstamp \r\n");
    write_log("This script: rss_feeds.php, This file: $log_file\r\n");



    function 
    write_rss($content$file) { 
            
    fwrite($file$content);
            
    fclose($file);
        }  


    $sql_plat "SELECT platform_id, plat_name,plat_url,plat_side,site_url,rss_cat FROM platforms"
    $result_plat mysql_query($sql_plat) or write_error("Could not find any PLATFORMS.".mysql_error()."\r\n"); 
    $num_plat mysql_num_rows($result_plat); 

    if (
    $num_plat == ) { 
        
    write_log("No Feed Available \r\n"); 
        
    fclose($handle); 
        exit;   
        }  
    // endif     

    write_log("Number of Platforms: $num_plat\r\n"); 
         
    $feed_no=1
    while (
    $row_plat mysql_fetch_assoc($result_plat)) { 
        
    extract($row_plat);     
         
        
    write_log("Plat_name: $plat_name\r\n"); 
             
        
    $feed_name $plat_url."-feed.xml";  
        
    $rss_file "/home/example/public_html/rss/".$feed_name;  

        
    write_rss("MY CONTENT\r\n"$rss_file); 

         
    $feed_no++; 

        }  
    // end while Finished Main 

         
    write_log("Finished Total $feed_no Feeds.\r\n");     
    fclose($handle); 

    Is it going to be OK to change the file name like this ??

    I thought it needed to be "opened" with a handle each time ?
    ( as in this statement $handle = fopen("$log_file", "wb"); )

    As suggested earlier, should I also get rid of the "global" statements, or do I need them to
    make the function work inside the while loop ?

    Thanks.

    .
    Last edited by jeddi; 05-23-2013 at 06:12 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #13
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    im not sure,

    you could treat it like a database and do everything in the function

    PHP Code:
    function write_rss($content$file) { 
            
    $handle fopen($file"wb"); 
            
    fwrite($handle$content);
            
    fclose($handle);
        }  

         
    $feed_no=1
    while (
    $row_plat mysql_fetch_assoc($result_plat)) { 
        
    extract($row_plat);     
        
    $rss_file "/home/example/public_html/rss/".$plat_url."-feed.xml";  

        
    write_rss("MY CONTENT\r\n"$rss_file); 
        
    $feed_no++; 

    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #14
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    you might be interested in this also

    http://php.net/manual/en/function.file-put-contents.php

    PHP Code:
    <?php
    $file 
    'people.txt';
    // The new person to add to the file
    $person "John Smith\n";
    // Write the contents to the file, 
    // using the FILE_APPEND flag to append the content to the end of the file
    // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
    file_put_contents($file$personFILE_APPEND LOCK_EX);
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Get rid of global. The only time you should *ever* use it is when you cannot modify a function signature such as that of an object oriented override, a callback such as in set_error_handler, and usort. These cannot be modified to change the input data within them; any other method can be written to accept the arguments you need, as well as provide them at runtime.
    Global is a debugging nightmare. It is easy to loose track of your variables, and even easier to assume something is valid. Global will never throw an error and will simply inject a variable into the symbols table regardless of if it exists or not. So now on top of not being able to track a variable, you now cannot guarantee a variable exists (even with error reporting). The actual use of global though doesn't matter where its defined. It takes a global of the variable of a given state at runtime when its called, not when its linked.
    Personally I wouldn't write an write_log and write_error like this at all. They are identical methods and both globalize. Instead, I would hook into the error handler and catch user errors instead:
    PHP Code:
    function mylogger($errno$errstr$errfile$errline, array $errcontext = array())
    {
        
    // see, here I *need* to use global.  For which I'll take that string.
        
    global $log_file;
        if (!
    file_exists($log_file))
        {
            
    // better check if we can create it.
            
    $aPathInfo pathinfo($log_file);
            if (!
    file_exists($aPathInfo['dirname']))
            {
                die(
    'Cannot handle provided log file: ' $log_file);
            }
        }
        
    // can create custom output here.  I won't.
        
    switch ($errno)
        {
            case 
    E_USER_NOTICE:
            case 
    E_USER_WARNING:
            case 
    E_USER_ERROR:
                
    error_log($errstr3$log_file); // *pretty* sure this runs via a+.  Otherwise create in above check
                
    break;
        }
    }

    set_error_handler('mylogger'E_USER_ERROR E_USER_WARNING E_USER_NOTICE);
    trigger_error("New record - Time Stamp: $logstamp"E_USER_NOTICE);
    trigger_error("This script: rss_feeds.php, This file: $log_file"E_USER_NOTICE);
    // etc. 
    That will let you change your error handler function at any time, as well as allowing you to change the handler for each error type. E_USER_NOTICE would be for information, E_USER_WARNING for something that's not quite right but it was *probably* sorted out anyways, and E_USER_ERROR for something is broken and we can't fix it. This is IMO far more flexible. Now you simply trigger_error for anything you want, and provide the error type.

    For your actual file now, you can use as suggested above with file_put_contents. If you don't have that available, pass a pointer to an open file:
    PHP Code:
    function write_rss($content$fh)
    {
        if (!
    is_resource($fh) || is_resource($fh) && get_resource_type($fh) == 'file')
        {
            die(
    'Provided resource is not a file');
        }
        
    fwrite($fh$content);

    If you open $fh with 'a' or 'a+', you don't need to lock. Otherwise, an flock() would be a good idea.

    This function *must* be defined outside of the loop. Although functions are handled on the link of the script normally, function's defined within a block scope outside of main will not have their signatures registered until the runtime goes over the block of code. For example, this:
    PHP Code:
    while (false)
    {
        function 
    f()
        {
            global 
    $a;
            print 
    $a;
        }
    }

    f(); 
    will throw an undefined function error. As will this:
    PHP Code:
    function f()
    {
        function 
    f2()
        {
        }
    }

    f2(); 
    In the above, you may call f() once and ONLY once, where calling it now allows you to call f2(). Attempting to call f() again will throw an previously declared function error. That is why the function_exists() method exists.
    This behaviour is the same regardless of how its done. The scope is what matters; functions, objects, loops, included scripts, etc, etc, CANNOT redeclare a function at any point in time.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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