Page 1 of 1

Adding a variable to an array

Posted: Wed Feb 27, 2008 7:59 am
by finny
Hi
Is it possible to add a php variable to an array?
I am using a php script to graph an array of numbers. eg....

Code: Select all

 $data=array(122,44,55,66,89,25,99); 
Is it possible to add a variable into that array. The code below is what I am entering but I cant get it to work...

Code: Select all

$data=array($var1,$var2,$var3); 
First question is...
Is it possible?

Second Question is...
If it is possible how far wrong is my syntax?

Thanks for any help.

Re: Adding a variable to an array

Posted: Wed Feb 27, 2008 8:09 am
by Zoxive
Your syntax is correct, is your array formatted properly for your graph function?

Code: Select all

print '<pre>'; print_r ($data); print '</pre>';
The Fearful Developer

Re: Adding a variable to an array

Posted: Wed Feb 27, 2008 8:21 am
by finny
Thanks.
The bar graph script I am using uses a GD Library function to create a graph of the figures in the array which is beyond my knowledge of php. It works perfectly when I use numbers in the array but if I attempt to take those numbers from variables which the user enters from a form it will not generate the image.

Even if I enter the variable in the code and then replace the numbers in the necessary place with $var1,$var2 (like below) the image wont be generated.

Code: Select all

$var1 = 23;
$var2 = 56;
$data=array($var1,$var2); 
 

Re: Adding a variable to an array

Posted: Wed Feb 27, 2008 10:28 am
by finny
Apologies, the problem is not were I thought. Basically I have a form.php file where I enter the variables. When I submit the form it calls form_results.php which contains a list of all the data entered on the previous page and calls graph.php which generates the graph using phpgraph.php.

So, the question really is why wont my variables from form.php be passed to graph.php and subsequently phpgraph.php?

Could it be because the way graph.php is called in form_results.php? i.e. it is called using <img src="http://www.website.com/graph/graph.php" />

Sorry if its very obvious but my PHP skills are limited.

Re: Adding a variable to an array

Posted: Wed Feb 27, 2008 2:56 pm
by EverLearning
To make these form values accessible to graph.php you need to either pass that data via $_GET

Code: Select all

<img src="http://www.website.com/graph/graph.php?field_1=<?php echo $field_1; ?>" />
or via $_SESSION, and then retrieve them in the graph.php, using $_GET or $_SESSION respectively.

Code: Select all

 
<?php
array($_GET['field_1'], ...);
?>
 

Re: Adding a variable to an array

Posted: Wed Feb 27, 2008 3:02 pm
by liljester
can you post your code for form.php and graph.php?

Re: Adding a variable to an array

Posted: Thu Feb 28, 2008 1:16 pm
by finny
Thanks for all your help. I used session variables to store the numbers and recalled them on the graph.php file. The section of code turned out like this...

Code: Select all

<?php
session_start(); 
include("phpgraph.php"); 
$data=array("BIN 01"=>$_SESSION['bin1'],"BIN 02"=>$_SESSION['bin2'],"BIN 03"=>$_SESSION['bin3'],"BIN 04"=>$_SESSION['bin4'],"BIN 05"=>$_SESSION['bin5'],"BIN 06"=>$_SESSION['bin6'],"BIN 07"=>$_SESSION['bin7'],"BIN 08"=>$_SESSION['bin8'],"BIN 09"=>$_SESSION['bin9'],"BIN 10"=>$_SESSION['bin10'],"BIN 11"=>$_SESSION['bin11'],"BIN 12"=>$_SESSION['bin12']);
Thanks again.