CodeIgniter Forums
Beginner getting a grasp of MVC/CI/PHP and Twitter library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Beginner getting a grasp of MVC/CI/PHP and Twitter library (/showthread.php?tid=22486)

Pages: 1 2 3


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-11-2009

[eluser]jpcody[/eluser]
Hey folks, forgive me if what I'm asking is far too simple, but I'm trying to get a good grasp on things and move from HTML and CSS to more advanced development. I'm trying to implement Simon Maddox's Twitter library to pull the user names, profile pictures and updates of a handful of people on Twitter, then display the information.

I was hoping the community, along with the user guide and php.net could give me a little help with this. Are my fundamentals correct?

• I'll autoload the Twitter library after installation.
• Create a table to store the updates
• Create a model that will plug the user name, ID, etc. into the table
• Create a controller function that will plug the info from Twitter into the table
• Create a controller function that will pass the info from the table to a view
• Create a view which will display the final information

Is this the right way to create this? Can I remove the database/model steps and simply pull from Twitter to display in a view?

Is it bad form to ask the community to guide me through this process of my first CI implementation, post my code along the way, etc?

Thanks so much for your help on this.


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-11-2009

[eluser]Phil Sturgeon[/eluser]
Yes to all of your questions other than the last.

Just read -> plan -> code. Post if you get any troubles.


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-11-2009

[eluser]garymardell[/eluser]
I think most people here would suggest you gave it a shot and then if you really get stuck then ask, its not that we don't want to help but we know that most people after reading the user guide and after writing some code will answer most of their own questions anyway.


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-12-2009

[eluser]jpcody[/eluser]
Great. Thanks so much. Firstly, I'm trying to build my model. I imagine I need to get the Twitter information first.

• I've autoloaded both the 'Twitter' and 'database' classes.
• I think I'm having trouble understanding the Twitter class. I believe I need to use the user_timeline method to get user timelines, but I'm looking to get more parameters than are included by default. I'd like to pull in their posts, pair them with their user id and display the time of the update and their profile picture. I'm looking to just plug all four of these into a database, and sort it by the time posted. I just don't think I understand how to parse the data returned from the user_timeline method
• Here is my model code so far:

Code:
<?php

class Twitter_model extends Model {    
        function Twitter_model(){
            parent::Model();
        }
        
        function insert_tweet(){
        $user = $this->twitter->user_timeline['id'];
        $pic = $this->twitter->user_timeline[];
        $time = $this->twitter->user_timeline[];
        $tweet = $this->twitter->user_timeline[];
        $this->db->query("INSERT INTO ***tablename*** (id, user, pic, time, tweet) VALUES";)
        }
        
        function retrieve_tweet(){
        $query = $this->db->query("SELECT * from ***tablename***");
        return $query;
        }
        
}

Thank you so much for any help you could give me.


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-14-2009

[eluser]jpcody[/eluser]
All right, I've decided to take some steps backwards to try and fix my problem. I've attempted to take the model out of the picture to make sure I can make it work with just the controller and the view. Alas, I cannot.

I'm attempting to use the following:

Code:
<?php
class Feed extends Controller {

    function index()
    {
        $this->twitter->get_user('jpcody');
        $data['tweet'] = $this->twitter->user_timeline('jpcody',2);
        $this->load->view('tweets', $data);
    }
}

And then cycling through it in the view like so:

Code:
<p>hello world!</p>

<p>
&lt;?php echo is_array($tweet) ? 'Array' : 'Not'; ?&gt;
&lt;?php foreach($tweet as $t): ?&gt;
&lt;?php echo $t; ?&gt;
&lt;?php endforeach; ?&gt;
</p>

It appears the user_timeline variable is not being stored as an array or I am screwing it up somehow. Any ideas?


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-14-2009

[eluser]Aken[/eluser]
You can figure out what your $tweet variables is being passed as by putting this somewhere in your view:

Code:
&lt;?php var_dump($tweet); ?&gt;

Then debug from there Smile

Oh, and by the way, you're single line if/else statement should have parentheses around the if part of the statement. It may work okay with PHP (I'm not sure without testing), but it's a good coding practice.

Code:
&lt;?php echo (is_array($tweet)) ? 'Array' : 'Not'; ?&gt;



Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-15-2009

[eluser]jpcody[/eluser]
Thanks so much Aken! That was such a big help. I wish I would have known to do that sooner. (I know this is my own fault for not learning this fundamental more quickly!)

I was getting a boolean false on the value because I had not submitted my auth credentials yet. Once submitting those, I was getting an array, where I was able to do a print_r inside of pre tags to see my entire array and grab specific data. Now just to plug this into a database, run some queries and display the result.

I trust I'll be back with another question I'm stumped by soon, but thanks a lot for this big help!


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-15-2009

[eluser]jpcody[/eluser]
All right, looks like I'm stuck again. That only took a few hours to realize I can't figure the next step out.

I got things working like this. I'll start with some prose explanation to avoid over-coding and make sure my fundamentals are right:

Controller: Feed.php
• Loads model 'Twitter_model'
• puts Twitter_model 'get_tweet' function into $data['tweet']
• loads 'tweets.php' view

Model: twitter_model.php
• defines $tweet as $this->twitter->auth('user','pass')
• returns the user_timeline for myself.

View:
• Runs a foreach($tweet as $t), then dives into the $t multi-dimensional array to get some third-level variables and display them

From here, I tried to move my foreach into my model so I could cycle through the values and store them directly in a database, so I could then sort by date, query the database and display the variables in my view. And I seem to have broken the world. Here's my code:

Model:
Code:
&lt;?php

class Twitter_model extends Model {    
        function Twitter_model(){
            parent::Model();
        }
        
        function get_tweet(){
        $tweet = $this->twitter->auth('user','pass');
        return $this->twitter->user_timeline('jpcody',10);
        }
        
        function insert_tweet(){
            foreach($tweet as $t) {
                $tweet = array(
                'user' => $t->user->name ,
                'pic' => $t->user->profile_image_url ,
                'time' => $t->user->created_at ,
                'tweet' => $t->text
                );
            }
            
            $this->db->insert('updates', $tweet);
        }
        
}

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

    function index()
    {
        $this->load->model('Twitter_model');
        $data['tweet'] = $this->Twitter_model->insert_tweet();
        $data['tweet'] = $this->Twitter_model->get_tweet();
        $this->load->view('tweets', $data);
    }
}

I'm feeling pretty frustrated but determined, and I value answers that point me in the right direction much more than answers that just solve my problems. I'm really hoping to get a good grasp on this for the future as well.

I really appreciate your help and apologize for clogging the boards with basic questions on the same topic again and again. (And for the questions I still anticipate having about this issue.)


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-15-2009

[eluser]n0xie[/eluser]
I'll try to answer.

Let's step to the process of what happens and hopefully you will see where you went wrong and learn something in the process Wink

So we start with your Controller:
Code:
//snippet
        $this->load->model('Twitter_model');
        $data['tweet'] = $this->Twitter_model->insert_tweet();
First it loads the model. This is fine.
Next line what you basically tell your application to do is this:
Quote:Assign the output of the method 'insert_tweet' from the model 'Twitter_model' to my array/key '$data['tweet'].

So your application tries to execute the method 'insert_tweet', just like you tell it to. At this point we look at your model:
Code:
// snippet
function insert_tweet(){
            foreach($tweet as $t) {}
Now the model at this point has no way of knowing who or what '$tweet' is, because you didn't tell it what it is.

So what you need to do is tell your model what '$tweet' is. You can do this in several different ways. First you need to fetch the tweets from Twitter, else you have nothing to work with and then pass this information to your model so it knows what it needs to insert into your database. After that you want to retrieve your data from the database in some order which you can alter. I'll make a quick example:
Code:
// controller
        $this->load->model('Twitter_model');
        $tweet = $this->Twitter_model->get_tweet();
        $this->Twitter_model->insert_tweet($tweet);
        $data['tweet'] = $this->Twitter_model->get_tweet_from_db();

// model
        function insert_tweet($tweet){
            foreach($tweet as $t) {
                $tweet = array(
                'user' => $t->user->name ,
                'pic' => $t->user->profile_image_url ,
                'time' => $t->user->created_at ,
                'tweet' => $t->text
                );
            }
          $this->db->insert('updates',$tweet);
        }

        function get_tweet_from_db()
        {
          // get some data from the database ordered however you want
        }

Hope this was clear and helps. All example code is untested, but should point you in the right direction.


Beginner getting a grasp of MVC/CI/PHP and Twitter library - El Forum - 09-15-2009

[eluser]Aken[/eluser]
Also, inside your model's insert_tweet() function, I'd recommend changing the name of the $tweet array that will be used for the insert() function. Using the same names as other information can lead to confusion and sometimes errors with information being overwritten. This example should work just fine, but it's just good practice in my opinion.