Page 1 of 1

Page Tracking;

Posted: Sat Sep 24, 2005 6:54 pm
by neophyte
I'm trying to build a script where the page a guest is viewing is captured in the db. I've seen this before where a page number is captured as opposed to the string file name. My question is do you do this with a db table or a switch function or some other means.... Why caputre a number as opposed to the string file name?

Thanks.....

Re: Page Tracking;

Posted: Sat Sep 24, 2005 7:11 pm
by feyd
neophyte wrote:My question is do you do this with a db table or a switch function or some other means....
I'm not understanding this.
neophyte wrote:Why caputre a number as opposed to the string file name?
  1. number is compact in storage requirements
  2. a file may have more than one page in it that you may want to track seperately
  3. multiple files may share the same "page"
  4. if you change the filename, you lose all previous counts for that page, now don't you?

Posted: Sat Sep 24, 2005 7:16 pm
by Buddha443556
What you capture I guess would depend on your site. If you have a dynamic site where every page can be identified by an ID number then that might be all you need. Me I capture the relative path as every page is an individual file on my sites.

Posted: Sat Sep 24, 2005 8:12 pm
by josh
If you have a table that holds your content, it's often convienent to just add a "viewed" field, and increment that on each page view.

Posted: Sat Sep 24, 2005 8:20 pm
by neophyte
Buddha443556 wrote:If you have a dynamic site where every page can be identified by an ID number then that might be all you need.
This is what I'm talking about Feyd. I'm trying to do this. My site does have multple views on a single file. Do you have to hard code every instance like:

Code: Select all

$page = 1
Or do you write a function to calculate it. I'm just looking for suggestions on the best way to do this.

Posted: Sat Sep 24, 2005 8:23 pm
by Smackie
So what your saying is you want to see which page is getting viewed the most?

Posted: Sat Sep 24, 2005 8:52 pm
by feyd
you can do either hardcoding or calculation, it's up to you. If you're going to have a small number of pages, you can simply hardcode the number or fetch it from a secondary mapping table (filename/circumstances to page id), otherwise you can use the algorithm. I often use a little bit of both.. I hardcode a base page id (typically through the database), then I add to that specific amounts for the circumstances of the request to differenciate between pages within.

Posted: Sat Sep 24, 2005 9:26 pm
by Skara
I think I see what you want to do... I'd do something like this, just to make it quick.

Code: Select all

//counter.php
$pages = array(
  '/page1.php' => 1,
  '/page2.php' => 2,
);
//query database something like...
mysql_query("UPDATE tb SET count=count+1 WHERE page='".$pages[$_SERVER['PHP_SELF']]."' LIMIT 1");

Code: Select all

//page#.php
include 'counter.php';
//...
Entirely up to you how you want to do it, though. The above means you only need one line per page, and all your page numbers are in one spot.
On the other hand, you could simply store the filenames themselves in the database rather than ids.

This is assuming you don't have too many pages. If you have a few hundred.... heh.
Also, if you want to pages with variables as separate entries, you'll have to modify a bit. (/page1.php?view=1 different form /page1.php?view=2)

[offtopic] http://webgeneius.com/ << geneius. funny. ^^;

Posted: Sat Sep 24, 2005 9:58 pm
by neophyte
I think with this small site I'll hard code views. $page = 0; ect... inside of the various members.

Then I'll create a function where specific views are stored. Not specific pages.

Code: Select all

function get_view($page){
     switch ($page){
            case '1':
                 $view = 'homepage';
              break;
             case '2':
                 $view = 'post';
              break;
  }
                 return $view;
}

Skara wrote:
[offtopic] http://webgeneius.com/ << geneius. funny. ^^;
[/quote]

Thanks. I'm plannng on making a "theme switcher" for the site. I like the transparency on your site Skara.


*edit note: I can't post today.... sorry.

Posted: Sun Sep 25, 2005 11:48 am
by Skara
hm. I suppose that'll work well if you have multiple cases for one view, e.g.

Code: Select all

switch ($page) {
  case 1:
  case 2:
  case 3:
    $view = 'homepage';
    break;
  case 4:
  //...
}
but if it's one case per view, It would be much more effecient if you used an array.

Code: Select all

function get_view($page) {
  $pages = array('homepage','post','next',...);
  if ($page < count($pages)) return $pages[$page];
  else return 'Undefined Page';
  // e.g. if you have 10 elements and you pass 12 for $page, you won't get an error, per se.  
}
I like the transparency on your site Skara.
Nowhere close to finished with it, but thanks. ^_^