close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Sessions and recordset

Thread began 8/01/2010 12:29 am by victor405909 | Last modified 8/26/2010 7:22 pm by victor405909 | 440670 views | 15 replies |

victor405909

Sessions and recordset

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

Sign in to reply to this post

CraigRBeta Tester

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

Sign in to reply to this post

victor405909

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!

Sign in to reply to this post

CraigRBeta Tester

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

Sign in to reply to this post

victor405909

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
?>

Sign in to reply to this post

Jason ByrnesWebAssist

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
}
?>

Sign in to reply to this post

victor405909

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?

Sign in to reply to this post

CraigRBeta Tester

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


php_ref_array.asp

language.types.array.php

Sign in to reply to this post

CraigRBeta Tester

In order to prove (to myself), I could do this, I produced the following on the product detail page, (which takes the url parameter &#8216;Item&#8217;).
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&#8217;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&#8217;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

Sign in to reply to this post

victor405909

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?

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...