Welcome Guest, Not a member yet? Register   Sign In
Split RSS Feed into three Feeds (based on Category)
#1

[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>

    

    &lt;title&gt;&lt;?php echo $feed_name; ?&gt;&lt;/title&gt;



    &lt;link&gt;&lt;?php echo $feed_url; ?&gt;&lt;/link&gt;

    <description>&lt;?php echo $page_description; ?&gt;</description>

    <dc:language>&lt;?php echo $page_language; ?&gt;</dc:language>

    <dc:creator>&lt;?php echo $creator_email; ?&gt;</dc:creator>



    <dc:rights>Copyright &lt;?php echo gmdate("Y", time()); ?&gt;</dc:rights>

    <admin:generatorAgent rdf:resource="http://www.names.co.uk/" />



    &lt;?php foreach($posts->result() as $entry): ?&gt;

    

        <item>



          &lt;title&gt;&lt;?php echo xml_convert($entry->title); ?&gt;&lt;/title&gt;

          &lt;link&gt;&lt;?php echo site_url('vacancies/id/' . $entry->id) ?&gt;&lt;/link&gt;

          <guid>&lt;?php echo site_url('vacancies/id/' . $entry->id) ?&gt;</guid>



          <description>&lt;![CDATA[

      &lt;?= str_replace('/img/post_resources/', base_url() . 'img/post_resources/', $entry->body); ?&gt;

      ]]></description>

      <pubDate>&lt;?php echo date ('r', $entry->pubdate);?&gt;</pubDate>

        </item>



        

    &lt;?php endforeach; ?&gt;

    

    </channel></rss>

Model (snippet):
Code:
&lt;?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 Sad

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 Smile
#2

[eluser]Aken[/eluser]
Adding that category() function to your main RSS controller is what you should normally do. You should explain what exactly didn't work - what errors you got or something.
#3

[eluser]invision[/eluser]
My apologies for the delayed reply.
When I visit this: /index.php/vacanciesfeed/category/1 it shows the start of the feed successfully, but then stops just after this line: <admin:generatorAgent rdf:resource="http://www.names.co.uk/" />
with the error message:
Code:
<br />
<b>Fatal error</b>:  Call to a member function on a non-object in <b>....../application/views/vacancies-feed/rss.php</b> on line <b>23</b><br />

Any ideas what this could be?


Thank you
#4

[eluser]invision[/eluser]
All fixed Smile

Decided to use a new function in my Model.

Code:
function getSpecificRecentVacancies($slug) {
          $this->db->where('category_id',$slug);
          $this->db->orderby('pubdate', 'desc');
          //$this->db->where('post_visible', 1);
          $this->db->limit(10);
          return $this->db->get('vacancies');    
  }


Thanks for your time.




Theme © iAndrew 2016 - Forum software by © MyBB