Hi guys. I am a newbie triying to do grown-up people stuff :)

I am trying to write a function to create a bar chart using JPGraph where I 
have to pass two variables (query name, and x axis column name) and the 
function creates the graphic. It is because I have several queries with 
diferent length, so I would like to have a function to create lots of graphics 
without to much coding. 

I have two questions: 

a) How could I create an array which collects the result of a 
mysql_fetch_array() function (excluding only the first column). 

b) How can I execute the string from a variable as if it where part of the 
For example I have the following variable 

var1 = '$y_'.$row_query[$org_field_name].' = new BarPlot('.($row_query).');'; 

which creates a string like this: 

$y_org1=new BarPlot(array); 

/* "$y_org1" is the result of the query  "$row_query[$org_field_name]" and 
"$row_query" is the array which contains the result of 

Any clue will be very, very, very appreciated. 

Best regards, 


Following is the whole code. 

1. The sample table is as follows 
| org_name | field1 | field2 | field3 |
| org1     |      1 |      5 |     10 |
| org2     |     12 |      4 |      8 |
| org3     |     45 |     12 |     20 |
| org4     |     40 |     30 |      4 |

2. The script as far as I've got it is: 
FILE: functions.inc.php

include ( "../../addons/jpgraph/src/jpgraph.php"); 
include ("../../addons/jpgraph/src/jpgraph_bar.php");

function fnc_y_name($query_name, $org_field_name)
$row_query=mysql_fetch_assoc($query_name);//Array que recoge los datos de las 
filas de la consulta. 
$total_rows=mysql_num_rows($query_name); //Número total de filas que devuelve 
la consulta
$total_fields=mysql_num_fields($query_name);//Número total de campos en la 

for($i=1; $i<$total_fields; $i++) 
 $fieldname = mysql_field_name($query_name,$i);
 $array_fieldname[] = $fieldname;

 for($i=0; $i<$total_fields-1; $i++) 
  $y_data[] = $row_query[$array_fieldname[$i]]; //Here is my bigger problem.
} while ($row_query=mysql_fetch_assoc($query_name));

//Creates the graphic
$graph = new Graph(900,400,"auto");    


//I will try to build this calls with a do..while loop from the data collected 
from the y_data array. This is why I need to pass the variable or array 
contents directly to the script. 

$b1plot = new BarPlot($y_data); 
$b2plot = new BarPlot($y_data);

// Joins the plots
$gbplot = new GroupBarPlot(array($b1plot, $b2plot));

// Executes the graphic

$graph->title->Set("Graphic Title");
$graph->yaxis->title->Set("Sales amount in thousands");

// Displays the graph
}; //End of the function

FILE: function_test.php (executes the function)

include ("functions.inc.php"); 
mysql_select_db($database_sis_mon_conn, $sis_mon_conn);
$query_rec_sql = "SELECT * FROM test";
$rec_sql = mysql_query($query_rec_sql, $sis_mon_conn) or die(mysql_error());
$row_rec_sql = mysql_fetch_assoc($rec_sql);
$totalRows_rec_sql = mysql_num_rows($rec_sql);

mysql_data_seek($rec_sql, 0);
fnc_y_name($rec_sql, "org_name");

//I know, the code is awful, but is the best I have got with my poor experience

Reply via email to