Jump to content

Archived

This topic is now archived and is closed to further replies.

screenshotcentral

Sessions variables and arrays

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?

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.