1

I have a problem. Is any possibility to put my xml file into a database and access them from it. I have this function that get content from a xml file:

function getFeed()
{
    $content = file_get_contents('http://feeds.reuters.com/reuters/technologyNews');
    $x = new SimpleXmlElement($content);
    foreach($x->channel->item as $entry) {
        $feeds = array('title' => (string)$entry->title
                );
        $this->session->set_userdata($feeds);
        $this->load->model('membership_model');
        $this->membership_model->feeds();
    }   

And a model who insert the titles into a database and after this get the titles as a link format. But i guess is not a good idea because is not working.

function feeds()
{   
    $this->db->empty_table('feeds');
    $this->db->set('title',$this->session->userdata('title'));
    $this->db->insert('feeds');
    $get_feed = $this->db->get('feeds');
    foreach ($get_feed->result() as $row){
        echo "<a href='$row->title'>".$row->title."</a><br/>";
        }
user3313651
  • 105
  • 1
  • 1
  • 8

1 Answers1

0

You can do with insert batch

First prepare the array for batch insertion.

function getFeed()
{
    $content = file_get_contents('http://feeds.reuters.com/reuters/technologyNews');
    $x = new SimpleXmlElement($content);
    foreach($x->channel->item as $entry) {
        $feeds[] = array(
                         'title' => (string)$entry->title,
                         'url'=> (string)$entry->url, // ad more fields if needed                        
                );

        $this->load->model('membership_model');
        $this->membership_model->feeds($feeds);
    }
 //load view with data. No need to get same data from db again.
 $this->load->view("feed_list",array("feeds"=>$feeds));
}

In model:

function feeds($feeds_data)
{   
    $this->db->empty_table('feeds');   
    $this->db->insert_batch('feeds', $feeds_data);    
}

In view

 foreach($feeds as $f)
{
   echo "<a href='".$f["url"]."'>".$f["title"]."</a><br/>";
}
Kumar V
  • 8,810
  • 9
  • 39
  • 58
  • the url does not work. In the view when i click on one link it not redirect me on the site. – user3313651 Feb 22 '14 at 15:06
  • kumar_v. is there on_duplicate_key_update function for codeigniter? i want to insert only the row is not exists yet. And if the row exists i want to update it. – user3313651 Feb 23 '14 at 14:27
  • try this : http://stackoverflow.com/questions/3361490/how-would-i-use-on-duplicate-key-update-in-my-codeigniter-model – Kumar V Feb 23 '14 at 14:57