473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

use $_SESSION[$variable] instead of $_SESSION['whatever'] ?

Hi

Really dumb problem that's got me beat. Can someone help.

The prolem: I'm trying to count the number of times an item has been clicked. There are many items. I want to create a session variable for each item as its clicked, and increment the value held by the session variable by 1 each time that item is clicked. My plan was to allow an item to be selected on one page - when a user selects an item, a numeric value will be passed to a second page by means of the querystring .... ?item=5.

Here is the second page where I try to deal with it...

[PHP]<?php

session_start();

$item = $_GET['item'];

if (isset($_SESSION[$item]))
{
//increment the number to times this item has been selected by 1.
$_SESSION[$item] = $_SESSION[$item] + 1;
}
else
{
//This is the first time this item has been selected. Set value to 1.
$_SESSION[$item] = 1;
}

echo "Item ".$item." has been selected ".$_SESSION[$item]." times";
//e.g Item 5 has been selected 14 times
?>[/PHP]


Can anyone see what I'm doing wrong. The session doesn't set, so item 5 (or any other item) always comes back as being clicked 1 time.

Thanks
Shaz


P.S. I also tried using a session array(?), but no joy there either:

[PHP]<?php

session_start();

$_SESSION['test'] = array();
$item = $_GET['item'];

if (isset($_SESSION['test'][$item]))
{
//increment the number to times this page has been selected by 1.
$_SESSION['test'][$item] = $_SESSION['test'][$item] + 1;
}
else
{
//This is the first time this page has been selected before. Set value to 1.
$_SESSION['test'][$item] = 1;
}

echo "Item ".$item." has been selected ".$_SESSION['test'][$item]." times";
//e.g Item 3 has been selected 14 times


?>[/PHP]
Mar 8 '08 #1
2 2250
Scrub that. I got it sussed all by myself :)

I was almost there second time round. I moved the line that sets the session up as an array to its own page - e.g. page 0. Now it all work.

Thanks!
Mar 8 '08 #2
ronverdonk
4,258 Expert 4TB
Good you found it yourself. See you next time.

Ronald
Mar 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: tornado | last post by:
Hello all, I am not able to find the solution to this problem :(. I have two php pages . On the first page i am getting some values in $_SESSION when i use print_r function. I don't get the...
1
by: Jeff Sandler | last post by:
I just started with a service provider who's giving me a server to put my scripts. I noticed when I copy scripts that work on my machine to this server, all things $_SESSION are null. I read some...
4
by: iannorton | last post by:
Hi, I've spent most of today trying to solve this problem, but sadly no luck. I have an shopping basket based on an array which stores the name, product id, quantity and price for products, i...
8
by: nephish | last post by:
hey there, is this ok? class MyClass { var $start; var $finish; function MyClass($start, $finish) { $this->start = $start; $this->finish=$finish,
12
by: Michael Windsor | last post by:
I've been trying to integrate some PHP pages of my own with some existing code. The details of this are for the support forums for that code (where I have been asking questions), but I wonder if...
14
by: Sandman | last post by:
Is there any particular reason I should still use $_POST? Also, I'm reading mixed things about sessions. php.net says you can propagate session IDs either via cookies or session IDs:...
2
by: gupta.divyendu | last post by:
Hello Everybody I have a big problem. Please guide me. Is there any way through which I can get what session variables are created in the session. Suppose there are two pages. Page A and...
4
by: Daz | last post by:
Hi everyone. I'm just wondering if it's considered bad practice to have a class read from and write to the $_SESSION super global. I was just learning a little about object serialization, and I've...
9
by: Erwin Moller | last post by:
Hi all, Is it possible (PHP5.2) to find the name of a variable used in the caller of a function from within the function itself? Or to be more clear: ...php code.. $result = foo($abc);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.