Function to create bar chart problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 script: 
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 "$row_query=mysql_fetch_assoc($sql)*/

Any clue will be very, very, very appreciated. 

Best regards, 

Alvaro

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

<?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 consulta

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

do
{
 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));

//STARTS THE GRAPHIC CREATION
//Creates the graphic
$graph = new Graph(900,400,"auto");    
$graph->SetScale("textlin");

$graph->SetShadow();
$graph->img->SetMargin(40,30,20,40);

//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); 
$b1plot->SetFillColor("olivedrab3");
$b2plot = new BarPlot($y_data);
$b2plot->SetFillColor("orange");

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

// Executes the graphic
$graph->Add($gbplot);

$graph->title->Set("Graphic Title");
$graph->subtitle->Set("Sales");
$graph->xaxis->title->Set("Organization");
$graph->yaxis->title->Set("Sales amount in thousands");
$graph->img->SetMargin(75,30,80,80);

$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
// Displays the graph
$graph->Stroke();
}; //End of the function
?>

FILE: function_test.php (executes the function)

<?php 
require_once('../Connections/sis_mon_conn.php'); 
include('../Connections/my_connection.php'); 
include ("functions.inc.php"); 
?>
<?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");
?>
<?php
mysql_free_result($rec_sql);
?>

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




[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux