Welcome Guest, Not a member yet? Register   Sign In
Creating a tracking table that is updated every day
#1

Hello,

I'm trying to create a tracking table which will be updated every 24 hours with cron.
The information will be used to draw a line chart (similar to what you can find on google analytics).
Here is an example of what I want to achieve. 

[Image: Instagram-Followers-Growth-uai-720x424.png]

Also, I have a table for Projects, that stores social media links, and I want to insert the Project ID, Social Media link, date, and followers number for that day into a Tracking Table.

This is my model right now: 

Code:
<?php
class Tracker_model extends CI_Model {
    
    #insert follower count 
    public function twitter_followers(){
                        
       $data = array (
            'ico_id'      => $ico_id,
            'twitter_url' => $twitter_url,
            'year'        => date("Y"),
            'month'       => date("m"),
            'month_word'  => date("F"),
            'day'         => date("d"),
            'day_word'    => date("l"),
            'week'        => date("W"),
            'followers'   => $parsed_followers
        );
        
        $insert_followers = $this->db->insert('twitter_followers', $data);
        return $insert_followers;
    }
}
?>

I want to get information from the Projects table and put them into the $ico_id, $twitter_url, $parsed_followers variables, and then insert new rows for each project. In this case, it would be around 200 new rows. 

I don't really know where to start. Put the Insert function into a foreach() loop? 

I tried doing this at the start of my model:

Code:
        $query = $this->db->get('icos');
        return $query->result();

            foreach($query->result() as $row){
                $ico_id      = $row->id;
                $twitter_url = $row->twitter;
            }

But I couldn't get it to work. I'm still trying to figure it out, but maybe there is the right way to do it which is applied in the real world. 

Also, another question!
Can I run just the model with cron? That would be amazing.

Looking forward to your answers!
Reply
#2

Found a solution, this is the code that worked for me: 

Code:
<?php
class Tracker_model extends CI_Model {
    #insert follower count 
    public function twitter_followers(){
        $query = $this->db->get('icos');
        if($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $ico_id = $row->id;
                $twitter_url = $row->twitter;

                $page = @file_get_contents($twitter_url);
                $followers = @explode("followers_count&quot;:", explode(",&quot;friends_count", $page)[0])[1];  

                $data = array (
                    'ico_id'      => $ico_id,
                    'twitter_url' => $twitter_url,
                    'year'        => date("Y"),
                    'month'       => date("m"),
                    'month_word'  => date("F"),
                    'day'         => date("d"),
                    'day_word'    => date("l"),
                    'week'        => date("W"),
                    'followers'   => $followers
                );

                $insert_followers = $this->db->insert('twitter_followers', $data);
            }
        } else {
            return false;
        }
    } 
}
?>

Now the question is, how to set up a cron for this function to run every 24 hours? And in some cases, the function stops if it exceeds 30 seconds.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB