473,661 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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($_SESSIO N[$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($_SESSIO N['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 2262
sharonDonnelly
2 New Member
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 Recognized Expert Specialist
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
12294
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 values when i use just echo() or print() functions. Now, the first page calls the second php page. But when i try to get the values for $_SESSION on the second page i get empty values even when i use print_r() function. register.globals is set...
1
2429
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 on php.net and found that session_autostart has to be set to true in order for me to use $_SESSION variables at all. Am I reading that right? Obviously, I have no control over this setting on someone else's server so I cannot change it. Is...
4
1802
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 want to take the Information in the array and output the Quantity and Product ID information into variables so that i can pass them into a Select Statement and submit the order to the database.
8
1770
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
41159
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 someone here can enlighten me as to why the problematic code is having the effect it is. For reasons I don't know, if the PHP version is 5 or greater, register_long_arrays is false and $_SESSION exists, the following statement is executed: ...
14
5844
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: http://www.php.net/manual/en/ref.session.php#session.idpassing OK cool. However, PHP in a nutshell makes no mention of this. Paul just says hey, use session_start() and you're on your way:
2
12989
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 page B Page A has a link for page B
4
2134
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 come to the conclusion that storing potentially large serialized objects in a database is perhaps a bad idea. The data could also be stored in a file, but in that case, I may as well use $_SESSION. If I'm going to go down that road, why not store...
9
1992
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); ...more phpcode..
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.