Results 1 to 6 of 6

Thread: php dilemma for rss feed

  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default php dilemma for rss feed

    Hello all, I'm using this code below...

    PHP Code:
    <?php
    $cache_time 
    3600*24// 24 hours

    $cache_file $_SERVER['DOCUMENT_ROOT'].'/cache/test.rss';
    $timedif = @(time() - filemtime($cache_file));

    if (
    file_exists($cache_file) && $timedif $cache_time) {
        
    $string file_get_contents($cache_file);
    } else {
        
    $string file_get_contents('http://www.web-development-blog.com/feed/');
        if (
    $f = @fopen($cache_file'w')) {
            
    fwrite ($f$stringstrlen($string));
            
    fclose($f);
        }
    }
    $xml simplexml_load_string($string);

    // place the code below somewhere in your html
    echo '
    <div id="rssbox">
        <ul>'
    ;
    $count 0;
    $max 3;
    // the next object is an example for a feed from wordpress
    foreach ($xml->channel->item as $val) {
        if (
    $count $max) { 
            echo 
    '
            <li>
                <strong>'
    .$val->title.'</strong><br />
                '
    .$val->description.' | <a href="'.$val->link.'">More  &gt;</a>
            </li>'
    ;
        }
        
    $count++;
    }
    echo 
    '
        </ul>
    </div>'
    ;
    ?>
    I'm using it to push a blog to my site, but i cant seem to get it to work. I'll type out now exactly what I'm doing so you can point out where I'm going wrong. This is the first time I've played with php so I dont know if what I'm doing is right or wrong.

    1. I open my html dreamweaver document where I want to show this blog feed
    2. I place the above code in where I want it to appear on the page
    3. I edit the '$string = file_get_contents' to my blog address
    4. I select save as and change the .html extension to .php
    5. I preview the page in my browser and there is no blog there and the bottom third of the code literally shows up on the page preview

    Where am I going wrong?

    Thanks in advance for any help.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's very hard to diagnose what is wrong without being able to test it. Do you get any error messages?

    What do you mean by this?
    the bottom third of the code literally shows up on the page preview
    The simplest thing to check first: did you switch to code view rather than design view? If you paste the code into the design view it will just display the text. If you paste it in code view it'll actually execute it as PHP.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi there, what I mean by that is, on the design view of the page, not the code view. Some of that php code shows up. I am doing everything in code view yes. Is it ok to have a .html file then just save it as .php? Ps, is that all I need to change in the php code, the '$string = file_get_contents('http://www.web-development-blog.com/feed/');'?

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ok, then that's not the problem. Next...

    .php is processed as PHP. This means that anything between <?php and ?> is run as PHP then outputs HTML and the browser sees pure HTML. Renaming a .htm page to .php without any <?php ?> tags will be fine. If you add some, then that will additionally be processed as PHP-- only in .php, not .htm-- so this is required.


    I don't fully understand what this code is supposed to do. Remember, code is just the use of many different tools to create a "working" whole. It can be written in many ways and do many things. Every script is different, so I'm not quite sure about this one yet. I see the basic steps, though, and one part jumps out as unusual.
    At the end there are a lot of symbols like $something->$something.
    Without getting into too much of the complex details here, this is something used in "classes" and you don't have any classes defined in your script.
    It also suggests that this is to be used with wordpress. Is this supposed to be connected to wordpress and it is not? Is this meant to run alone?

    Perhaps a link to where you got this would help explain what it's supposed to do (and then why it's not doing it).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    this is the link where I got it from http://www.finalwebsites.com/snippets.php?id=49

    So are you saying that since my file is .php i dont need my <?php and ?> ??

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    <?php ?> marks PHP code. You DO need it if you are using PHP code and not just plain HTML. It only works in .php pages, not .htm.
    So, yes, you're doing this correctly.

    But the main point of what I said is that it looks like that code uses things that aren't already included-- it needs a base of wordpress.
    I suppose it's based on the xml function, but I'm not sure how this relates to your blog.

    I'd suggest starting on some basic PHP tutorials to get an idea of how all this works together then returning to this to figure out how to complete it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    paulprice85 (04-28-2010)

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
  •