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
ljCharlie
Forum Contributor
Posts: 289 Joined: Wed May 19, 2004 8:23 am
Post
by ljCharlie » Thu Oct 21, 2004 1:37 pm
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§ionID=$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 » Thu Oct 21, 2004 1:42 pm
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;
}
?>
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Thu Oct 21, 2004 1:49 pm
to pass an array you would use something like this:
Code: Select all
http://site.com/path/script?paramї]=a¶mї]=b¶mїsomething]=blah
ljCharlie
Forum Contributor
Posts: 289 Joined: Wed May 19, 2004 8:23 am
Post
by ljCharlie » Thu Oct 21, 2004 1:52 pm
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 » Thu Oct 21, 2004 1:57 pm
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§ionID=$sectID&picIDarray[]=$numbPics target='_parent' class='submenu'>Picture Here</a>";
But what about when retrieving the array on the _parent page?
ljCharlie
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Thu Oct 21, 2004 2:09 pm
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 » Thu Oct 21, 2004 2:47 pm
Thanks! I got it.
ljCharlie