Link to home
Start Free TrialLog in
Avatar of maunded
maunded

asked on

PHP JPGraph and MSSQL

Hi,
I am having a problem using jpgraph with some php code.
FIrstly, the php script gets some data from mssql, then displays relevant data on a page
WHere the problem is, is i am calling an external .php to build the jpgraph graphs and i keep getting  image placeholders.  Im guessing this is because the headers for the page have already been sent.  Heres some code:

main.php
<?
  $bat_cur = 0;
  $lamp_cur = 0;
  $array_vol = 0;
  $mains_vol = 0;
  $bat_vol = 0;
$image = "fig1.php?name=B.Curr&val=$bat_cur&min=0&max=10";
require ('globals.inc');
$result = mssql_query("SELECT     TOP 1 SAT24R3.[Main Lamp Fail], SAT24R3.[Standy Lamp Fail], SAT24R3.[Inverter Fail], SAT24R3.[Mains Power Fail], SAT24R3.[Generator Fail],
                      SAT24R3.[Photo Cell Fail], SAT24R3.[Fuel Level Low], SAT24R3.[Security Door Open],
                      SAT24R3.[Rotation RPM], SAT24R3.[Battery Volts], SAT24R3.[Mains Volts], SAT24R3.[Array Volts], SAT24R3.[Lamp Current], SAT24R3.[Battery Current],
                      SAT24R3.MessageIdentifier
FROM         ST_Terminal_CommonName_Table INNER JOIN
                      DapDeliverMessage ON ST_Terminal_CommonName_Table.TerminalID = DapDeliverMessage.TerminalSubscriptionID INNER JOIN
                      SAT24R3 ON DapDeliverMessage.ID = SAT24R3.DapDeliverMessageID
ORDER BY DapDeliverMessage.ReceivedTimestamp DESC");
while ($data = mssql_fetch_array($result)) {
  $main_lamp = $data['0'];
  $standby_lamp = $data['1'];
  $inverter = $data['2'];
  $main_power = $data['3'];
  $generator = $data['4'];
  $photo_cell = $data['5'];
  $fuel_low = $data['6'];
  $door = $data['7'];
  $bat_cur = $data['13'];
  $lamp_cur = $data['12'];
  $array_vol = $data['11'];
  $mains_vol = $data['10'];
  $bat_vol = $data['9'];
  $rotation = $data['8'];
}

print ("
<body>

<table border =\"1\" font face=\"tahoma\" size=\"2\" width = \"50%\">
<tr>
<td><img src=\"fig1.php?name=B.Curr&val=$bat_cur&min=0&max=10&min=0&max=10\" border=0 align=center></td>
<td><img src=\"fig1.php?name=L.Curr&val=$lamp_cur&min=0&max=10\" border=0 align=center></td>
<td><img src=\"fig1.php?name=Array&val=$array_vol&min=0&max=10\" border=0 align=center></td>
<td><img src=\"fig1.php?name=Mains&val=$mains_vol&min=0&max=10\" border=0 align=center></td>
<td><img src=\"fig1.php?name=B.Volt&val=$bat_vol&min=0&max=10\" border=0 align=center></td>
<td><img src=\"fig1.php?name=Rot.RPM&val=$rotation&min=0&max=10\" border=0 align=center></td>



</tr>
<tr>
<td align=\"center\"><font face=\"tahoma\" size=2\">$bat_cur</td>
<td align=\"center\"><font face=\"tahoma\" size=2\">$lamp_cur</td>
<td align=\"center\"><font face=\"tahoma\" size=2\">$array_vol</td>
<td align=\"center\"><font face=\"tahoma\" size=2\">$mains_vol</td>
<td align=\"center\"><font face=\"tahoma\" size=2\">$bat_vol</td>
<td align=\"center\"><font face=\"tahoma\" size=2\">$rotation</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table border =\"1\" font face=\"tahoma\" size=\"2\" width = \"50%\">
<tr>
<td>Main Lamp</td><td><img src=\"$main_lamp.gif\"><br></td>
<td>Standby Lamp</td><td><img src=\"$standby_lamp.gif\"></td>
<td>Inverter</td><td><img src=\"$inverter.gif\"></td>
<td>Main Power</td><td><img src=\"$main_power.gif\"></td>
<td>Generator</td><td><img src=\"$generator.gif\"></td>
<td>Photo Cell</td><td><img src=\"$photo_cell.gif\"></td>
<td>Fuel Low</td><td><img src=\"$fuel_low.gif\"></td>
<td>Security Door</td><td><img src=\"$door.gif\"></td>

</tr>

</body>
</html>");
?>

fig1.php
<?PHP
$name = $_GET['name'];
$datay[] = $_GET['val'];
$minval = $_GET['min'];
$maxval = $_GET['max'];
include ("globals.inc");
include ("JPGraph/jpgraph.php");
include ("JPGraph/jpgraph_bar.php");

// Create the graph. These two calls are always required
$graph = new Graph(35, 150,"auto");    
$graph->SetScale("textlin",$minval,$maxval);

// Setup margin and titles
$graph->img->SetMargin(20,5,10,0);
$graph->yaxis->SetFont(FF_FONT0,FS_NORMAL,0.5);
$graph->title->Set($name);
$graph->title->SetFont(FF_FONT0,FS_NORMAL,0.5);
$graph->xaxis->SetFont(FF_FONT0,FS_NORMAL,0.5);
$graph->xaxis->scale->ticks->Set(1);
$graph->yaxis->SetLabelMargin(2);

// Create the bar plot
$bplot = new BarPlot($datay);

// Setup color for gradient fill style
$bplot->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);

// Set color for the frame of each bar
$bplot->SetColor("navy");
$bplot->SetWidth(1.0);
$graph->Add($bplot);

// Add the plot to the graph

$graph->Add($bplot);

// Display the graph
$graph->Stroke();
?>

Basically it displays an indicator graph for each of the values from the database.
Can someone help me out on this?
CHeers
D.
Avatar of hernst42
hernst42
Flag of Germany image

Does the image display correctly when you open it in a new window (right-click on the image -> copy link location, open that url in a new window) and see if there are any PHP-errors or something else which prevents the display of the image) Or is the image displayed correctly in that window?
Avatar of maunded
maunded

ASKER

If I right click and do Show Image it is OK
Avatar of maunded

ASKER

Its OK, I furgured that I need to use a function to create each of the graphs before I pass any data to the browser.
Not sure how I close an open question now tho???
Avatar of maunded

ASKER

I solved this problem by instead of calling an external file (fig1.php) to build the graphs, i created a function in the main php code and put the fig1.php code into that, so before I output anything to the browser, I call the function which creates the graph images.
Hope this can help someone a little more patient than I :)
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial