473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to set attributes into request object in PHP

4 New Member
In java we vll set attributes into request object as request.setAttribute(aa,bb), how to do the same in PHP
Feb 24 '10 #1
8 5323
Atli
5,058 Recognized Expert Expert
I suppose:
Expand|Select|Wrap|Line Numbers
  1. // By instantiation 
  2. $request->setAttribute($aa, $bb);
  3.  
  4. // Or statically
  5. request::setAttribute($aa, $bb);
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 "setAttribute" 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.)
Feb 24 '10 #2
rythmic
29 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. $_SESSION['attr_name'] = $value;
  5.  
There can be no print action or even whitespace before the session_start() method
Feb 24 '10 #3
ajay2287
4 New Member
Hi,
In my case i have written a class inside the <?php> tag as below

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. class First{
  4.              public function setValues(){
  5.                  $inputArray=array();
  6.                  $inputArray['1']="one";
  7.                  $inputArray['2']="two";
  8.                  $inputArray['3']="three";
  9.                  //now i have to pass this array to another PHP webpage,such that i 
  10.                  //can print the values of this array in the next PHP page(second.php given below).
  11.              }
  12. }
  13. ?>
second.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <?php
  3.  
  4. ?>
  5.      <body>
  6.             //here i have to access the $inputArray and print its value as
  7.             <h1>The value of 1 is:</h1><?php $inputArray["1"] ?>
  8.       </body>
  9. </html>
how to carry the array that is $inputArray from First PHP and print its values in second.php as above.
Feb 25 '10 #4
johny10151981
1,059 Top Contributor
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?
Feb 25 '10 #5
Atli
5,058 Recognized Expert Expert
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)
Feb 25 '10 #6
ajay2287
4 New Member
Ya i have to pass the array after user action.
Feb 25 '10 #7
rythmic
29 New Member
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. class First{
  4.              public function setValues(){
  5.                  $inputArray=array();
  6.                  $inputArray['1']="one";
  7.                  $inputArray['2']="two";
  8.                  $inputArray['3']="three";
  9.                  //now i have to pass this array to another PHP webpage,such that i 
  10.                  //can print the values of this array in the next PHP page(second.php given below).
  11.  
  12.    // rythmic input!!!: in order to get the array at all you need to return it from the function
  13.     return $input_array;
  14.              }
  15. }
  16. ?>
  17.  
Now, each time that you use this class you will need that page to start with

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start(); // now you are using session vars which are reached through the $_SESSION var which is an array.
  3.  
To store classes in a session you need to serialize the class by using the serialize function built-in in php like so

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. $first = new First();
  5.  
  6. // serialize and add to session
  7. $_SESSION['first'] = serialize($first);
  8.  
Then in second.php you'd write

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. //get the saved class
  5. $first = unserialize($_SESSION['first']);
  6.  
  7. $input_array = $first->setValues();
  8.  
  9. print "<h1>The value of 1 is:</h1>" . $inputArray[1];
  10.  
  11.  
That should do it!

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
Expand|Select|Wrap|Line Numbers
  1. // prints The value of 1 is:
  2. <h1>The value of 1 is:</h1><?php $inputArray["1"]?>
  3.  

should be:
Expand|Select|Wrap|Line Numbers
  1. // prints The value of 1 is: 1
  2. <h1>The value of 1 is:</h1><?php print $inputArray["1"];?>
  3.  
best regards
rythmic
Feb 25 '10 #8
ajay2287
4 New Member
Thanks..i used this code, it worked..
Feb 26 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2560
by: Dan Sikorsky | last post by:
How can I get the recordset attributes for a table field in SQL Server 2000 to report the field updatable attribute correctly ... mine keeps saying the fields are not updatable? That is, (...
3
3737
by: Michael Iantosca | last post by:
I have a custom attribute that I attach to certain pages in my application and I want to inspect each page request as it is made to see if the custom attribute is attached to the underlying page...
4
1759
by: jensen bredal | last post by:
Hello, i was not able to find any documentation about the largest value one can assign the "timeout" attribute of the <sessionState section in web.config. any idea?
3
1392
by: Danny Crowell | last post by:
I have a question related to ASP.net sessions and static attributes. In an ASP.net application (MyApp) I have a class called Globals with a public static string called UserName. Will UserName be...
6
5945
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
1
6801
by: LG | last post by:
I want to add attributes to a custom dropdownlist and I found some code on Internet that doesn't work after post back. First time the control has the attributes, but after postback the attributes...
28
1446
by: Alan Isaac | last post by:
I have a class whose instances should only receive attribute assignments for attributes that were created at inititialization. If slots are not appropriate, what is the Pythonic design for this? ...
1
1142
by: james_027 | last post by:
hi, for example an request object ... is request.user the same as request.__class__.user? THanks james
26
1878
by: tjhnson | last post by:
Hi, With properties, attributes and methods seem very similar. I was wondering what techniques people use to give clues to end users as to which 'things' are methods and which are attributes. ...
11
6224
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.