In java we vll set attributes into request object as request.setAttr ibute(aa,bb), how to do the same in PHP
How to set attributes into request object in PHP
Collapse
X
-
I suppose:
[code=php]
// By instantiation
$request->setAttribute($ aa, $bb);
// Or statically
request::setAtt ribute($aa, $bb);[/code]
But I have no idea what the "request" object is; whether it is some special Java thing that. I'm just assuming you have a class called "request" and that you want to call the a "setAttribu te" method on it.
If that is not what you are trying to do, elaborate. (And keep in mind that many of us do not know details about the inner workings of Java.) -
In PHP you have three global arrays to accomplish this.
$_POST, $_GET which contains data sent through post method or get method
There is also $_REQUEST which contains the content found in $_POST + $_GET
to carry data between webpages, use $_SESSION['varname'] = value
To use sessions you also need to use the session_start() ; method at the very beginning of your php file. like this:
There can be no print action or even whitespace before the session_start() methodCode:<?php session_start(); $_SESSION['attr_name'] = $value;
Comment
-
Hi,
In my case i have written a class inside the <?php> tag as below
second.phpCode:<?php class First{ public function setValues(){ $inputArray=array(); $inputArray['1']="one"; $inputArray['2']="two"; $inputArray['3']="three"; //now i have to pass this array to another PHP webpage,such that i //can print the values of this array in the next PHP page(second.php given below). } } ?>
how to carry the array that is $inputArray from First PHP and print its values in second.php as above.Code:<html> <?php ?> <body> //here i have to access the $inputArray and print its value as <h1>The value of 1 is:</h1><?php $inputArray["1"] ?> </body> </html>Comment
-
Do you want to pass this array after user action on the web site or you just want to include the file with class into another php file?Comment
-
Assuming, again, that at some point in your code the First::setValue function would be called, and that the code in the second page would not be called in the same request, then you would want to use Sessions.
See the examples in the manual for details on how they work. You can also check out this tutorial. (Or one of the many others out there. Come to think of it, I even wrote one of those a few years back xD)Comment
-
Now, each time that you use this class you will need that page to start withCode:<?php class First{ public function setValues(){ $inputArray=array(); $inputArray['1']="one"; $inputArray['2']="two"; $inputArray['3']="three"; //now i have to pass this array to another PHP webpage,such that i //can print the values of this array in the next PHP page(second.php given below). // rythmic input!!!: in order to get the array at all you need to return it from the function return $input_array; } } ?>
To store classes in a session you need to serialize the class by using the serialize function built-in in php like soCode:<?php session_start(); // now you are using session vars which are reached through the $_SESSION var which is an array.
Then in second.php you'd writeCode:<?php session_start(); $first = new First(); // serialize and add to session $_SESSION['first'] = serialize($first);
That should do it!Code:<?php session_start(); //get the saved class $first = unserialize($_SESSION['first']); $input_array = $first->setValues(); print "<h1>The value of 1 is:</h1>" . $inputArray[1];
By the way: another thing I noticed, whenever you want to put something in the html code from your php code you need to echo or print it.
So your example with
Code:// prints The value of 1 is: <h1>The value of 1 is:</h1><?php $inputArray["1"]?>
should be:
best regardsCode:// prints The value of 1 is: 1 <h1>The value of 1 is:</h1><?php print $inputArray["1"];?>
rythmicComment
Comment