Jump to content

Archived

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

Rony

problems appending to a url , array values

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>

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You can create a comma seperated list from an array by using the implode() function:
[code]<?php $csl = implode(',',$arr); ?>[/code]

Ken

Share this post


Link to post
Share on other sites
Ken, I tried the implode function before. It just gives me Array,Array,Array  How do I get the actual values (I also tried array_values)

Shogun, I didnt not understand how can I passteh variables like ?v1=x&v2=x&v3=x
I trying the serialize and urlencode now

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Ken, It worked !! I had used simple array before I looked up array_push but I dont know what I did differently then. Before it kept on showing me just the last value in the array outside the while loop.

Everyone thank you for all your help !! :D

Share this post


Link to post
Share on other sites

×

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.