Jump to content

Sessions variables and arrays


screenshotcentral

Recommended Posts

I have a slight issue regarding session variables and arrays.

[code]<?
session_start();
$ID=$_POST['ID'];

session_register('gun_array');
$_SESSION['gun_array']=$array;
$array = array();
$array[]=$ID;

[/code]
This is what I have coded. I want start out with a blank array, then every time you access a certain page, a new $ID should be entered into the array. This value all depends on selections made on the previous pages. The problem I am having is that the new id just replaces the old id, and does not make an array. When I echo the array, its only give the one value, and is replaced when I change $ID. I read before that $array[]=(variable) is equivalent to array_push, but if it is, what else am I doing wrong, since array_push does not work either?
Link to comment
Share on other sites

Guest footballkid4
Never ever ever use session_register...it's an old function! ESPECIALLY if you have register globals on!

Anways, here's your script:
[code]<?php
session_start();
$id = $_POST['ID'];
if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array();
$_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $id );
?>[/code]
Link to comment
Share on other sites

When I used that script, the php gave an error saying

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: array_push(): First argument should be an array (wesite path removed) on line 8[/quote]

7 if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array();
8 $_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $id );

I understand that in order to push an array, an array must be stated inside of array_push(array, variable). When the script ran, I am guessing that it decided that $_SESSION_['gun_array'] was not a valid array to push. Also the code that previously worked to view the data was [code]<?
session_start();
foreach($_SESSION['gun_array'] as $value) {echo("$value<BR>");}
?>
[/code] but that does not seem to work now, which I assume is because the session variable fails to register as a legitmate array.
Link to comment
Share on other sites

Guest footballkid4
I apologize, that was my fault...try this:
[code]<?php
session_start();
$id = $_POST['ID'];
if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array( rand() );
$_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $id );
?>[/code]
As you can see, if session gun_array isn't set, we start it with rand() as one of the array values. So, when you loop through them...just exclude the first entry.
Link to comment
Share on other sites

Sadly, that does not work either, it gives the exact same error on line 8, but I thank you for helping. I guess what I need to do is to learn more about sessions mixed with arrays in order to help work out these problems in the future. Do you know of anywhere where I can get information on session variables used as arrays? I have searched the net many times before coming here, but I could not find much of anything significant. The manual for php either has topics on sessions or ones on arrays, but never a mixture of the two.

Thanks again.

Exact code on the page:
[code]<?
session_start();
$ID=$_POST['ID'];
$NAME=$_POST['NAME'];
$PRICE=$_POST['PRICE'];

if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array( rand() );
$_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $ID );
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.