Recently viewed list in PHP Mysql
Recently viewed list in PHP MySQL
Learn How to show recently viewed products list in a PHP website using MySQL Database.
In the previous tutorial, you learn how to get the last five product IDs in an array. In this tutorial, we use MySQL to get the details of each product from recently viewed, and display them in the bottom of the product details page.
Here we're using two files, one is bookdetails.php and another is recentlyviewed.php.
In Bookdetails.php bookid is added to a session array 'lastviewed' every time when user clicks on some book.
bookdetails.php
<?php @session_start(); include "connect.php"; //check if //A) a bookid has been submitted //B) the submitted value is numeric $auth=""; if(isset($_GET['bid'])){ //clean it up if(!is_numeric($_GET['bid'])){ //Non numeric value entered. Someone tampered with the catid $error=true; $errormsg=" Security, Serious error. Contact webmaster: bid enter: ".$_GET['bid'].""; }else{ //book_id is numeric number //clean it up //recently viewed books if(!isset($_SESSION["lastviewed"])) { $_SESSION["lastviewed"] = array(); } $maxelements = 5; if (isset($_GET['bid']) && $_GET['bid'] <> "") {// if we have url parameter if (in_array($_GET['bid'], $_SESSION["lastviewed"])) { // if product id is already in the array $_SESSION["lastviewed"] = array_diff($_SESSION["lastviewed"],array($_GET['bid'])) ; // remove it $_SESSION["lastviewed"] = array_values($_SESSION["lastviewed"]); //optionally, re-index the array } if (count($_SESSION["lastviewed"]) >= $maxelements) {//check the number of array elements $_SESSION["lastviewed"] = array_slice($_SESSION["lastviewed"],1); // remove the first element if we have 5 already array_push($_SESSION["lastviewed"],$_GET['bid']);//add the current itemid to the array } else { array_push($_SESSION["lastviewed"],$_GET['bid']);//add the current itemid to the array } } // recently viewed code end .................. .................. <?php include("recentlyviewed.php"); ?> <div class="clear"></div></div> <footer> <div class="footer-bottom"> <p>© University Bookstore 2019.</p> </div> </footer> </div> </body> </html>
recentlyviewed.php
This page uses session array and get the book details from database and displays. This page is included in the bookdetails page, where the recently viewed list needs to appear.
Step 1: Using the array values.
As the user clicking, the book ID that is added to a Session array 'lastviewed'. Next, this array used to get the bookdetails from MySQL.Step 2: Creating the SELECT query (MySQL).
To list out the books which appear in the array,
inside the loop, we used a select query to get each book details by using the $bid(array element).
Step 3: Displaying the record.
Once the details are obtained, they are used to display the book.
As in example, there is an image tag with a link which will take to the book details page,
and other details like title and price of the book and so on.
This process (step2 and step3) will be repeated inside the loop, until the available IDs in the array.
<?php
@session_start();
include("connect.php");
$error =false;
//print_r($_SESSION["lastviewed"]);
?>
<h2> Recently Viewed </h2>
<div class="productRow">
<?php
foreach($_SESSION["lastviewed"] as $bid)
{
$query ="SELECT * from books WHERE book_id=".$bid;
$results = mysqli_query($con,$query);
if($results){
$num = mysqli_num_rows($results);
}
else{
//there's a query error
$error = true;
$errormsg .= mysqli_error();
}
if(!$error){
if($num > 0){
$rowb = mysqli_fetch_assoc($results);
?>
<!-- Each individual product description -->
<article class="productInfo">
<?php
$img =file_exists("images/".$rowb['book_img'])? $rowb['book_img']: 'default.png';
?>
<div> <a href="bookdetails.php?bid=<?php echo $rowb['book_id']?>">
<img alt="sample" src="images/<?php echo $img ; ?>" height="100" weight="100"></a>
</div>
<p class="title"><?php echo trim(stripslashes($rowb['title']))?></p>
<p class="price"><?php echo "$".trim(stripslashes($rowb['price']))?></p>
</article>
<?php
}
?>
<?php
}else{
?>
<?php
echo "The following errors occurred: ".$errormsg.""
?>
</p>
<?php
}
}
?>
</div>
Sample View of code implementation:
Comments
Post a Comment