Jump to content

How to keep the data of a user's 5 recently viewed pages?


hotdog1983

Recommended Posts

Hi, I have a mysql table like this

-----------------------------------------------

UserID    Email    Joined    Country

-----------------------------------------------

Justin    jj@a.com 110815    USA

-----------------------------------------------

 

What I want to achieve is to keep the 5 recent pages viewed by each user.

For example,

---------------------------------------------------------------------------------------------------

UserID    Email    Joined    Country    Act5      Act4      Act3      Act2      Act1     

---------------------------------------------------------------------------------------------------

Justin  jj@a.com 110815    USA        8.php    6.php    5.php    3.php    1.php   

---------------------------------------------------------------------------------------------------

 

So only the 5 recent pages are inserted and when the user views the 6th page,

the oldest record gets dropped and the 4 last pages moves to the right by 1 column.

---------------------------------------------------------------------------------------------------

UserID    Email    Joined    Country    Act5      Act4      Act3      Act2      Act1     

---------------------------------------------------------------------------------------------------

Justin  jj@a.com 110815    USA                      8.php    6.php    5.php    3.php   

---------------------------------------------------------------------------------------------------

 

And then the last viewed page gets inserted.

---------------------------------------------------------------------------------------------------

UserID    Email    Joined    Country    Act5      Act4      Act3      Act2      Act1     

---------------------------------------------------------------------------------------------------

Justin  jj@a.com 110815    USA        11.php    8.php    6.php    5.php    3.php   

---------------------------------------------------------------------------------------------------

 

 

I know how to use php to take the page name value to mysql query but I don't know

how to use the SQL commands to do this.

 

Does anyone know how to do this? or is there a better way to do it?

 

Some help would be great, thank you.

Link to comment
Share on other sites

i would setup a relational database-

users; userid (auto_increment - primary key), email, joined, country

pageViews; pageView (auto_increment - primary key), page, datetime, userid

 

link users table to the pageViews table and insert the pageViews into the pageViews table - with added information such as datetime so you know when they viewed that page.

 

If you want to limit this to 5, then you can run a

$check = @mysql_query("SELECT pageView FROM pageViews WHERE user_id='{$_SESSION['user_id']}'");
if (mysql_num_rows($check) >= 5) {
@mysql_query("DELETE FROM pageViews ORDER BY `datetime` LIMIT 1");
}

//insert page view

 

I would be inclined to store all the pageviews and not commit to deleting, also I've assumed you have the user id kept in the session somewhere? if not, you can change the session value to a $_GET value or however you determine the userID you're searching for.

I would be

Link to comment
Share on other sites

Thank you for the replies. I'll go look into table linking.

 

Both of you recommended keeping all the pageview data instread of deleting them,

I was just worried if it's going to slow down the server when the database gets big.

But it's better if I can keep them.

 

@The Little Guy, I don't understand that method. If I make 3 columns like that,

would I be able to put more than 1 data into the page column? Sorry if I got it wrong.

Link to comment
Share on other sites

I think The Little Guy is saying to make a table named "History" with the 3 fields of id, user_id and page. Then when a user go to a page it logs that user with new id so it would be similar to:

 

id            user_id            page

------------------------------------------

1                123                  6.php

2                123                  3.php

3                123                  1.php

4                123                  2.php

5                234                  2.php

6                345                  12.html

7                123                  15.php

8                123                  11.php

Link to comment
Share on other sites

i think you should go with the pageviews / history table - all three of us seem to agree on the one solution;

and don't worry about slowing the database with too many records - you can set up some cronjobs later to delete any records over a certain amount for each user.

pageViews; pageView (auto_increment - primary key)' date=' page, datetime, userid[/quote']

I would create a table with 3 columns:

id, user_id, page

table named "History" with the 3 fields of id' date=' user_id and page[/quote']
Link to comment
Share on other sites

i would setup a relational database-

users; userid (auto_increment - primary key), email, joined, country

pageViews; pageView (auto_increment - primary key), page, datetime, userid

 

link users table to the pageViews table and insert the pageViews into the pageViews table - with added information such as datetime so you know when they viewed that page.

 

If you want to limit this to 5, then you can run a

$check = @mysql_query("SELECT pageView FROM pageViews WHERE user_id='{$_SESSION['user_id']}'");
if (mysql_num_rows($check) >= 5) {
@mysql_query("DELETE FROM pageViews ORDER BY `datetime` LIMIT 1");
}

//insert page view

 

I would be inclined to store all the pageviews and not commit to deleting, also I've assumed you have the user id kept in the session somewhere? if not, you can change the session value to a $_GET value or however you determine the userID you're searching for.

I would be

Actually, it's much better to use INSERT ON DUPLICATE KEY UPDATE, or REPLACE with a UNIQUE KEY, if you're actually going to maintain a stack.

Link to comment
Share on other sites

Actually, it's much better to use INSERT ON DUPLICATE KEY UPDATE, or REPLACE with a UNIQUE KEY, if you're actually going to maintain a stack.

I was thinking that though I didn't know how you could use insert, on duplicate to check that there are 5 values and then update/replace the last?

Link to comment
Share on other sites

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.