View Full Version : Sessions and recordset
victor405909
08-01-2010, 12:29 AM
I am still having a bit of problem trying to create a recently viewed items application.
I find <?php session_start();> should be placed at very start of pages requiring session variable.
I want to store Product IDs that will be generated by visitor when they view product details page
Then I want to use these ids to be displayed in a recordset called rsRecentlyViewed.
I can't seem to store a visitors session i.e. items with specific ids so a recordset can filter and display thumbnail, title and price in a particular div from db.
What do I do next ?
Also another question along same lines is how can more than one session variable be made available for receiving page to use? I am also attempting to create an upselling feature as well on same page in regards to this (can you have more than one recordset on a page).
Thanks
CraigR
08-01-2010, 06:47 AM
to have more than one session variable available, do the following...
<?php
if (!session_id()) session_start();
if(!isset($_SESSION["numberone"])) {//if session 'name' hasn't been set
$_SESSION["numberone"] = "10"; //set session 'name' to a value
}
if(!isset($_SESSION["numbertwo"])) {
$_SESSION["numbertwo"] = "5";
}
?>
you can have many session variables and many recordsets on a page.
you can use the session variable as a filter parameter in your recordset when you do the recdorset binding
victor405909
08-01-2010, 10:35 PM
That is excellent I am gonna try this out.
One thing I wanted to know.
How is a session variable generated by user and how do I get ProductID in the session you described.
Say the user clicks a link with a URL parameter from a page:
e.g. <a href="cat.php?ProductCategoryID=1">Dress</a>
This leads to a category page that displays all products with that specific ProductCategoryID. Which would be all dresses in this instance.
On this category page "cat.php" user clicks a individual item image which takes them to details page "proDETAIL.PHP"
e.g <a href="prodDETAIL.php?recordID=<?php echo $row_rsvlProducts['ProductID'];?>"><img src="/images/large/<?php echo $row_rsvlProducts['ProductImage']; ?>" alt="image" /></a>
On the details page there is a recordset "rsItemDetail" which filters ProductID to match URL Parameter "recordID" and just displays info for single item as desired.
Now I need to some how store the ProductIDs viewed by user on details page "proDETAIL.PHP" IN THE SESSION CODE YOU PROVIDED
Do I do this by typing "recordID" to replace session name
e.g. $_SESSION["recordID"]
I also need to retain ProductIDs generated which appears to done generated by previous code seen above
e.g <?php echo $row_rsvlProducts['ProductID'];?>
I am unsure of how to acheive.
Please could you clarify for me.
Thank you very much by the way!
CraigR
08-01-2010, 10:54 PM
session name is the name of the session variable itself, you don't change this, what you do is change its value.
for example, when setting a session variable, you can do it directly, or use another variable.
$_SESSION["numberone"] = "5";
OR
$_SESSION["numberone"] = $row_rsvlProducts['ProductID'];
once you have set the session variable, you can now use this to filter a different recordset
victor405909
08-09-2010, 01:54 AM
I still cannot get this to work and I really need some help understanding where i am going wrong.
I need products to repeat in repeating table I setup. Have recordset created to display products by id.
This table should only display items viewed on detail page.
Trying to use sessions to achieve this.
I am not able to store product ids ,which has been created by user when they click link which holds URL parameter, and use this to display all products that has been viewed.
This is code I have currently on detail page:
<?php
if (!session_id()) session_start();
if(!isset($_SESSION["numberone"])) {//if session 'name' hasn't been set
$_SESSION["numberone"] = $_GET["recordID"]; //set session 'name' to a value
}else{
$_SESSION["numberone"] = $_GET["recordID"];
}
if(!isset($_SESSION["numbertwo"])) {
$_SESSION["numbertwo"] = $_GET["recordID"];
}else{
$_SESSION["numbertwo"] = $_GET["recordID"];
}
$rvitem = $_GET["recordID"];//allows me to echo product id in body
?>
Jason Byrnes
08-09-2010, 07:56 AM
I see no problem with this code, as long as there is a querystring name recordID being passed to the detail page, the session variables will be set, the only sugetion I would make is to add a check for the recordID querystring:
<?php
if (!session_id()) session_start();
if(isset($_GET["recordID"]) && $_GET["recordID"] != "") {
if(!isset($_SESSION["numberone"])) {//if session 'name' hasn't been set
$_SESSION["numberone"] = $_GET["recordID"]; //set session 'name' to a value
}else{
$_SESSION["numberone"] = $_GET["recordID"];
}
if(!isset($_SESSION["numbertwo"])) {
$_SESSION["numbertwo"] = $_GET["recordID"];
}else{
$_SESSION["numbertwo"] = $_GET["recordID"];
}
$rvitem = $_GET["recordID"];//allows me to echo product id in body
}
?>
victor405909
08-11-2010, 01:03 AM
The code maybe correct but I cannot get it to do want I want.
I can use a variable in a session to filter recordset no problem. However it only displays one product at a time.
I want it so every time a product is viewed on a details page the product id is stored in a variable. Repeated in a repeat table. This table to show maximum of 10 viewed items. If nothing in table displays "No recently viewed items".
I tried to explain more below, code maybe wrong but you should get an idea where I am heading with this.
User clicks detail page of an item
---------------------------------------------------
<a href=" proDETAIL.php?recordID=34>Dress</a>
Page retrieves product id from url parameter
---------------------------------------------------
$_GET('recordID');
Set product id to session named "recentlyViewedItem" (recordset uses to filter db, HOWEVER THIS IS ONLY ONE ID)
---------------------------------------------------
$_GET('recordID') = $_SESSION["recentlyViewedItem"];
set session variable to easier name to work with
---------------------------------------------------
$_SESSION["recentlyViewedItem"] = $rvItem
That variable needs to be checked to see if it already exist in an array of some kind
(perhaps 10 items to display at any onetime, code for array should be on page or coded on page to check MySql database for exact match, I do not know code to do this)
If ($rvItem in array): Ignore/die
If ($rvItem not in array) AND (all array positions have a value in it) : Delete oldest key/value in array then add $rvItem.
Elseif($rvItem if not in array) AND ( array values are not full) : put $rvItem in array And push other values down in array.
i tried to get recordset to read a test array I created of product ids so products could be viewed in a repeat table.
e.g $_SESSION["recentlyViewedItem"] = array(2,36,54,4)
But this did not work.
After some reading around there seems to be different ways to achieve this
sessions, cookies, using MySql database.
However it appears to be in bits and pieces to me and I am still unclear exactly how to achieve this.
Any ideas?
CraigR
08-11-2010, 03:35 AM
i never tried this, but i think I know what you are trying to achieve.
the problem seems to be in 2 parts,
Storing the id's in an array
Retrieving the array values and using them as parameters for your select statement
first off, i would focus on building the array and ensuring that this part works
then i would create the select statement (with a variable $string or whatever) and build $string by looping through the array, with the intention of ending up with lots of 'OR' values as your criterial,
SELECT ... FROM ... WHERE ID = 1 OR 2 OR 3
does that make sense ?
here are some good tutorials on php arrays to help
http://www.w3schools.com/PHP/php_ref_array.asp
http://uk3.php.net/manual/en/language.types.array.php
CraigR
08-11-2010, 10:05 AM
In order to prove (to myself), I could do this, I produced the following on the product detail page, (which takes the url parameter ‘Item’).
It may need some tidying up, but it seems to work ok
This code creates an array stored as session variable to hold the last 6 itemid’s
The array is then sliced to remove the current itemid, so we end up with 5 elements for setting the criteria.
A criteria string is then built to append to the select statement, which returns 5 rows, for the last 5 items viewed.
(you could also do some further inspection of the array, to eliminate any duplicate key values, but i have't yet done this)
The only problem is in the order of the resulting rows.
I can’t think of a way to sort the rows in the order in which they were stored in the array.
Can anyone help with this, or come up with an alternative solution ?
<?php
if (!isset ($_SESSION["lastviewed"])) {//set up array to hold item id of selected products
$_SESSION["lastviewed"] = array();
}
if (isset($_GET['Item'])) {//adds a itemid to the array
if (count($_SESSION["lastviewed"]) <= 5) {//we only want 6 elements to the array, so add one if we currently have 5 or fewer
array_push($_SESSION["lastviewed"],(get_magic_quotes_gpc()) ? $_GET['Item'] : addslashes($_GET['Item']));//add the current itemid to the array
} else {
$_SESSION["lastviewed"] = array_slice($_SESSION["lastviewed"],1) ;// we have 6 elements already, so remove the first element of the array
array_push($_SESSION["lastviewed"],(get_magic_quotes_gpc()) ? $_GET['Item'] : addslashes($_GET['Item']));//add the current itemid to the array
}
}
//create the criteria string
$_SESSION["criteria"] = array_slice($_SESSION["lastviewed"],0,-1) ;// we don't want the current itemid, so remove the last element of the 'lastviewed' array
$stringcriteria = '';
foreach ($_SESSION["criteria"] as $key => $value) {//loop through the array to build the string to set the criteria
$stringcriteria = $stringcriteria.' OR ItemID = '.$value;
}
$stringcriteria = substr($stringcriteria,4) ;//the string needs to be trimmed, to remove ' OR ' from the beginning
mysql_select_db($databaseConnection, $Connection);
$query_rslastfiveviewed = "SELECT ItemID, ItemName, ItemImageURL, ItemThumbnailURL FROM tblitem WHERE ".$stringcriteria."";
$rslastfiveviewed = mysql_query($query_rslastfiveviewed, $Connection) or die(mysql_error());
$row_rslastfiveviewed = mysql_fetch_assoc($rslastfiveviewed);
$totalRows_rslastfiveviewed = mysql_num_rows($rslastfiveviewed);
?>
...add the appropriate code to the page to view the results
victor405909
08-24-2010, 12:06 PM
Thanks for this I will try it out.
I have been working on a way to store the product ids into Mysql database every time user looks at a product. I have got the db to only store unique ids so that the recordset will not produce duplicates, only problem is this works for one user and not many since only one has been table created.
Anyone got any ideas?
CraigR
08-24-2010, 12:11 PM
Are you storing the data in a tble for a reason, (are you intending to use the data again) ?
victor405909
08-24-2010, 09:23 PM
I thought it could be a good idea to see what customers are looking at the most. However it maybe more useful to know what they bought instead so I guess on this occasion no.
I have read that temporary tables in Mysql can be created and be destroyed when session had ended maybe this is the way to go.
What do you advise?
CraigR
08-24-2010, 10:34 PM
i don't think there is a right or a wrong solution.
personally, i would use my site statistics if i wanted to see which pages were most visited
showing the customer 'recently viewed' items could be useful in securing a sale, but if this information is redundant for other customers (or the same customer on their next visit), then I see no need to store it.
(Saying that, i have written some code for showing the last items viewed, by storing the productid's in an array and can't (yet) get the records to display in the order in which they were viewed)
upselling or cross selling would usually involve storing data and can be very useful in increasing customer spend.
i can advise on these techniques if you wish.
hope this helps
victor405909
08-25-2010, 09:02 AM
Upselling and cross selling are exactly the things I wanted to tackle next if you could give any info on either of these to give me a grounded head start I would be very grateful.
Thank you so much!
CraigR
08-25-2010, 11:42 PM
Ok, here are some ideas
Upselling - Suggesting a better or higher value alternative
Simple
Add a field to your product table of the same data type as your product id
Use this field for other product id’s which you ‘recommend’, or are of higher value/margin
Advanced
Create a new table for recommended products.
Need a minimum of 2 fields (if using a compound key) or 3 fields if using DataAssist.
DataAssist table example structure, ID (PK), MainProductID, RecommendedProductID.
Link this table to your products table in your select query.
Good place to use this is on product detail pages, (where you are already retrieving productid as a URL parameter), so you can filter the recordsets above.
Cross Selling – Suggesting additional or related products to increase customer spend per transaction
Both methods above still apply for cross selling, depending on the recommended products added.
Additional ways
Items recently viewed, (covered in this thread)
Reminds a customer what they were considering in this session.
Sometimes referred to as ‘Customers who bought x also bought y’
Analyse the order details table, to see what items were ordered at the same time as the currently selected product. Collate this data into descending order of quantity purchased, perhaps limiting to a few rows, and display on the product detail and/or cart pages. (This is only applicable when you have some order history.)
The benefit of this approach is that the data is always based on your order history, so will update itself.
That should get you started
victor405909
08-26-2010, 07:22 PM
I will get started on this
Thank you!
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.