Solved

PHP JPGraph and MSSQL

Posted on 2004-08-22
7
543 Views
Last Modified: 2013-12-12
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.
0
Comment
Question by:maunded
  • 3
  • 2
7 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 11867586
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?
0
 
LVL 1

Author Comment

by:maunded
ID: 11867589
If I right click and do Show Image it is OK
0
 
LVL 1

Author Comment

by:maunded
ID: 11869138
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???
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 48

Expert Comment

by:hernst42
ID: 11869402
0
 
LVL 1

Author Comment

by:maunded
ID: 11876639
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 :)
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 11920589
Closed, 500 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now