• home
  • forum
  • my
  • kt
  • download
  • Dynamic RSS Feeds

    Author: 2007-08-13 10:36:25 From:

    Ever wondered how you create an RSS feed dynamically? It's actually pretty simple. With some use of PHP's header() function, it's not a problem at all.

     

    The Code:
    1. <?php
    2. $output = '
    3. <rss version="2.0">
    4.         <channel>';
    5. mysql_connect('localhost','user','pass');
    6. mysql_select_db('news');
    7.  
    8. //set the content type to xml
    9. header("Content-Type: text/xml");
    10.  
    11. $sql = mysql_query("SELECT * FROM news LIMIT 10");
    12. while($res = mysql_fetch_array($sql))
    13. {
    14.         $title = $res['title'];
    15.         $description = $res['description'];
    16.         $author = $res['author'];
    17.         $id = $res['id'];
    18.         $link = "http://www.yoursite.com/news.php?id=$id";
    19.         $output .= <<<EOT
    20.  
    21.                 <item>
    22.                         <title>The Your Site News</title>
    23.                         <link>http://www.yoursite.com/</link>
    24.                         <description>$description</description>
    25.                 </item>
    26.        
    27. EOT;
    28.  
    29. }
    30. $output .= '
    31. </channel>
    32. </rss>';
    33. echo $output;
    34.  
    35. ?>


    Explanation:

     We start connecting to the database and whatnot, then we use the header() function to tell the user that this is an RSS feed. We then move on to grabbing the news article from the database. Once thats done we loop through the information and then output it.

    Voila! You have a dynamically created RSS feed!

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot