[eluser]invision[/eluser]
Hi,
I built an RSS Feed in CodeIgniter, from using the brilliant Derek Allards blog entry on this.
Controller:
Code:
<?php
class Vacanciesfeed extends Controller
{
function Vacanciesfeed()
{
parent::Controller();
$this->load->model('MVacancies', '', TRUE);
$this->load->helper('xml');
}
function index()
{
$data['encoding'] = 'utf-8';
$data['feed_name'] = 'names.co.uk';
$data['feed_url'] = 'http://www.names.co.uk';
$data['page_description'] = 'names';
$data['page_language'] = 'en-gb';
$data['creator_email'] = '[email protected]';
$data['posts'] = $this->MVacancies->getRecentVacancies();
header("Content-Type: application/rss+xml");
$this->load->view('vacancies-feed/rss', $data);
}
}
View:
Code:
<?php
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title><?php echo $feed_name; ?></title>
<link><?php echo $feed_url; ?></link>
<description><?php echo $page_description; ?></description>
<dc:language><?php echo $page_language; ?></dc:language>
<dc:creator><?php echo $creator_email; ?></dc:creator>
<dc:rights>Copyright <?php echo gmdate("Y", time()); ?></dc:rights>
<admin:generatorAgent rdf:resource="http://www.names.co.uk/" />
<?php foreach($posts->result() as $entry): ?>
<item>
<title><?php echo xml_convert($entry->title); ?></title>
<link><?php echo site_url('vacancies/id/' . $entry->id) ?></link>
<guid><?php echo site_url('vacancies/id/' . $entry->id) ?></guid>
<description><![CDATA[
<?= str_replace('/img/post_resources/', base_url() . 'img/post_resources/', $entry->body); ?>
]]></description>
<pubDate><?php echo date ('r', $entry->pubdate);?></pubDate>
</item>
<?php endforeach; ?>
</channel></rss>
Model (snippet):
Code:
<?php
class MVacancies extends Model{
function MVacancies(){
parent::Model();
}
function getVacancies(){
$data = array();
$this->db->join('vacancies_categories', 'vacancies.category_id = vacancies_categories.id');
$Q = $this->db->get('vacancies');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
function getRecentVacancies() {
$this->db->orderby('pubdate', 'desc');
//$this->db->where('post_visible', 1);
$this->db->limit(10);
return $this->db->get('vacancies');
}
.....
Right now, if I visit
http://www.names.co.uk/index.php/vacanciesfeed/ the XML feed is generated.
However, I'd also like to split the feed up by category. So if they wanted specific vacancies, they could go to /vacanciesfeed/category/1/ and it would retrieve all vacancies with the category_id of 1.
Does this make sense?
I tried with this function in the Controller, but with no success
Code:
function category($slug)
{
$data['encoding'] = 'utf-8';
$data['feed_name'] = 'names.co.uk';
$data['feed_url'] = 'http://www.names.co.uk';
$data['page_description'] = 'names';
$data['page_language'] = 'en-gb';
$data['creator_email'] = '[email protected]';
$data['posts'] = $this->MVacancies->getVacanciesByCategory($slug);
header("Content-Type: application/rss+xml");
$this->load->view('vacancies-feed/rss', $data);
}
Many thanks for any help.
I'm convinced it's something very simple to do