Jump to content


Photo

problems appending to a url , array values


  • Please log in to reply
8 replies to this topic

#1 Rony

Rony
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 July 2006 - 06:44 PM

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[]=". $container . "&l[]=time&t=Test";
?>

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



#2 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 10 July 2006 - 06:53 PM

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?
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 07:01 PM

If you want to pass an array via a URL, you have to serialize it first.
<?php $url="http://...//piechart.php?d[]=". serialize($container) . "&l[]=time&t=Test"; ?>
In the processing script you need to unserialize to get the array back into a usable form:
<?php $url = unserialize($_GET['d'][0]); ?>
One question ... why are you using an array specification "d[]" in the URL?

Ken

#4 Rony

Rony
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 July 2006 - 07:03 PM

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[]=3&l[]=windows&d[]=7&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?

#5 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 10 July 2006 - 07:05 PM

First what you need is what way the script on the other end is picking it up.
You could think about using serialize and urlencode to send the whole array and then urldecode and unserialize 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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 07:07 PM

You can create a comma seperated list from an array by using the implode() function:
<?php $csl = implode(',',$arr); ?>

Ken

#7 Rony

Rony
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 July 2006 - 07:12 PM

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

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 07:31 PM

You're getting that because of this statement:
<?php array_push($container, array($value)); ?>
This is creating a 2-level array. If you do a
<?php echo '<pre>' . print_r($container,true) . '</pre>'; ?>
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
<?php $container[] = $value; ?>
which will create a plain array.

Ken

#9 Rony

Rony
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 July 2006 - 07:57 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users