Jump to content


Photo

Sessions variables and arrays


  • Please log in to reply
4 replies to this topic

#1 screenshotcentral

screenshotcentral
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 March 2006 - 04:24 AM

I have a slight issue regarding session variables and arrays.

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

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

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?

#2 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 05:19 AM

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

Anways, here's your script:
<?php
session_start();
$id = $_POST['ID'];
if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array();
$_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $id );
?>


#3 screenshotcentral

screenshotcentral
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 March 2006 - 09:31 PM

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
<? 
session_start();
foreach($_SESSION['gun_array'] as $value) {echo("$value<BR>");}
?>
but that does not seem to work now, which I assume is because the session variable fails to register as a legitmate array.

#4 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 10:36 PM

I apologize, that was my fault...try this:
<?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 );
?>
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.

#5 screenshotcentral

screenshotcentral
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 31 March 2006 - 01:48 AM

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:
<?
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 );
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users