Jump to content

Pedro!

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Pedro!'s Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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. :@
  2. 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...
  3. 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!
  4. Pedro!

    Sup folks

    *original quip here* Sup guys, I'm not sure how much I'll be around here but I'm sure your a friendly bunch so I'll look forward to getting to know you all as much as I can. It's a shame IRC is so quiet freenode has a web client so no excuses for not hopping onto http://webchat.freenode.net/ and visiting ##phpfreaks! My name on there is nvm if anyone is about. Right, now I'm going to go to the help forum and leach some knowledge
×
×
  • 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.