Jump to content

Creating view count


LeiHa
 Share

Recommended Posts

Hi guys.

This time, I'm trying to make a view count in my custom forum page.

Goal for my custom view counter is:

1. View count value increase once a registered user load this page at the first time.

2. View count value doesn't change if a same registered user loads a same page.

3. View count value dose change if a same page is modified even though a user visited before.

What I've got from search through internet, there are so many way of creating it.

Cookie, I think, doesn't work I guess because it either expires or deleted by user or browser.

Session might not work. I'm just guessing.

So, I'm trying to make some kind of a holder for the view count value.

I'm trying to create a field using API.

Then that field is attached either page or user template.

If it is in a page template, this field holds user IDs who loads this page.

This field should hold many other users IDs(thinking of an array datatype)

Once a same user loads a page and that user id is in the field of a page, view count doesn't change.

If a page has been modified, id of an user id is removed. So that whenever a user loads modified page, it can change view count value.

This is my plan.

Now that I've found some problem or I'm still not know how to solve it, yet.

I can't find any field that can hold array datatype.

If any of you know how to do this or have different solution, I would really appreciate it.

Thank you. :rolleyes:

Link to comment
Share on other sites

Thanks, adrian.

I've looked through those two posts.

I think they are great for the visit counter.

However, in my case, I need to find different solution.

Those two posts are increase view count no matter what situation. This means it increase every time a page loads.

My goal is to stop increasing view count once an user visit and load a page after second time.

But, it will increase once page has been modified some how.

Thank you.  :rolleyes:

Link to comment
Share on other sites

I think your best choice is the Page fieldType holding the users that have visited the page. Add onePage fieldType to the templates of the pages you need to monitor and  choose the Users page as parent of selectable pages. Then use the API to start populating it.

if(!$page->counter->has($user)) {
    $page->of(false);
    $page->counter = $user;
    $page->save();
}

and to get your view count:

count($page->counter)
  • Like 1
Link to comment
Share on other sites

Right - just thought it might help - wasn't sure how far along you were. You might likely have already thought of this, but you could serialize or json_encode the array of users who have already visited the page and store that in a text field which you can do your check against.

The array could contain the ID for each user who has visited the page. Within that you'd need to store the number of visits as well as the last datetime they visited.

Then, if I understand correctly, each time the user visits, you can check if the user has visited once already and if they have and the date of that visit was before the modified date of the page, then increase counter, if not then load the required page. Or something like that anyway.

Obviously you'd need to unserialize etc the array and search through it each time the page is loaded.

After all that, I feel like a better solution might be a separate db table which adds a row every time the page is visited. The fields would be page_id, user_id, and datetime. Then you could easily query this to count the number of visits for that user_id and whether they were before the page modified date or not. I am sure you could do this through the PW API if you wanted to. You could set up a parent page called page-visits and add a new child page for every visit. Alternatively, it might be simpler to do it with SQL. Perhaps a PW guru could suggest which would be better, or more than likely, they'll come up with a much simpler solution altogether :)

Link to comment
Share on other sites

Thanks, adrian and diogo.

I've took diogo's solution.

It works perfectly well.

I've created Page field to hold user data array and attached to the template I will use.

And another integer field to hold view count number and attached to same template.

And put this code every time a page loads.

if (!$this->page->forum_visited_users->has($this->user)){
	$this->page->of(false);
	$this->page->forum_visited_users = $this->user;
	$this->page->forum_num_views++;
	$this->page->save();
}

Then, every time page has been edited or deleted, page array reset. And put a current user data in the array so that it doesn't increase view count.(because an user who edit or delete is already read this post)

$page_sent->of(false);					
$page_sent->forum_visited_users->removeAll();
$page_sent->forum_visited_users = $user;

One problem is whenever edit or delete reply post, which is a child of topic post, somehow it doesn't work.

topic post is the parent of reply post.

below code is not working. I don't know why.

$page_sent->parent->of(false);
$page_sent->parent->forum_visited_users->removeAll();
$page_sent->parent->forum_visited_users = $user;
$page_sent->parent->save();

Even though I got new problem, this is a huge improvement.

I really appreciate it.

Love you guys.

Thank you. :rolleyes:

  • Like 1
Link to comment
Share on other sites

oh well.

I've found out why that didn't work.

$page_sent = $this->pages->get($post->fid);
$page_sent->of(false);
if ($post->type == 'edit_topic') {
     $page_sent->title = $post->subject;
     $page_sent->forum_visited_users->removeAll();
     $page_sent->forum_visited_users = $user;
}
else{
     $page_sent->parent->of(false);
     $page_sent->parent->forum_visited_users->removeAll();
     $page_sent->parent->forum_visited_users = $user;
     $page_sent->parent->save();
     $page_sent->parent->of(true);
}
$page_sent->body = $post->message;
$page_sent->save();
$page_sent->of(true);

$page_sent->of(false); was inside of if statement.

I've moved it before the if statement.

Now that it works, I still don't understand why.

I've put the of(true) after save() I think I should do this. or not.

A lot of coding I'm writing on are still confusing. sometimes I don't know what I'm doing. lol

Thank you. :)

Link to comment
Share on other sites

$page_sent->of(false); was inside of if statement.

I've moved it before the if statement.

Now that it works, I still don't understand why.

It's because you have $page_sent->save(); also outside the if.

I've put the of(true) after save() I think I should do this. or not.

Yes, if you will use fields from this page later in the code sequence. If not, you don't need to.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...