Welcome Guest, Not a member yet? Register   Sign In
My Page View Counter - could this code be more effencent?
#1

[eluser]Latavish[/eluser]
Hi guys,

I have developed a model that will count the number of views a page has has from users (rather logged in or not logged in) and records the info in my database. I have the code and everything seems to work just like I want it to.

Could you guys take a look at this code and tell me what you think? Like I stated, it works fine for me but if it could be done a better or more effecent way then please let me know. I'm also using cookies for this purpose since i dont want viewer to be counted more then once per 24 hrs per page.

here is the model.

Code:
function picviewcounter($picid, $cur_num_views)
    {        
        
        //Check if PicViewCounter(PVC)cookie is set? if not..then set
        if(!isset($_COOKIE['pvc']))
        {
            $cookie = array(
                   'name'   => 'pvc',
                   'value'  => "$id:",
                   'expire' => '86400',
                   'domain' => '.mylocalhost.com',
                   'path'   => '/',
               );

        set_cookie($cookie);
        
        //Add 1 Increment to Current Picture View Count
        $updatedpvc = $cur_num_views + 1 ;
        $this->db->where('id',$id);
        $data = array(
                   'views' => "$updatedpvc",
                            );
        $this->db->update('pictures',$data);
        
        }else{
            
            //Get Cookie Info
            $pvcinfo = get_cookie('pvc');
            
            //Verify User Has Not Viewed This Image Already
            $currentpicviews = explode(":", $pvcinfo);
            
            if(!in_array("$id",$currentpicviews))
            {
                //Then Add View Increment and Add New Pic ID to Cookie
                $updatedpvc = $cur_num_views + 1 ;
                $this->db->where('id',$id);
                $data = array(
                           'views' => "$updatedpvc",
                                    );
                $this->db->update('pictures',$data);
                
                //Update PVC Cookie
                $pvc_values = implode(':',$currentpicviews);
                
                
                $cookie = array(
                   'name'   => 'pvc',
                   'value'  => "$pvc_values".":$id",
                   'expire' => '86500',
                   'domain' => '.mylocalhost.com',
                   'path'   => '/',
               );

                set_cookie($cookie);
            }
        }
    }
#2

[eluser]louis w[/eluser]
- You should check out the Cookie Helper
- You can increment a mysql value without fetching it (From google) UPDATE MyTable SET MyColumn=MyColumn+1 Where MyID=123
- Seems like you do not need 2 separate updates in there. I would just set a flag $update_db = true; or something and check for it in the end.
- Personally I do not like to enclose variables in quotes. e.g. "$updatedpvc"
#3

[eluser]Latavish[/eluser]
[quote author="louis w" date="1212711924"]- You should check out the Cookie Helper
- You can increment a mysql value without fetching it (From google) UPDATE MyTable SET MyColumn=MyColumn+1 Where MyID=123
- Seems like you do not need 2 separate updates in there. I would just set a flag $update_db = true; or something and check for it in the end.
- Personally I do not like to enclose variables in quotes. e.g. "$updatedpvc"[/quote]

Aww.....thanks for the tips..going to make some changes to this code. Even though I does exactly what I want it to do, I felt that it could be more efficient.
#4

[eluser]nmweb[/eluser]
A small thing I noticed, instead of doing this:
Code:
'value'  => "$pvc_values".":$id",
do this:

Code:
'value'  => $pvc_values .':'. $id,

It's faster and more easy to read. Strings between "" are searched for variables whereas strings with '' are not.




Theme © iAndrew 2016 - Forum software by © MyBB