Jump to content

Recommended Posts

So this is my first time taking the OO approach to PHP and I'm not finding it easy.  A few things are confusing me but here's the scenario I'm stuck with right now...


  • I want to track the popularity of a page by telling how many times it has been viewed.
     
  • The integrity of the statistics may be damaged by guests refreshing the page or visiting it more than once.

 

My solution? Record the ID of the pages the guest has viewed in an array stored as a $_SESSION value - when the guest visits a page check the array for the ID, if it's not there increment the view count of that page by one in the database and add the id of it to the array held in the $_SESSION value.

 

Now my problem is one of design and apologies if this should have gone in the Program Design forum.  What should I do?

 

[*]Declare the session var and create/manipulate the array in the main body of the code.

[*]Declare the session var in the main body of the code and manipulate the array in a class, e.g. statsClass.php

[*]Declare (and name) the session var in the class

 

Also if I choose option 3 do I :

 

A) choose the name of the session var in the main code and pass it in my method call?

B) just choose the name of it in the class so it is always the same

 

I'm soooooooooooooooooooo confused D:  I would love to get comfy with this OOP stuff but real world applications are never as simple as the examples provided in tutorials.

 

Any help is greatly appreciated, also if you operate a non-commercial site I'll throw you a link on a 'friends' list when my site is eventually done.

 

Cheers,

 

Pedro!

Link to comment
https://forums.phpfreaks.com/topic/186465-trying-to-design-with-oo-and-confused/
Share on other sites

If I were you I would have set the session name outside and assign it to a class property

through the constructor or directly (after instantiation)

 

if you're not going to use the rest of the class so often and will use this method alot, just make it a static method for quicker access and less code

 

try not to think too much of OOP in design in the very beginning, you'll learn more by practice.

 

It's vague in the beginning like the abstract class concept didn't make any sense to me in the beginning, after that when I needed it. I understood.

Thanks for your reply BloodyMind I opted for the constructor option...

 

Here is what I've produced, I'd greatly appreciate it if anyone could point out any pitfalls or idiotic things I have done because it isn't working to great:

 

stats.php

<?php //classes/stats.php

class statsClass {

private $pages;

function __construct($varName) {

	$pages = $varName;

	$_SESSION[$pages] = array();

}

function addPage($pageID) {

	$_SESSION[$this->pages][] = $pageID;

}

function lookup($pageID) { //returns true if the Page ID is in the session var

		foreach($_SESSION[$this->pages] as $key => $existingID) {

			if ($existingID == $pageID) {return true;}

			else {return false;}
		}
}

}

?>

 

teststats.php

<?php //music/teststats.php

session_start();

include('../classes/stats.php');

$stats = new statsClass('pagesVisited');  //should create a $_SESSION var called 'pagesVisited'

    $id_of_this_page = $_GET['id'];  //in reality would be fetched from the database

if (!$stats->lookup($id_of_this_page)) {

echo("Page with the id '" . $id_of_this_page . "' not found<br/><br/>");

$stats->addPage($id_of_this_page); //add it so next time lookup returns true

/*run some code to update some stats in the database*/

}

print_r($_SESSION['pagesVisited']);

?>

 

Visiting teststats.php?id=11 will display:

 

"Page with the id '11' not found

 

Array ( )"

 

Visiting it twice or more will display the same but in theory the it should only say "Array('11')".

 

Anyone any ideas? Thanks again.

 

I'm also on the IRC as 'nvm' if anyone prefers chatting there...

No edit button?

 

So I found out my array was getting blanked everytime the constructor was loaded which was dumb.

 

My latest code is here: http://pastebin.com/d6989e591

 

and it has a strange behaviour... the concept works well for the first value in a new session e.g. if I visit http://www.3xe.co.uk/music/teststats.php… '10' will not be found the first time and added to the array...

 

Then if I visit again it will find 10 and not add it to the array. Great.

 

But in the same session, visit it with a new ID and that ID will always be 'not' found and be added to the array every time. :@

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.