How to pass an array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

How to pass an array?

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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;
}

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Thanks! I got it.

ljCharlie
Post Reply