CodeIgniter Forums

Full Version: Using Simple Stats: Attemp #1 ;).
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mitnick[/eluser]
Hi,

I'm pretty new to Codeigniter and still trying to figure things out. After going trough the forums a couple of times, i must say that i love the community behind Codeigniter and this is my first attempt for help... Wink.

I have built a small CMS application for a personal site and it's almost completed, i however would like to include a built in Statistics instead of using Google Analytics etc.

I was going trough the WIki when i ran into Simple Stats http://codeigniter.com/wiki/Simple_Stats/

I however have a couple of questions if you don't mind. I understand that these documents show the controller and the code to Log the site.

1. From where do i log the site and input the "log_site(1);" code. Do i insert this in my Application Controller?.

2. How can i save all the data this controller records to a table in the Database?.

I'm sorry if they are dumb questions, as i told you i'm pretty new here and still learning how Codeigniter works and only have basic knowledge of PHP.

Thank in advance, all help appreciated....

El Forum

[eluser]danmontgomery[/eluser]
1. The controller class constructor
2. Using the code in the wiki without modification, it looks like you would have to create a model called Logmodel, and give that class a function log_site() which accepts all of the parameters passed in that code. You would handle the statistics however you like in that function, unless I'm missing something that plugin isn't anything more than a helper function which passes some data to a model and function which aren't written.

El Forum

[eluser]WebsiteDuck[/eluser]
You put the log_site(1); in the controller constructor for every controller you want to log, like so:
Code:
class Mycontroller extends Controller {
  function __construct()
  {
    parent::Controller();
    log_site(1);
  }

  function index()
  {
    ...
  }
}

You have to create a helper that has that function from the wiki in it, then autoload the helper in /application/config/autoload.php

CodeIgniter User Guide: Helpers

You also have to create log_model.php in your application models folder, then create a log_site function in it that saves the info to the database, like noctrum said.

CodeIgniter User Guide: Models

El Forum

[eluser]mitnick[/eluser]
[quote author="WebsiteDuck" date="1263002715"]
You also have to create log_model.php in your application models folder, then create a log_site function in it that saves the info to the database, like noctrum said.
CodeIgniter User Guide: Models[/quote]

Ahh, that would then be the only problem, can you help me to write the "log_model" model so it imports these informations to the database, i'm sure that it would be very useful to add that info in the wiki.

Thank you so much for your answers, i really appreciate your help.

El Forum

[eluser]WebsiteDuck[/eluser]
/application/models/Logmodel_model.php
Code:
class Logmodel extends Model {

    function log_site($zone, $general, $specific, $item, $session, $ip, $user_id, $user_agent, $browser, $browser_version, $os, $agent)
    {
        $entry = array(
          'zone' => $zone,
          'general' => $general,
          'specific' => $specific,
          'item' => $item,
          'session' => $session,
          'ip' => $ip,
          'user_id' => $user_id,
          'user_agent' => $user_agent,
          'browser' => $browser,
          'browser_version' => $browser_version,
          'os' => $os,
          'agent' => $agent
        )

        $insert = $this->db->insert('log_entries', $entry);
        return $insert;
    }
}
You have to make a table called log_entries in your database to hold this info also.

Let me know if it works