Page 1 of 1

How to pass an array?

Posted: Thu Oct 21, 2004 1:37 pm
by ljCharlie
Is it possible to pass an array through url? Here's what I have done:

Code: Select all

$numbPics = array();
//while loop below here
$sectID = $row_rsSection["sectionID"];
$numbPics[] = $picID;
echo "<a href=photoGallery.php?imageID=$picID&sectionID=$sectID&picIDarray=$numbPics target='_parent' class='submenu'>Picture Here</a>";
On the _parent page:

Code: Select all

if(!isset($HTTP_GET_VARS["picIDarray"])){
			$numPicArray = array();
			$numPicArray = $HTTP_GET_VARS["picIDarray"];
			foreach($numPicArray as $val){
				echo "Value is: ".$val."<br>";
				}
			}
But this is not working. When echoing the value in the _parent page, nothing is showing.

ljCharlie

Posted: Thu Oct 21, 2004 1:42 pm
by timvw
depends on what you are trying to do:

Code: Select all

<?php
// +---------------------------------------------------------------------------
// | builds an url
// |
// | $args should be an associative array that contains $key, $value pairs
// |
// +---------------------------------------------------------------------------
function buildurl($url, $args)
{
  // test if there are already $_GET variables
  $has_get = false;

  $pos = strpos($url, '?');
  if ($pos === false)
  {
    $has_get = false;
  }
  else
  {
    $has_get = true;
  }

  // now add each pair to the url
  foreach($args as $key => $val)
  {
    $val = urlencode($val);

    if ($has_get)
    {
      $url .= '&' . $key . '=' . $val;
    }
    else
    {
      $url .= '?' . $key . '=' . $val;
    }
    $has_get = true;
  }

  return $url;
}

?>

Posted: Thu Oct 21, 2004 1:49 pm
by Weirdan
to pass an array you would use something like this:

Code: Select all

http://site.com/path/script?param&#1111;]=a&param&#1111;]=b&param&#1111;something]=blah

Posted: Thu Oct 21, 2004 1:52 pm
by ljCharlie
Thanks for the response. Here's what I'm trying to do. In the iframe page, I have picture thumbnails. I store all the picture ID's in the array. Once the user click on any of those thumbnails, pass the current picture ID that the user click and also the array that I use to store all the picture ID's. I like to know if my code is correct for passing an array and getting an array on the _parent page.

ljCharlie

Posted: Thu Oct 21, 2004 1:57 pm
by ljCharlie
So Weirdan, in my case, I would have something like this:

Code: Select all

$numbPics = array();
//while loop below here
$sectID = $row_rsSection["sectionID"];
$numbPics[] = $picID;
echo "<a href=photoGallery.php?imageID=$picID&sectionID=$sectID&picIDarray[]=$numbPics target='_parent' class='submenu'>Picture Here</a>";
But what about when retrieving the array on the _parent page?

ljCharlie

Posted: Thu Oct 21, 2004 2:09 pm
by Weirdan
nop. Here is simple function to construct arrays for GET requests:

Code: Select all

function array2get($name, $array) {
   $ret = '';
   foreach($array as $key => $val) {
      if(strlen($ret)) $ret .= '&';
      $ret .= $name.'['.$key.']='.strval($val);
   }
   return $ret;
}
// for aforementioned url it would be used as
$q = array('a','b','something'=>'blah');
echo 'http://site.com/path/script?' . array2get('param', $q);
note: this function does not support multidimensional arrays.

Posted: Thu Oct 21, 2004 2:47 pm
by ljCharlie
Thanks! I got it.

ljCharlie