Welcome Guest, Not a member yet? Register   Sign In
Something messed up with foreach as $item
#1

[eluser]Lockzi[/eluser]
I'm trying to store some RSS feeds into a database of mine for fun...

Now I'm having a huge-a problem with $descr for some reason...

Code:
foreach ($this->simplepie->get_items() as $item) {
    $href    = $item->get_link();
    $title    = $item->get_title();
    $descr    = $item->get_description();
    
    
    if($this->rss_model->checkNewFeed($href))
    {
        die(var_dump($descr));
        $this->rss_model->insertNewFeed($feedName, $title, $href, $descr);
    }
}

That code produces the following:

string(0) ""

Sure... That make's it look like there's no description... But watch this!

Code:
foreach ($this->simplepie->get_items() as $item) {
    $href    = $item->get_link();
    $title    = $item->get_title();
    $descr    = $item->get_description();
        die(var_dump($descr));
            
    if($this->rss_model->checkNewFeed($href))
    {
        $this->rss_model->insertNewFeed($feedName, $title, $href, $descr);
    }
}

That code dumps this...

string(159) "The title of that feed item bla bla bla"

and I've checked this quite alot, and my conclusion is that whenever I use $this-> somewhere (*edit* between setting it and reading it *edit*) it looses the information in $descr.

Is that a bug?
Is there a way to get around it since I'd like to keep the checkNewFeed() code in the model to keep the database connections in the same place?

Regards,
Christoffer Wallenius
#2

[eluser]coolfactor[/eluser]
I can't see any logical reason why that would be happening with the $descr variable.

Can you post a larger sample of your code? Maybe that entire function?
#3

[eluser]Lockzi[/eluser]
Sure, here you go...

Code:
function index()
    {
        $this->load->helper('url');
        
        $this->load->model('rss_model');
        $this->load->library('simplepie');
        
        $feeds = $this->rss_model->getAllFeeds()->result();
        
        $data['feeds'] = $feeds;
        
        foreach ($feeds as $item)
        {
            $this->simplepie->set_feed_url($item->url);
            $this->simplepie->init();
            $feedName = $item->name;
            
        foreach ($this->simplepie->get_items() as $item) {
            $href    = $item->get_link();
            $title    = $item->get_title();
            $descr    = $item->get_description();
            
            
            if($this->rss_model->checkNewFeed($href))
            {
                $this->rss_model->insertNewFeed($feedName, $title, $href, $descr);
            }
        }
            
            $data[$feedName] = $this->simplepie;
            
        }
        
        $this->load->view('startup_view', $data);
    }
#4

[eluser]Michael Wales[/eluser]
I've just glanced over your code really quick without looking into the entire issue, but you are defining $item in an outer foreach as well as within an inner foreach - could this be causing the issue?
#5

[eluser]coolfactor[/eluser]
For starters, you're doing this inside the outer foreach() loop:

Code:
$data[$feedName] = $this->simplepie;
#6

[eluser]Lockzi[/eluser]
Walesmd:
The problem really could be that I have defined as $item twice, but why is it only description that's the problem then? Shouldn't title and link be the same?

Coolfactor:
I'm doing that to send all the feeds to the view file for output, do you think that could be the problem?

I'm really going nuts about this, especially since it's only the $descr variable that's not working! Thanks for joining me in this mystery Wink
#7

[eluser]Lockzi[/eluser]
[quote author="coolfactor" date="1189772582"]For starters, you're doing this inside the outer foreach() loop:

Code:
$data[$feedName] = $this->simplepie;
[/quote]

Tried removing it, and still returns null when i var_dump inside that if statement.
#8

[eluser]Michael Wales[/eluser]
wow - I was trashed last night and don't even remember making this post...

Can you show us your rss_model?
#9

[eluser]Lockzi[/eluser]
Code:
function checkNewFeed($link = null)
    {
        // Just checking for empty queries!
        if ($link == null)
        {
            die('Link is empty!');
        }    
        
        $query = $this->db->select('link')->from('news')->where('link', $link);
        $res = $this->db->get();
        
        if ($res->num_rows() == 0)
        {
            return TRUE;
        }
        else {
            return FALSE;
        }
        
    }
Code:
function insertNewFeed($feed = null, $title = null, $link = null, $description = null)
    {
        // Just checking for empty queries!
        if ($title == null)
        {
            die('Title is empty!');
        }
        
        if ($link == null)
        {
            die('Link is empty!');
        }
        
        if ($description == null)
        {
            die('Description is empty!');
        }
        
        $queryData = array(
            'feed'    => $feed,
            'title'    => $title,
            'link'    => $link,
            'descr'    => $description
            );
        
        return $this->db->insert('news', $queryData);
        
    }
#10

[eluser]ELRafael[/eluser]
this $item.... first, it looks like an array. like $item['var'] or $item->var.. second, looks a object, $item->metod().

For good pratice, try to rename $item... i know that php has implicit conversion, but it's not a good pratice to make this!




Theme © iAndrew 2016 - Forum software by © MyBB