Adding a variable to an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
finny
Forum Newbie
Posts: 15
Joined: Mon Apr 24, 2006 7:43 am

Adding a variable to an array

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Adding a variable to an array

Post 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
finny
Forum Newbie
Posts: 15
Joined: Mon Apr 24, 2006 7:43 am

Re: Adding a variable to an array

Post 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); 
 
finny
Forum Newbie
Posts: 15
Joined: Mon Apr 24, 2006 7:43 am

Re: Adding a variable to an array

Post 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.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Adding a variable to an array

Post 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'], ...);
?>
 
Last edited by EverLearning on Wed Feb 27, 2008 4:12 pm, edited 1 time in total.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Adding a variable to an array

Post by liljester »

can you post your code for form.php and graph.php?
finny
Forum Newbie
Posts: 15
Joined: Mon Apr 24, 2006 7:43 am

Re: Adding a variable to an array

Post 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.
Post Reply