Jump to content

RSS Feed Cdata sections in PHP


Cep

Recommended Posts

Hi,

 

I know I am doing something stupid here but for the life of me I cannot see what.

 

This code should read a local database on my webserver and then output an RSS feed based on the information. The RSS feed is basically a string variable made of up the database information.

 

However two fields in my database can have illegal characters like ampersands which will break the xml so, my plan is to CDATA these sections. The only problem is using CDATA seems to cause the PHP parser to screw up :D

 

I know I am doing something wrong but I just can't quite put my finger on it. Here is my script,

 

<?php
function db() {
  $user = "";
  $pass = "";
  $database = "dave_rss";
  
  $db_conn = odbc_connect($database, $user, $pass) or die ("Unable to connect".odbc_errormsg());

return $db_conn;
}

function odbc_record_count($result, $db, $query) {
  
  $num_records = odbc_num_rows($result);
  
  if ($num_records < 0) {
    $count_query_string = "SELECT count(*) as results FROM ({$query})";
    $count = odbc_exec($db, $count_query_string);
    $num_records = odbc_result($count, "results");
  }
  
return $num_records;
}
    $thisyear = date('Y');

    $sql = "SELECT * FROM qryRssFeedCourses";
    
    $xml = "<?xml version='1.0' encoding='ISO-8859-1'?>\n";
    $xml .= "<rss version='2.0'>\n";
    $xml .= "<channel>\n";
    $xml .= "<title>mypeeps</title>\n";
    $xml .= "<description>A list of our up an coming courses at mypeeps.</description>\n";
    $xml .= "<link>http://www.mypeeps.com/</link>\n";
    $xml .= "<copyright>mypeeps copyright {$thisyear}</copyright>\n";
    $xml .= "<image>\n";
    $xml .= "<title>My Peeps</title>\n";
    $xml .= "<link>http://www.mypeeps.com/</link>\n";
    $xml .= "<url>http://mypeeps/rss/moomin.gif</url>\n";
    $xml .= "</image>\n";
    
    $result = odbc_exec(db(), $sql) or die ("Unable to run select query ".odbc_errormsg());
    
    $num_rows = odbc_record_count($result, db(), $sql);
    
    for ($i = 1; $i <= $num_rows; $i++) {
     $row = odbc_fetch_array(odbc_exec(db(), $sql), $i) or die ("Unable to fetch row");

     $date = date("D M Y", strtotime($row['From Date']));
     
     $title = "<!CDATA"."[{$row['Title']}"."]]>";
     $venue = "<!CDATA"."[{$row['Name of Location']}"."]]>";

     $xml .= "<item>\n";
     $xml .= "<title>{$title}</title>\n";
     $xml .= "<venue>{$venue}</venue>\n";
     $xml .= "<level>{$row['Course Level']}</level>\n";
     $xml .= "<type>{$row['CourseType']}</type>\n";
     $xml .= "<speaker>{$row['Speaker']}</speaker>\n";
     $xml .= "<times>{$row['From Time']} to {$row['To Time']}</times>\n";
     $xml .= "<date>{$date}</date>\n";
     $xml .= "</item>\n";
    }

    $xml .= "</channel>\n";
    $xml .= "</rss>";

    echo $xml;
?>

Link to comment
https://forums.phpfreaks.com/topic/98303-rss-feed-cdata-sections-in-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.