Experts Round Table Network
Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:
Password:
Remember me
Forgot your password?
Register
Unsetting part of a session with a clause
Welcome,
Guest
. Please
login
or
register
.
December 02, 2008, 01:54:11 PM
11304
Posts in
1248
Topics by
498
Members
Latest Member:
katCheeme
Home
Help
Search
Login
Register
Experts Round Table Network
|
Databases
|
MySQL
|
Unsetting part of a session with a clause
« previous
next »
Pages:
[
1
]
Print
Author
Topic: Unsetting part of a session with a clause (Read 705 times)
paul_mellon36
Offline
Posts: 17
Unsetting part of a session with a clause
«
on:
February 26, 2006, 12:28:01 PM »
Hi,
I am having a problem unsettting part of a session in my shoppping basket.
The problem is that the session has 2 parts, an item id and a quantity related to it.
I want to unset this session part if they remove that item from the shopping basket.
The code that is not working is below:
Create the session:
Code:
<?php
session_start();
if(!isset($_SESSION['basket']))
{
$_SESSION['basket'] = array();
}
if(isset($_POST['selected_item_id']))
{
$u_item_id = $_POST['selected_item_id'];
$u_quantity = $_POST['sel_item_qty'];
$u_details = $u_item_id."|".$u_quantity;
$_SESSION['basket'][] = $u_details;
header("Location: showcart.php");
}
?>
Removing part of it:
Code:
// this calls the following code
<td class=border align = center><a href='removefromcart.php?id=$item_id'>Remove</a></td>
<?php
if ($_GET[id] != "")
{
unset($_SESSION['basket'][] == $_GET[id]);
header("Location: showcart.php");
}
else
{
header("Location: shopfront.php");
exit;
}
?>
Very grateful for any help you can give
Logged
Paul
VGR
Mentor
Offline
Posts: 682
Unsetting part of a session with a clause
«
Reply #1 on:
February 26, 2006, 03:27:27 PM »
Code:
unset($_SESSION['basket'][] == $_GET[id]);
!!!???
something like this, perhaps ?
Code:
$b=count($_SESSION['basket']);
$i=0; $notfound=TRUE;
while (($i<$b) AND $notfound) {
$zar=explode('|',$_SESSION['basket'][$i]);
$notfound=($_GET['id']<>$zar[0]);
if ($notfound) $i++;
} // while
if ($notfound) { // not found
// anything to do ?
} else { // found
unset($_SESSION['basket'][$i]); // I don't like this, but it's what you ask for
}
If I were you, I would store the basket as a normal two-dimensional array with code like :
Code:
session_start();
if(!isset($_SESSION['basket']))
{
$_SESSION['basket'] = array();
}
if(isset($_POST['selected_item_id']))
{
$u_item_id = $_POST['selected_item_id'];
$u_quantity = $_POST['sel_item_qty'];
$_SESSION['basket'][] = array('id'=>$u_item_id,'qty'=>$u_quantity);
header("Location: showcart.php");
}
this would simplify your life a lot, and if ever you want to store it, then it's time to implode() using '|' - or even better, to encode or dump the whole sesison variable -
Logged
techie overlord, answers all kind of questions on
http://www.europeanexperts.org
paul_mellon36
Offline
Posts: 17
Unsetting part of a session with a clause
«
Reply #2 on:
February 26, 2006, 04:24:02 PM »
ok,
i have adapted your code to use for the session array to display my shopping cart.
Basically i need to display the 2 items in the array, i can display the first half but now i don't know how i get the second (qty).
At present i use this but it doesn't work:
Code:
<?php
session_start();
if(!isset($_SESSION['basket']))
{
$_SESSION['basket'] = array();
}
if(isset($_POST['selected_item_id']))
{
$u_item_id = $_POST['selected_item_id'];
$u_quantity = $_POST['sel_item_qty'];
$_SESSION['basket'][] = array('id'=>$u_item_id,'qty'=>$u_quantity);
header("Location: showcart.php");
}
?>
Code:
for($i = 0; $i < count($_SESSION['basket']); $i++)
{
$product = $_SESSION['basket'][$i];
$myarray = implode('|', $product);
when i try to use $myarray[1] i don't get anything
Please help
Logged
Paul
paul_mellon36
Offline
Posts: 17
Unsetting part of a session with a clause
«
Reply #3 on:
February 26, 2006, 05:15:26 PM »
its ok i got it, its just the problem of removing it now?(unset)
Logged
Paul
VGR
Mentor
Offline
Posts: 682
Unsetting part of a session with a clause
«
Reply #4 on:
February 27, 2006, 02:02:55 AM »
the access is done via $product_id = $_SESSION['basket'][$i]['id'] and qty=<trhesame>['qty']
to unset it, I think you'd to NOT unset() it, but rather ste the quantity to 0. This way your count() still equals to real number of array elements and to the highest array index present (plus 1)
if you really want to unset() $_SESSION['basket'][$i] you can , but then adapt my suggested snippet of code to nto do for($i=0;$i<count()) { something on element $i } but rather count $i from 0 to until the count()-th element has been found as set - use isset() - because unsetting an array element creates a "hole"
Logged
techie overlord, answers all kind of questions on
http://www.europeanexperts.org
paul_mellon36
Offline
Posts: 17
Unsetting part of a session with a clause
«
Reply #5 on:
February 27, 2006, 04:42:06 AM »
sorry but i do not understand???
A NOT unset???
could you show me in a code form???
below is the code i have so far:
Code:
//removefromcart.php
<?php
if ($_GET[id] != "")
{
unset($_SESSION['basket'][id] == $_GET[id]);
header("Location: showcart.php");
}
else
{
header("Location: shopfront.php");
exit;
}
?>
Code:
//addtocart.php
<?php
session_start();
if(!isset($_SESSION['basket']))
{
$_SESSION['basket'] = array();
}
if(isset($_POST['selected_item_id']))
{
$u_item_id = $_POST['selected_item_id'];
$u_quantity = $_POST['sel_item_qty'];
$_SESSION['basket'][] = array('id'=>$u_item_id,'qty'=>$u_quantity);
header("Location: showcart.php");
}
?>
Logged
Paul
paul_mellon36
Offline
Posts: 17
still stuck
«
Reply #6 on:
March 13, 2006, 04:13:43 AM »
Hi Guys, I am still having trouble with this if anyone could help? it'd be much appreciated
It's just the unset problem
Paul
Logged
Paul
VGR
Mentor
Offline
Posts: 682
Unsetting part of a session with a clause
«
Reply #7 on:
March 13, 2006, 04:16:54 PM »
this is not a MySql question but a PHP one
I suggested you to not unset() a session variable, not to NOT_unset() it :D
I suggested some code changes, and your latest code is a mix of the old one and the new one. I doubt this will work.
your problem is not that complicated
Logged
techie overlord, answers all kind of questions on
http://www.europeanexperts.org
admin
Administrator
Offline
Posts: 3
Unsetting part of a session with a clause
«
Reply #8 on:
March 13, 2006, 06:10:49 PM »
VGR,
I have edited your post. If you want to post the link to your site when appropriate, that is acceptable and allowed, but please do not invite members to move their questions to another site.
Cd&
Site Administrator
Logged
VGR
Mentor
Offline
Posts: 682
Unsetting part of a session with a clause
«
Reply #9 on:
March 14, 2006, 12:13:33 AM »
complementary
Logged
techie overlord, answers all kind of questions on
http://www.europeanexperts.org
Pages:
[
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
ERT 1.5
-----------------------------
=> Round Table Learning Center
=> Bug reports
-----------------------------
Legacy
-----------------------------
=> The next level
=> History of ERT
-----------------------------
Community Affairs
-----------------------------
=> Introductions
=> Ballot Box
===> Closed Polls
=> Soapbox
=> Propose and Consult
===> Propose and Consult...CLOSED
-----------------------------
Bits and Bytes
-----------------------------
=> Tips, Tricks, Snippets, Tidbits And General Pearls Of Wisdom
-----------------------------
Serverside Technology
-----------------------------
=> PHP
=> ASP
-----------------------------
Webservers
-----------------------------
=> Apache
=> IIS
-----------------------------
Databases
-----------------------------
=> MySQL
=> Access
=> MS SQL Server
-----------------------------
Clientside Technology
-----------------------------
=> HTML
=> CSS
=> Javascript
=> Flash
=> WAP/WML
-----------------------------
Web Technologies
-----------------------------
=> General Web Dev
=> Web Standards
=> XML
=> Online Marketing
-----------------------------
Graphics
-----------------------------
=> Graphics Design and Animation
-----------------------------
Programming
-----------------------------
=> .NET
=> JAVA
=> MS DOS Batch Scripting
=> Mathematics
=> C & C++
=> VB
=> Delphi
=> Algorithm design
-----------------------------
Operating Systems
-----------------------------
=> Windows (General)
=> NT Based (2K, 2K-03, NT, XP, Vista)
=> Open Source (All)
-----------------------------
Hardware
-----------------------------
=> Hardware General
=> Gamers Hardware (Advanced)
-----------------------------
Networking
-----------------------------
=> Home (small)
=> Office (large)
=> Internet
-----------------------------
Security
-----------------------------
=> General Security Issues
-----------------------------
Rants/Opinions/Proposals
-----------------------------
=> Site operation
Powered by SMF 1.1 RC2
|
SMF © 2001-2005, Lewis Media
Joomla Bridge by
JoomlaHacks.com