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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.