php rss

Posted on 2005-04-18
Last Modified: 2008-02-01
How would I create an rss that is generated by php and uses db values?
Question by:bartonjo2
    LVL 14

    Accepted Solution

    LVL 12

    Expert Comment

    LVL 16

    Expert Comment

    this is my own real life code. just replace my tables by your tables in your context!
    specification of the xml can be found here


    /* connection to the database and standard function */

    /* Select on system table */
    $querySystem = "select * from system where company = $company";
    $resultSystem = mysql_query($querySystem);
    $numberSystem = mysql_numrows($resultSystem);
    $webSiteName = htmlspecialchars(mysql_result($resultSystem,0,"websitename"), ENT_QUOTES);
    $mainLanguage = mysql_result($resultSystem,0,"mainlanguage");
    $adminEmail = mysql_result($resultSystem,0,"adminEmail");
    if ($language == "") {
       $language = $mainLanguage;

    $queryMetatags = "select * from metatags where language = '$language' and company = $company";
    $resultMetatags = mysql_query($queryMetatags);
    $numberMetatags = mysql_numrows($resultMetatags);
    $description = mysql_result($resultMetatags,0,"description");

    header ("Content-type: text/xml");

    $lastBuildDate = date("r");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    echo "<rss version=\"2.0\">";
    echo "<channel>";
    echo "<title>conceptMonitor : An open source and freeware software version tracker by $webSiteName</title>";
    echo "<link>http://$SERVER_NAME</link>";
    echo "<description>ConceptMonitor is a Software Version Tracker that checks regularly project homepage of open source and freeware products and provided a one stop shop to check the version of the latest stable release.<br/>The list of software projects monitored currently is small, however anyone can contribute by requesting that a particular project is added to the list.</description>";
    echo "<language>en-us</language>";
    echo "<lastBuildDate>$lastBuildDate</lastBuildDate>";
    echo "<category>Software Version Tracker</category>";
    echo "<generator>http://$SERVER_NAME</generator>";
    echo "<docs></docs>";
    echo "<copyright>&#169; $webSiteName</copyright>";
    echo "<managingEditor>$adminEmail</managingEditor>";
    echo "<webMaster>$adminEmail</webMaster>";
    echo "<image>";
    echo "<url>http://$SERVER_NAME/images/logo.jpg</url>";
    echo "<title>conceptMonitor : An open source and freeware software version tracker by $webSiteName</title>";
    echo "<link>http://$SERVER_NAME</link>";
    echo "</image>";
    echo "<ttl>60</ttl>";

    $query = "select * from monitor where deleteind = 'N' and status = 'L' and company = $company order by currentdate desc";

    $result = mysql_query($query);
    $number = mysql_numrows($result);

    $i = 0;

    if ($number != 0) {
       while ($i < $number) {
          $id = mysql_result($result,$i,"id");
          $name = mysql_result($result,$i,"name");
          $platform = mysql_result($result,$i,"platform");
          $url = mysql_result($result,$i,"url");
          $version = mysql_result($result,$i,"currentValue");
          $category = mysql_result($result,$i,"category");      
          $description = mysql_result($result,$i,"description");      
          $dateVersion = mysql_result($result,$i,"currentDate");
          $dateVersionFormat = fromMysqlDate($dateVersion);
          $dateVersionPubDate = date("r",strtotime($dateVersion));
          echo "<item>";
          if ($platform != "") {
             echo "<title>$name ($platform) version $version has been released on $dateVersionFormat</title>\n";
             echo "<description>$description</description>\n";
          else  {
             echo "<title>$name version $version has been released on $dateVersionFormat</title>\n";
             echo "<description>$description</description>\n";
          echo "<author>Alain Orset</author>\n";        
          echo "<category>$category</category>\n";          
          echo "<link>$url</link>\n";
          echo "<pubdate>$dateVersionPubDate</pubdate>\n";
          echo "</item>\n";
    echo "</channel>";
    echo "</rss>";

    exit ?>

    LVL 3

    Author Comment

    So I could have a while loop that would print the latest 10 items in xml format?? alain34?
    LVL 16

    Expert Comment

    you have 2 way of doing that
    - put a limit statement in the sql stament. in y exmaple it will be :
    $query = "select * from monitor where deleteind = 'N' and status = 'L' and company = $company limit 0,10 order by currentdate desc";
    - force $number to be 10 if the value produced is more than 10
    $number = mysql_numrows($result);
    if ($number > 10) {
       $number = 10;

    first option is better!
    LVL 3

    Author Comment

    yeah the first option  will allow me to actually show the top ten because it will sort by currentdate and by the field flag  being T for topten... I will see if this works

    Featured Post

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now