My Page View Counter - could this code be more effencent? |
[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)
[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"
[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.
[eluser]nmweb[/eluser]
A small thing I noticed, instead of doing this: Code: 'value' => "$pvc_values".":$id", Code: 'value' => $pvc_values .':'. $id, It's faster and more easy to read. Strings between "" are searched for variables whereas strings with '' are not. |
Welcome Guest, Not a member yet? Register Sign In |