terungwa Posted April 28, 2014 Share Posted April 28, 2014 (edited) I am creating a recently viewed article index for my PHP/MYSQL forum script that will store recently viewed articles that a user may have forgoten to bookmark. This way the Web site will be able to remember which articles they read and present a list. To remember which documents have been read by a given user, i am identifying each user and each document by a unique identifier.For the user, the session ID satisfies this requirement. I have got the script working but the viewed list is not incrementing, rather a new viewed article always overwrites the old one. What i want to create is a list of all articles read. My code is below: topic.php session_start() // database connection //Extract articles from database for display on the topics page $_SESSION['post']=array(); //create an array to hold read articles // Add article title and link to list $PostLink = "<a href='topic.php?topic={$topic}'>{$topic}</a>"; if (! in_array($PostLink, $_SESSION['post'])){ $_SESSION['post'][] = $PostLink;} // Output list of requested articles this bit of code is to be place in the sidebar if(isset($_SESSION['post'])){ echo "<p>Recently Viewed Articles</p>"; echo "<ul>"; foreach($_SESSION['post'] as $doc) { echo "<li>$doc</li>"; } echo "</ul>";} Any thoughts on how i may achieve this Edited April 28, 2014 by terungwa Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 28, 2014 Share Posted April 28, 2014 Why do you re-create the array every time you begin??? Quote Link to comment Share on other sites More sharing options...
X5HOST Posted April 28, 2014 Share Posted April 28, 2014 Why don't you add the Article ID and the User ID into a specific table within the database; this method would be efficient, and would never expire unless the user could remove recently viewed articles. Seems much more efficient and will probably be more secure... Kind regards, Jason Moore X5HOST.co.uk Quote Link to comment Share on other sites More sharing options...
terungwa Posted April 29, 2014 Author Share Posted April 29, 2014 (edited) Why don't you add the Article ID and the User ID into a specific table within the database; this method would be efficient, and would never expire unless the user could remove recently viewed articles. Seems much more efficient and will probably be more secure... Kind regards, Jason Moore X5HOST.co.uk Thanks Jason. Even though I have resolved the problem, I shall take a look at your proposal. Edited April 29, 2014 by terungwa Quote Link to comment Share on other sites More sharing options...
Solution terungwa Posted April 29, 2014 Author Solution Share Posted April 29, 2014 Why do you re-create the array every time you begin??? Thanks for the heads up ginerjm. I have wrapped the array in an if statement as shown below and this fixed the bug: if(empty($_SESSION['post'])) { $_SESSION['post']=array(); } Notes: This note is for the benefit of any person who may have need for this solution and needs to understand my code choice. In case the post array(haystack) is undefined, NULL or not set, in_array, will trigger a warning as the second parameter in_array can not be NULL: I therefore created an empty array to store the article identifiers in session variables, only if the session variable array is empty. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.