How to set attributes into request object in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajay2287
    New Member
    • Feb 2010
    • 4

    How to set attributes into request object in PHP

    In java we vll set attributes into request object as request.setAttr ibute(aa,bb), how to do the same in PHP
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.)

    Comment

    • rythmic
      New Member
      • Feb 2010
      • 29

      #3
      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:

      Code:
      <?php
      session_start();
      
      $_SESSION['attr_name'] = $value;
      There can be no print action or even whitespace before the session_start() method

      Comment

      • ajay2287
        New Member
        • Feb 2010
        • 4

        #4
        Hi,
        In my case i have written a class inside the <?php> tag as below

        Code:
        <?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).
                     }
        }
        ?>
        second.php
        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>
        how to carry the array that is $inputArray from First PHP and print its values in second.php as above.
        Last edited by Atli; Feb 25 '10, 06:27 AM. Reason: Added [code] tags.

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          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

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            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

            • ajay2287
              New Member
              • Feb 2010
              • 4

              #7
              Ya i have to pass the array after user action.

              Comment

              • rythmic
                New Member
                • Feb 2010
                • 29

                #8
                Code:
                <?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;
                             }
                }
                ?>
                Now, each time that you use this class you will need that page to start with

                Code:
                <?php
                session_start(); // now you are using session vars which are reached through the $_SESSION var which is an array.
                To store classes in a session you need to serialize the class by using the serialize function built-in in php like so

                Code:
                <?php
                session_start();
                
                $first = new First();
                
                // serialize and add to session
                $_SESSION['first'] = serialize($first);
                Then in second.php you'd write

                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];
                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
                Code:
                // prints The value of 1 is:
                <h1>The value of 1 is:</h1><?php $inputArray["1"]?>

                should be:
                Code:
                // prints The value of 1 is: 1
                <h1>The value of 1 is:</h1><?php print $inputArray["1"];?>
                best regards
                rythmic

                Comment

                • ajay2287
                  New Member
                  • Feb 2010
                  • 4

                  #9
                  Thanks..i used this code, it worked..

                  Comment

                  Working...