When we are working on a project, some modules are usually the browsing record list. but how can we implement the browsing record function in PHP, now we use cookies to implement the browsing record list in some modules when we are doing projects. but how can we implement the browsing record function in PHP, now we use cookies to implement
Let's analyze the ideas first.
Access page
Obtain the product data list in the storage browsing record
If the currently browsed item is not included in the history, it is added. If yes, it is updated.
If you know the idea, you can directly view the code.
The complete code is as follows:
5) {// bring up the last unit of the array until its length is less than or equal to 5. array_pop ($ history) ;}// concatenate the array with commas (,) into a string to write the COOKIE, and set the expiration time to 30 days setcookie ('shop [history] ', implode (', ', $ history), $ cur_time + 3600*24*30 );} else {// if the COOKIE is empty, the ID of the currently browsed item is written into the COOKIE, which occurs only when the website is browsed for the first time: setcookie ('shop [history] ', $ id, $ cur_time + 3600*24*30);} // the IDs of the browsed items are recorded in the COOKIE, the following describes how to use the data in the COOKIE to obtain the data in the COOKIE in the format of 1, 2, 3, and 4. of course, it can also be 0 $ history = isset ($ _ COOK IE ['shop'] ['History '])? $ _ COOKIE ['shop '] ['History']: 0; // write an SQL statement, use IN to query the product list of these IDs $ SQL _history = "SELECT * FROM 'goods' WHERE 'goods _ id' in ({$ history })"; // execute the SQL statement and return the data list $ goods_history = $ db-> getAll ($ SQL _history); if ($ goods_history) {$ tpl-> assign ('goods _ history ', $ goods_history) ;}?>
The following mysql operations that obtain product information based on the storage ID can be obtained based on your own code, you can use the original ecology, or you can use the database query method of the framework, you can modify it as appropriate