close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

outputting cookie array of IDs

Thread began 3/06/2016 12:36 pm by Christopher West | Last modified 3/23/2016 11:56 am by Ray Borduin | 4636 views | 18 replies |

Christopher WestCommunity Expert

outputting cookie array of IDs

Hi, a while back I asked about using cookies to display "last viewed products slider"
I almost got it working, however its only displaying 1 cookie. so the problem looks like on the page that sets the cookie (its is overwriting the previously saved cookie) how can I store it all in an array which I can then output on my other pages (also not storing any duplicate IDs)

I think on the product detail page I need to save the product ID into a string array (check so its not a duplicate) and then after save the string array into a cookie. then on the in the recordset that accesses the cookie I need to access the array and output it as a products.

php:
On the page that displays all last viewed products: 


<?php
$rsViewedProducts 
= new WA_MySQLi_RS("rsViewedProducts",$ecartdb,1);
$rsViewedProducts->setQuery("SELECT * FROM products WHERE ProductID = ?");
$rsViewedProducts->bindParam("i""".(isset($_COOKIE['ViewedProducts'])?$_COOKIE['ViewedProducts']:"")  ."""-1"); //colname
$rsViewedProducts->execute();
?>


 <?php
while(!$rsViewedProducts->atEnd()) {
?>
        <a href="product.php?Product=<?php echo($rsViewedProducts->getColumnVal("ProductName")); ?>" class="b-link-fade b-animate-go">
        <div class="item4"><img  src="libra/images/<?php echo($rsViewedProducts->getColumnVal("ProductSKU")); ?>.jpg" alt=""  class="responsive-image"></div>
        <div class="b-wrapper">
          <h2 class="b-from-left b-animate b-delay03"><?php echo($rsViewedProducts->getColumnVal("ProductName")); ?></h2>
          <p class="b-from-right b-animate b-delay03">£<?php echo($rsViewedProducts->getColumnVal("ProductPrice")); ?></p>
        </div>
        </a>
        <?php
$rsViewedProducts
->moveNext();
}
$rsViewedProducts->moveFirst(); //return RS to first record
?>



php:
on the page that saves a cookie



<?php
$rsProduct 
= new WA_MySQLi_RS("rsProduct",$ecartdb,1);
$rsProduct->setQuery("SELECT * FROM products WHERE ProductName = ?");
$rsProduct->bindParam("s""".(isset($_GET['Product'])?$_GET['Product']:"")  ."""-1"); //colname
$rsProduct->execute();?>
<?php
if ("" == "") {
    
setcookie("ViewedProducts""".($rsProduct->getColumnVal("ProductID"))  .""time()+(60*60*24*30), "/""localhost"0);
}
?>
Sign in to reply to this post

Ray BorduinWebAssist

You would want to save the productID values in the cookie comma-separated that way you could store more than one. Then you could use an IN instead of = to and an "il" (integer list) as the parameter type in your recordset to return all of the previously viewed products instead of just the last one.

Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

ah just saw your reply after I was updating this post: this is what I added :
I think on the product detail page I need to save the product ID into a string array (check so its not a duplicate) and then after save the string array into a cookie. then on the in the recordset that accesses the cookie I need to access the array and output it as a products.

With your approach I didnt see an option for cookie comma-seperated way? See screenshot

is this part of the webassist server behaviours or is it custom code? How would I go about coding that?

Sign in to reply to this post

Ray BorduinWebAssist

It isn't an option in our server behavior. It would require hand coding.

You would check to see if the cookie had a value in an IF statement and add a comma before the value if it had a value, if not then you just set the value as it is done now. It is another issue that requires hand coding and I could help with in a premier ticket if you had difficulty pulling it off on your own. Something like:

<?php
if ("" == "") {

$newCookieVal = isset($_COOKIE['ViewedProducts'])?$_COOKIE['ViewedProducts']:"";
if ($newCookieVal) {
$newCookieVal .= ", ";
}
$newCookieVal .= $rsProduct->getColumnVal("ProductID");
setcookie("ViewedProducts", "".$newCookieVal ."", time()+(60*60*24*30), "/", "localhost", 0);
}
?>

Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

ok I will add this and try and see how it works with my code. Thanks

Sign in to reply to this post

Christopher WestCommunity Expert

Hi Ray, in Firebug I can see that the product IDs are being added. however after more than 1 product IDs get added to the cookie then I get a PHP error in syntax message (whats the php debug code to see what the error relates to?)

on the page with the error my recordset is:

Which I thought would work since I selected the comma-separated list checkbox in the recordset advance tab (see screenshot)

Chris

php:
<?php

$rsViewedProducts 
= new WA_MySQLi_RS("rsViewedProducts",$ecartdb,1);
$rsViewedProducts->setQuery("SELECT * FROM products WHERE ProductID = ?");
$rsViewedProducts->bindParam("il""".(isset($_COOKIE['ViewedProducts'])?$_COOKIE['ViewedProducts']:"")  ."""-1"); //colname
$rsViewedProducts->execute();
?>




I did change to this:

However I managed the Error in SQL syntax alert comes up if I clear the cookies. Also it doesnt display the product images just the one.

php:
<?php

$rsViewedProducts 
= new WA_MySQLi_RS("rsViewedProducts",$ecartdb,1);
$rsViewedProducts->setQuery("SELECT * FROM products WHERE ProductID IN ?");
$rsViewedProducts->bindParam("il""".(isset($_COOKIE['ViewedProducts'])?$_COOKIE['ViewedProducts']:"")  ."""-1"); //colname
$rsViewedProducts->execute();
?>



In the while loop I am using this code:

php:
<?php

while(!$rsViewedProducts->atEnd()) {
?>

html.......................

 <?php
$rsViewedProducts
->moveNext();
}
$rsViewedProducts->moveFirst(); //return RS to first record
?>



I think if I can just this this solved here that be brilliant as I think its almost fixed.

Sign in to reply to this post

Ray BorduinWebAssist

That second one looks correct. IN with integer list should do the trick. I'd have to debug at this point to determine the issue.

Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

See PM

Sign in to reply to this post

Ray BorduinWebAssist

I've fixed your page and prevented repeated entries to the cookie.

The issue was in the first line of the recordset:
$rsViewedProducts = new WA_MySQLi_RS("rsViewedProducts",$ecartdb,1);

The "1" at the end is the maximum number of rows to return... updating it to 0 allows it to return all rows.

Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

Hey Ray, when I tested it on the live server (the one you have access to) it worked. However in FireFox FireBug, if I cleared the cookie and try to access the category.php page I get the same error Error in SQL syntax.
Please could you check but clearing the cookies and see why it has that error?

Chris

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...