Welcome Guest, Not a member yet? Register   Sign In
Beginner getting a grasp of MVC/CI/PHP and Twitter library
#15

[eluser]jpcody[/eluser]
Thanks so much n0xie, that one is the first thing that has made me feel really dumb! That makes total sense, and now the database is returning all 10 results.

I was really hoping to get this next part on my own, but alas, I'm missing something somewhere:

Code:
function index()
    {
        $this->load->model('Twitter_model');
        $users = array('user1','user2','user3');
        foreach($users as $u){
        $tweet = $this->Twitter_model->get_tweet($u);
        $this->Twitter_model->insert_tweet($tweet);
        }
    }

This is returning 3 sets of 10 updates, but all are coming from user1. Any ideas?

Thanks again everyone for your help. I have got this pretty much up and running with the exception of two issues:

• The code will only iterate through one user, albeit multiple times.
• I think I have an error in my SQL query to INSERT IGNORE, as that's throwing me errors like crazy. (I've tried to troubleshoot this and done a var_dump on both the array and a variable. All of these are successfully returning a string, yet when I attempt to run the query, I'm getting "Object of class stdClass could not be converted to string." Perhaps the arrow operator is not allowed or recognized in a SQL query?)

MODEL
Code:
<?php

class Twitter_model extends Model {    
        function Twitter_model(){
            parent::Model();
        }
        
        function get_tweet($u){
        $auth = $this->twitter->auth('user','pass');
        return $this->twitter->user_timeline($u,5);
        }
        
        function insert_tweet($tweet){
            foreach($tweet as $t) {
                $messages = array(
                'user' => $t->user->name ,
                'sn' => $t->user->screen_name ,
                'pic' => $t->user->profile_image_url ,
                'time' => $t->created_at ,
                'tweet' => $t->text
                );
            $this->db->query("INSERT IGNORE INTO updates (user, sn, pic, time, tweet) VALUES ('$t->user->name', '$t->user->screen_name', '$t->user->profile_image_url', '$t->created_at', '$t->text')");
            }
            
        }
        
        function return_tweet(){
        return $this->db->query('SELECT * FROM updates ORDER BY time DESC');
        }
        
}

VIEW:
Code:
<ul>
&lt;?php foreach($query->result() as $row): ?&gt;
<li><img >pic; ?&gt;" alt="Profile Picture"><h1>&lt;?php echo $row->user; ?&gt;</h1><h2>&lt;?php echo $row->sn; ?&gt;</h2><p>&lt;?php echo $row->tweet; ?&gt;</p><span>&lt;?php echo $row->time; ?&gt;</span></li>
&lt;?php endforeach; ?&gt;    
</ul>

</p>

CONTROLLER:
Code:
&lt;?php
class Feed extends Controller {

    function index()
    {
        $this->load->model('Twitter_model');
        $users = array('user1','user2','user3');
        foreach($users as $u){
        $tweet = $this->Twitter_model->get_tweet($u);
        $this->Twitter_model->insert_tweet($tweet);
        }
        $data['query'] = $this->Twitter_model->return_tweet();
        $this->load->view('tweets',$data);
    }
}

I would love if someone could lend a little help with these final two issues, and I'd also love any suggestions or recommendations about cleaning up or optimizing my code. I'm brand new at this, so anything is welcomed.

Thanks again for all your help folks.


Messages In This Thread
Beginner getting a grasp of MVC/CI/PHP and Twitter library - by El Forum - 09-16-2009, 04:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB