Jump to content

problems appending to a url , array values


Rony

Recommended Posts

I am getting values from a sql query and storing them in an array using the array_push function. I have tried many many things to print the array $conatiner or rather pass the values of the array into the URL. It only seems to work if you use the print_r command. I even tried using that but it would add a lot of other data which is not necessary. I have been trying to figure this out for 2 weeks now !!  ??? Please help !!

<?php

include connection file;
$result = mysql_query(" my query") or die ("LSE usage query failed");

$container = array();
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$diff = $row{'end_ts'} - $row{'start_ts'} ;
$value=  gmdate( 'H:i:s ', $diff );
array_push($container, array($value)) ;
}

$url="http://...//piechart.php?d[]=". [color=red]$container[/color] . "&l[]=time&t=Test";
?>

<a href="<?php echo $url; ?>">click me</a>

Link to comment
https://forums.phpfreaks.com/topic/14207-problems-appending-to-a-url-array-values/
Share on other sites

Hey Rony,

It appears you may be going at this a little too weird.  You are actually putting the array values inside the URL?  Why don't you build a comma seperated list, and assign them to a get variable?  Can you please explain what the recieving file looks like as well?
If you want to pass an array via a URL, you have to serialize it first.
[code]<?php $url="http://...//piechart.php?d[]=". serialize($container) . "&l[]=time&t=Test"; ?>[/code]
In the processing script you need to unserialize to get the array back into a usable form:
[code]<?php $url = unserialize($_GET['d'][0]); ?>[/code]
One question ... why are you using an array specification "d[]" in the URL?

Ken
I am passing the values to the URL to create a pie chart. Example if the array has values
$conatiner = {3,7};

The url should actually be
http://...../piechart.php?d[]=[color=red]3[/color]&l[]=windows&d[]=[color=red]7[/color]&l[]=apple&t=Test

This would show me the piechart.

I get there values 3, 7 etc from the database. I thought storing them in an array and then passing them would do teh trick. I know I need a loop for it and I tried foreach but that didnt work. How can I build a comma seperated list?
First what you need is what way the script on the other end is picking it up.
You could think about using [b]serialize[/b] and [b]urlencode[/b] to send the whole array and then [b]urldecode[/b] and [b]unserialize[/b] to turn it back into an array.

Alternatively you could send them like ?v1=x&v2=x&v3=x etc. and then cycle through all the URL parameters with V in the start.

It all depends on what way you are interpreting it.
You're getting that because of this statement:
[code]<?php array_push($container, array($value)); ?>[/code]
This is creating a 2-level array. If you do a [code]<?php echo '<pre>' . print_r($container,true) . '</pre>'; ?>[/code] you will see that each entry is an array. This also explains why you've been having problems with the foreach statement.

Change that line to be [code]<?php $container[] = $value; ?>[/code] which will create a plain array.

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.