Web Analytics Made Easy -
StatCounter Passing an array to another page through a hyperlink - CodingForum

Announcement

Collapse
No announcement yet.

Passing an array to another page through a hyperlink

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing an array to another page through a hyperlink

    Hi All,
    i am populating an array depending on the user inputs and displaying the contents to the user. The user wants to print the same information.I want to pass that array without using sessiond through a link to another page so that everytime i need not query the database.
    Please help me in this. i have tried using serialize() but its not heping me.
    Thanks and god bless

    BhaktiThakkar

  • #2
    I think this is the wrong forum for that, but I am sure the administrator will move it.

    Just a quick reply: It is not not clear to me if you wanted to POST the data to your user or that you just want some global that anyone can use when logging into your site.
    If you want to POST, just use the $_POST array either via the form that calls the user's script or use CURL if you don't use a form.

    Ronald
    RTFM is an almost extinct art form, it should be subsidized.

    Comment


    • #3
      hi,
      i dnt understand why is this forum wrong for my PHP doubt.

      anyways i dnt want to post the data to another page. i am actually preparing a report which the user can print too. what i was doing previously was passing the form data to the new window and requery the database again and then the user can print but now i think that instead of requering why not pass that array itself and then just populate the data from the array. i dnt want to use sessions for this.

      Thanks and hope some help coming up

      Comment


      • #4
        (about the forum: you are in the "Post a PHP Snippet forum, not the "PHP Help" forum, but that doesn't matter).

        I am still a bit confused about what you want, but I'll give it a try.
        You have build an array withy the results of a db query and you want to pass that array to another page (of you) without using the $_SESSION array.

        As said in my previous reply, in PHP you then have to POST that array to another page. If you don't want that to be done via a form submit, you can use CURL to transfer the array to the new page. When that new page is opened your array data is stored in, and can be retrieved via, the $_POST array in that new page. Let me give you an example of using CURL to pass an array to the $_POST array of the new screen.

        The first script builds the array to be posted, calls the new script and passes the built array via the POST. The second snippet you can use to test that the array has actually been sent.
        PHP Code:
        <?php
        // ************ POST an array via curl *********

        // create a new curl resource
        $ch curl_init();

        // set URL and other appropriate options
        curl_setopt($chCURLOPT_URL"http://path/to/your/page.php");

        // build the test array
        $data = array('name' => 'John''surname' => 'Doe');

        // Do a POST
        curl_setopt($chCURLOPT_POSTtrue);
        curl_setopt($chCURLOPT_POSTFIELDS$data);

        // grab URL, and print
        curl_exec($ch);

        ?>

        <?php
        //---------- and the test in the posted script  -----
        echo '<h3>Variables I received: </h3>';
        echo 
        '<pre>';
        print_r ($_POST);
        echo 
        '</pre>';
        ?>
        Hope this is what you were looking for! Good luck.

        Ronald
        RTFM is an almost extinct art form, it should be subsidized.

        Comment


        • #5
          i don't see why you don't want to use sessions.
          i also don't understand why you don't just add the serialzes array to the querystring.
          i don't even understand why you don't want to requiry the data, because that normally doesn't consume noticably more resources then unserializing an array or so + requerying the data has the advantage that you are sure you are working on the most recent version of that data (it's very well possible that the data in the db was changed between the two pagerequests...)
          if you don't want the serialized array in a session or querystring and don't want to use curl or requery the db, then you can still store the variable-value pairs (or a serialized array) in a persistent cookie...
          Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

          Comment


          • #6
            hi,
            i am loading the data first on a page to the user. also i am providing a link besides and opening it in a blank window to facilitate printing. I am already using sessions but the problem is that if the user closes the window and click on the print link again the data was repeating as the array needs to be unset() and then again populated.

            then i tried to add the serialized array [i am even getting the data properly in that serialied variable] but the problem is something i have never come accrossed. the link itself gets disabled. the print page doesnt open.i am still guessing why ???if i am able to solve this then nothing like it....

            i will now try it using CURL[never used till now].

            mean while if some one could help we why the link doent open evn though there is data in the varaible which has been serialized and added to the link as querystring. if i print the variable it has the data but when i add it to the link it is empty plus the link also doesnt function??????

            Thanks for the extended help...
            Bhakti
            Last edited by bhakti_thakkar; Sep 26, 2006, 03:40 AM. Reason: wrong framing

            Comment


            • #7
              maybe you should post the code you are using? not sure what you expect from us if you're basically just saying 'it doesn't work and i have no idea why'.
              I am already using sessions but the problem is that if the user closes the window and click on the print link again the data was repeating as the array needs to be unset() and then again populated.
              euh... you can just repopulate it like:
              PHP Code:
              $yourarray = array('foo'=>'bar'); 
              or leave it alone if it's already set. like
              PHP Code:
              if (!isset($yourarray)){
                 
              $yourarray = array('foo'=>'bar');

              Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

              Comment


              • #8
                Million Thanks


                i m through with the problem by just one line of code of isset().it had been my nightmare. now i have to just ponder to find out something more on why my serialize() did not work.

                i will also post my code of serialize so that i can can some help in it.

                Bhakti

                Comment


                • #9
                  Originally posted by bhakti_thakkar View Post
                  now i have to just ponder to find out something more on why my serialize() did not work.

                  i will also post my code of serialize so that i can can some help in it.
                  to serialize an array, you just need
                  PHP Code:
                  $serialized serialize($your_array); 
                  so to add it to a link, all you could possebly need is
                  PHP Code:
                  echo '<a href="foobar.php?serialize='serialize($your_array) , '" title="whatever">link</a>'
                  edit --> you need to make sure that the querystring doesn't exceed the maximum allowed number of chars! this limit is browserspecific, but if you have a large array (or large values for the elements) then you might need to check this.
                  Last edited by raf; Sep 26, 2006, 07:24 AM.
                  Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                  Comment


                  • #10
                    serialize help

                    hi,
                    i did try out the way u had suggusted. the link was working fine. but the problem is that, when i print the array after serializing in the same page or unserialize it there itself, it works absolutely fine but in the target page i.e CollectionRepPrint page it is incomplete. i think it is because of the huge data. below is the code:

                    //CollectionRep.php
                    <?echo '<a href="CollectionRepPrint.php?serialize=', serialize($SingleDisparr) , '" title="whatever" target="_blank">link</a>';?>


                    //CollectionRepPrint.php
                    in my CollectionRepPrint.php page
                    <?print_r($serialize)?>

                    ANY SUGGESTIONS???
                    Thanks
                    Bhakti

                    Comment


                    • #11
                      Originally posted by bhakti_thakkar View Post
                      //CollectionRepPrint.php
                      in my CollectionRepPrint.php page
                      <?print_r($serialize)?>
                      on the CollectionRepPrint.php, you need
                      PHP Code:
                      print_r(unserialize($_GET['serialize'])); 
                      Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                      Comment


                      • #12
                        Hi raf,
                        the page goes blank. nothing gets printed. i m using PHP 4.4.2.

                        Thanks

                        Comment


                        • #13
                          maybe your array contains stringvalues? --> then you need to urlencode the serialzed array, because else the html for the link gets broken.

                          here's a testcode a wrote --> put this in a file that you call serialize.php and then load that page in your browser. hit the link and the array from the querystring is printed:
                          PHP Code:
                          <?php
                          print_r
                          (unserialize(stripslashes($_GET['serialize'])));
                          $arr_test = array('element1'=>'fdgdfgdfgdfgdfgdf''element2'=>'setyurtaezrt'2=>1254);
                          echo 
                          '<a href="serialize.php?serialize='urlencode(serialize($arr_test)) ,'">test</a>';
                          ?>
                          Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                          Comment


                          • #14
                            hi raf,
                            as soon as i add urlencode the link doesnt work. i had tried this in the very begining.

                            <?echo '<a href="CollectionRepPrint.php?serialize=', urlencode(serialize($SingleDisparr)) , '" title="whatever" target="_blank">link</a>';?> the link fails to function.

                            Thanks
                            Last edited by bhakti_thakkar; Sep 26, 2006, 09:40 AM. Reason: reframing

                            Comment


                            • #15
                              did you bother trying the code i posted --> that definitely works.
                              and coud you maybe post a bit more info that is actually usefull for us?
                              like the generated html for that link? or maybe the results of a print_r of the array right before your serialize it? you know --> something that we can start from to see why this simple feature would possibly not work?
                              Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                              Comment

                              Working...
                              X