i have a search function consisting of 10 check boxes in an array that when you check the boxes and press search it passes them as an array and this works fine.
problem is that i have pagination on the search results page and if the results are too long it places links to the other pages and these have query strings that need to pass the search selection criteria. the search criteria is an array, how do i pass an array by a link? i cant work it out, it just wont work. when ever i click to goto page 2 of results all i get is errors as the array doesnt get passed.

    Have you tried working it out this way:

    1.) Create a form with the method as GET
    2.) Add some inputs to it which create an array
    3.) Submit the form
    4.) Check the URL to see how the array is constructed

    Doing that should give you an idea of how it's posted as an array.

      yeah ive got the forum with the check boxes that get submitted as an array. i know how the array is constructed as i designed it. it gets passed using $_POST as an array to a foreach loop where it deconstructs the array to form the query.
      then to get the second page of results i need to pass that original array into the page 2 link so it gets reused on page 2 but cant pass it into the link for what ever reason.

        Use serialize to serialize the array, and unserialize in the next page.

          leatherback wrote:

          Use serialize to serialize the array, and unserialize in the next page.

          ive tried that but it doesnt recognise the array afterwards.

            Could we see some code or do we have to guess at how you tried to do it?

              Okay, so by following my own instructions and looking at how GET sends arrays, I was able to construct something to take an array and prep it for URL insertion. Here's what it is:

              <?php
              
              function urlizeArray($array=null, $key='var') {
              	if($array === null)
              		return '';
              
              $string = '';
              foreach($array as $item)
              	$string .= $key.'%5B%5D='.urlencode($item).'&';
              return substr($string, 0, -1);
              }
              
              ?>

              Using that function if you just send an array to it, it will properly take each element, encode it and add it to the previous element. This is for simple arrays. Multidimensional ones are a different story, and really should probably be serialized and then type-cast back to arrays.

              [Proof of Concept]

                yeah i gave up and decided it couldnt be done the way i wanted to and came up with a similar function as you did but doesnt make the query string as long though, i encoded it a different way but does same thing.
                so basically im all sorted now, everything works ok. thanks for help though.

                  maybe you could share YOUR method with the community so this thread will have some worth

                    scrupul0us wrote:

                    maybe you could share YOUR method with the community so this thread will have some worth

                    just that when the check boxes are selected the array is passed by the $_POST function and i just create a string like "3|34|76|12|74" out of it by looping them and them passing that as a string back to the script when a user clicks on the link to goto page 2 then the script just loops through that string to create a query similar to looping through the array.

                      Write a Reply...