Page Tracking;

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Page Tracking;

Post 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.....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Page Tracking;

Post 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?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

So what your saying is you want to see which page is getting viewed the most?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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. ^^;
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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. ^_^
Post Reply