CSSKarma

display your <style>

Creating & Managing RSS from PHP

This is just a quick tutorial on something that's been coming up a lot for me lately. Managing a large web site with no content management system (and no CMS in sight for the near future) can be a hell of a task when everyone wants to get their hand into the web content and make edits. I run into this problem A LOT. So lately I've been implementing a custom sort of CMS using simple PHP/MySQL and making the output available in an RSS format.

This example comes from http://vista.ncsu.edu and it applies to the announcement section in the middle of the homepage.

First, I set up the database to host the announcements with 4 fields: id, title, field, and timestamp. Here's a screenshot with 1 entry:

mySQL table

So that's the database. There's also an admin area for users (with access) to manage the announcements. Here's that:

announcement admin

And the code...

HTML
<h2>Manage Vista Announcements</h2>
<form method="post" action="/announcements/index.php" name="xmltesting" id="xmltesting">
<label for="title">Title: <input type="text" name="title" id="title"/></label>
<label for="field">Announcement: <textarea name="field" cols="50" rows="5" id="field"></textarea></label>
<input type="submit" name="submit" value="submit"/>
</form>
<table>
<tr>
<th>announcement</th>
<th>date</th>
<th>action</th>
</tr>
<form action="index.php" method="post">
<tr>
<input type="hidden" name="delete" id="delete" value="46"/>
<td class="field"><strong>Update about LMS Evaluations: </strong> blah blah blah. [Posted 2.21.08]</td>
<td>02/21/2008</td>
<td><input type="submit" name="submit" value="Delete"/></td>
</tr>
</form>
</table>

Nothing too alarming there, what to focus on is the hidden form field with value="46", that corresponds with the announcement ID in the database.

PHP
<?php
//managing announcements
$number = $_POST["delete"];
$id = $row['id'];

//if someone is submitting an announcement, do this
if ($_POST["field"]){
mysql_query("INSERT INTO announcements_vista (field, title) VALUES ('$_POST[field]', '$_POST[title]')");
echo "<p class='add'><strong>Announcement added as</strong>:<br/>&quot;".$_POST["field"]."&quot;</p>";
}

//if someone is deleting an announcement, do this
if ($_POST["delete"]){
mysql_query("DELETE FROM announcements_vista WHERE id=$number") or die(mysql_error()); 
echo "<p class='del'>Record ".$number." deleted</p>";
}

//getting data from database
$query = "SELECT id, title, field, date_format(timestamp, '%m/%d/%Y') AS datestamp FROM announcements_vista ORDER BY id DESC"; 
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num = mysql_num_rows($result);

if ($num = 0) { echo 'No Records found'; } else { echo '<table><tr><th>announcement</th><th>date</th><th>action</th></tr>'; while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { echo '<form action="index.php" method="post"><tr>'; echo '<input type="hidden" name="delete" id="delete" value="'.$row['id'].'"/>'; echo '<td class="field"><strong>'.$row['title'].': </strong>'.$row['field'].'</td>'; echo '<td>'.$row['datestamp'].'</td>'; echo '<td><input type="submit" name="submit" value="Delete"/></td>'; echo '</tr></form>'; } echo '</table>'; } ?>

You'll also need database connection info to make that work.

Then next step is making the output an RSS feed. In a new file, we do this:

<?php
header('Content-type: text/xml');
$query = "SELECT id, title, field, date_format(timestamp, '%m/%d/%Y') AS datestamp FROM announcements_vista ORDER BY id DESC";
$results = mysql_query($query, $con) or die('Query failed: ' . mysql_error());
$num = mysql_num_rows($results);
//print_r($row_commentSys);
?>
<rss version="2.0">
<channel>
<title>Vista Announcements</title>
<description>View the latest announcements</description>
<link>http://vista.ncsu.edu/</link>
<copyright>2007</copyright>
<?php do { ?>
<item>
<title><?php print $result['title'];?></title>
<description><?php print $result['datestamp'];?></description>
<link><?php print 'http://vista.ncsu.edu/'; ?></link>
<pubDate><?php print $result['datestamp'];?></pubDate>
<guid><?php print 'http://vista.ncsu.edu/'; ?></guid>
</item> 
<?php } while ($result = mysql_fetch_assoc($results)); ?>
</channel>
</rss>

Again, you'll need you database connection info here, I just omitted mine. This will create your RSS feed from the data from your mySQL database.

Lastly, you'll need to display this on the homepage. Since you're dealing with MySQL and not regular RSS, there's no parsing needed, you can just connect and query your database like this:

<?php
include("connection.php");
if ($con){
$query = "SELECT id, title, field, date_format(timestamp, '%m/%d/%Y') AS datestamp FROM announcements_vista ORDER BY id DESC";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num = mysql_num_rows($result);

if ($num = 0) {
echo 'No announcements found';
} else {
echo '<ul>';
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo '<li><strong>'.$row['title'].': </strong>'.$row['field'].'</li>';
}
echo '</ul>';
}
}<
mysql_close();
?>

That's all, you should now have a way for people to edit content without a CMS and I think the RSS is a nice touch to add in. Feel free to email me any questions about this, I feel like I kinda rushed through it.

with ease,
Tim

© 2008 csskarma.com - Valid CSS - Valid XHTML 1.0 Creative Commons License