Current location: Hot Scripts Forums » Programming Languages » PHP » Foreach Variable/array Scope


Foreach Variable/array Scope

Reply
  #1 (permalink)  
Old 10-21-05, 08:43 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Foreach Variable/array Scope

I am in the middle of everything and my heads all over the place, so forgive me if this sounds stupid.

I am producing a form which has update buttons that write new entries to a database. I thought I would make it user friendly and have the values 'stick' when the form reloads so the user won't lose what they wrote. (this is not the submit stage its just a change in the selection about half way down the form)

Anyway I used $_SESSION[''] variables to record the form variables which works great for all of the boxes apart from 1. I had a bit of a headache with a multi-selection box (the one where the user can hold control button and select many). I have sorted this for writing it to the mysql database with the use of
foreach ($_POST['mulitselection'] as $value)
{
write database with individual $value;
}

The thing I need to do now is to have the array copied into
$_SESSION['multiselection'] and be able to use it the same way...

forech ($_SESSION['multiselection'] as $value)...ect

The problem stems from the fact that I cant seem to get my head around why things are not working.

$count = 0;
foreach ($_POST['multiselection'] as $value)
{
$_SESSION['multiselection[$count]'] = $value;
$count++;

foreach ($_SESSION['multiselection[]'] as $testin)
{
echo $testin ."<br>";
}

}

seems to confirm with the echo that the values are there but if I put this second foreach on the outside the values are not there. ie..

$count = 0;
foreach ($_POST['multiselection'] as $value)
{
$_SESSION['multiselection[$count]'] = $value;
$count++;
}

foreach ($_SESSION['multiselection[]'] as $testin)
{
echo $testin ."<br>";
}

So my question is how to make the $_SESSION['multiselection'] array global and why is this not and the other $_SESSION[''] variables are!??

Aghhh!
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-21-05, 11:55 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Ok I think Ive confused everyone with that. Hows about this one;

how do you move the array $_POST['multiselection'] to $_SESSION['multiselection']?
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-21-05, 02:54 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
simply as:
$_SESSION['multiselection'] = $_POST['multiselection'];
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-21-05, 05:51 PM
Trevor's Avatar
Trevor Trevor is offline
Wannabe Coder
 
Join Date: Jun 2003
Location: Denver, Colorado
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
How about :

PHP Code:

foreach ($_POST['multiselection'] as $key => $value) {
    
$SESSION['multiselection'][$key] = $value;

Of course, NeverMind's solution is easier... but if you get paid by the kb, go with my solution.
__________________
The Universe Our God, Nature Our Temple, Love And Duty Our Religion, Knowledge Our Happiness And Consolation, Death The Dissolution Of The Ego, And The Return To Eternity.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-21-05, 08:39 PM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks everyone. Guess I just needed to stop and go back to the basics. I did try originally what was suggested by NeverMind but when it didnt work I assumed that I was looking for a more complicated answer. After NeverMind and Trevor confirmed that the solution was just that easy, I changed my code back to this and after it didnt work I scanned through and realised that I put an extra character on my array name.

Whoo hoo it works. Thanks again.
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 10-22-05, 02:38 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
but if you get paid by the kb, go with my solution
isn't this a kind of fraud?

Dal, no problem
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 03:22 AM
formmail problem gscraper Perl 12 08-27-04 04:06 AM
Formmail Script Fix - Cant display multiple list selections.. zamen PHP 3 05-02-04 10:59 AM
limiting foreach() simone PHP 5 02-26-04 08:39 PM
Classified Ads skipper23 Perl 2 12-30-03 04:43 AM


All times are GMT -5. The time now is 06:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.