Jump to content

Recommended Posts

am currently building the cart system of a product, now there is this part wherein, the non-logged-in OR logged-in user should also be able to see the items that he/she viewed, how to do that?., am not asking for code snippets , just give me some ideas/hints/strategies/tips that may help me get the big picture on how to do this thing and proceed coding. :shrug:

Link to comment
https://forums.phpfreaks.com/topic/257605-you-viewed-items-strategy/
Share on other sites

You would use a session variable ($_SESSION['viewed'][n] = true;, where n is a product id) to remember the product id's that the person has viewed. To display the product information, use array_keys to get the list of id's from the $_SESSION['viewed'] array.

You would use a session variable ($_SESSION['viewed'][n] = true;, where n is a product id) to remember the product id's that the person has viewed. To display the product information, use array_keys to get the list of id's from the $_SESSION['viewed'] array.

 

ok, if that's the case, what if the person viewed alot of items? , i think it's uncool to paginate the data in the small box, what to do next ? or there's a better way?

If you only want to keep a specific number of recent items, you would push the values onto the end of an array and only keep the last n items -

 

<?php
$n = 5; // number of recent items to remember

if(isset($_GET['id'])){
$id = (int)$_GET['id'];
if(!in_array($id,$_SESSION['viewed'])){
	$_SESSION['viewed'][] = $id;
	$_SESSION['viewed'] = array_slice($_SESSION['viewed'],-$n,$n); // keep last n entries
}
}

You could also prioritize items that have been viewed multiple times so like above with the session variable, but instead of true/false have it count the number of times it's been viewed. I know personally I usually look at something and come back to it again several times before I buy online.

 

You could order from most viewed to least viewed and call it something like, "Other items you've recently been interested in."

ok guys,

 

I ended up with my own function

 


//from the class file

    public function displayItemViewed(){
      $image = '';
      $catid = '';
      foreach(array_keys($_SESSION['viewed']) as $item){
        $image = $this->getProductImage($item);
        $catid = $this->getCatByProductID($item);
        if(count($_SESSION['viewed']) <= 5){
          echo '<li>
                  <a alt="'.$item.'" title ="'.$catid["ProductName"].'"href="catalog-details.php?prod_id='.$item.'&cat_id='.$catid["ProductCatID"].'"><img src="module/newmodule/products/'.$image['ImageLocation'].'"></a>
                </li>';
        } else {
          array_shift($_SESSION['viewed']);
          reset($_SESSION['viewed']);
        }
      }
    }

 

 

//inside the you also viewed items box
                        <ul>
                            <?=$myCart->displayItemViewed();?>
                        </ul>

 

//from the product details page from where I set the session

$productid = $_GET['prod_id'];


if(isset($productid)){
    $_SESSION["viewed"][$productid] = true;
}

 

 

now my problem is, whenever i run everything...my function seem to display a 0 key?, when it shouldn't

that's why there are some instances where my "you also viewed items" has 1 item missing in the set of five ,

how to fix this ?

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.