You are here: Re: Newbie: Variable Scope: Passing arrays between PHP programs. « PHP Programming Language « IT news, forums, messages
Re: Newbie: Variable Scope: Passing arrays between PHP programs.

Posted by qazwart on 07/12/07 13:59

On Jul 11, 1:19 pm, ZeldorBlat <zeldorb...@gmail.com> wrote:
> On Jul 11, 12:55 pm, qazwart <qazw...@gmail.com> wrote:
>
>
>
> > I am new to PHP, but have been a Perl programmer for over a couple of
> > decades. My question isn't necessarily a particular programming
> > question, but has to do with passing variables between PHP programs:
>
> > I have a file that contains two fields on each line: The build number
> > tag and the QA Release tag. I display a form and give users a chance
> > to do one of three things:
>
> > 1. Generate release notes between any two QA releases.
> > 2. Generate new release notes for the next QA release.
> > 3. Generate release notes for a previous release.
>
> > The source files are stored in CVS and I am using the "cvs rlog"
> > command to build the release notes which consists of all the files
> > that have been modified, deleted or added and the check in comments.
> > The user selects the QA releases via a drop down fields in the form.
> > In order to just get the files that have been changed, I need a "From"
> > release and a "To" release.
>
> > For item #1, this is pretty simple because the user gives me the
> > "From" and "To" release. For item #2, I store the information I need
> > in hidden fields. Item #3 is the tricky one. The user is selecting
> > only a single release (the "To" release), and I have to figure out the
> > previous release in order to generate the release notes. I know the
> > name of the file, and could easily parse the file to find this
> > information, but I already did this once in order to build the drop
> > down fields.
>
> > What I would really like to do is pass the whole PHP variable array
> > from one PHP program to another between http calls much like the _POST
> > array does. I see several ways of doing this:
>
> > * Use a SQL database. This seems like a bit of overkill for just this
> > one item.
> > * Use hidden fields: There could be dozens, if not hundreds of
> > releases to choose from. That's a lot of data to pack into a form.
> > * Use cookies: I know I can pass single pieces of data to cookies, but
> > a whole array?
>
> > So, exactly how do you pass a lot of information from one PHP program
> > to another between http calls? Global variables don't work in this
> > case because the variables lose their information between http calls.
>
> Use sessions:
>
> <http://www.php.net/session>
>
> On page 1:
>
> <?php
> session_start();
> $_SESSION['foo'] = 'bar';
> ?>
>
> On page 2:
>
> <?php
> session_start();
> echo $_SESSION['foo'];
> ?>

Thanks! This is exactly what I need. No need to even serialize data
structures. They are passed between sessions as is. It even works
between different scripts. Here's a test program:

test.php:

1 <?php session_start()?>
2 <HTML>
3 <HEAD><TITLE>TEST</TITLE>
4 </HEAD>
5 <BODY>
6 <?php
7 $foo = array('one' => 'a', 'two' => 'b', 'three' => 'c');
8 foreach($foo as $key => $value) {
9 print "\$foo[\"$key\"] = \"$value\"<br>\n";
10 }
11 print <<<HTML
12 <FORM method="post" action="test2.php">
13 <INPUT type="hidden" name="loaded" value="1">
14 <INPUT TYPE="submit" name="submit" value="Submit">
15 </FORM>
16 HTML;
17 $_SESSION['foo'] = $foo;
18 ?>
19 </BODY>
20 </HTML>

test2.php:

1 <?php session_start();
2 $foo = $_SESSION['foo'];
3 print <<<HTML
4 <HTML>
5 <HEAD><TITLE>LOADED TEST</TITLE>
6 </HEAD>
7 <BODY>
8 HTML;
9 print "The value of foo_array is:<br>\n<ul>\n";
10 foreach($foo as $key => $value) {
11 print "<li>\$foo[\"$key\"] = \"$value\"</li>\n";
12 }
13 ?>
14 </ul>
15 </BODY>
16 </HTML>

I'm going to rewrite my PHP scripts to remove the heavy handed use of
"hidden" form fields in order to pass variables back and forth.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация