Aero77 Posted May 16, 2014 Share Posted May 16, 2014 I got this code <?php // Check if session is not registered, redirect back to main page. // Put this code in first line of web page. session_start(); if (!isset($_COOKIE["user"])) { header("location:login.php"); } include 'connection.php'; ?> <html> <head> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="HandheldFriendly" content="true"> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>EBS Service Skjema</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <?php include 'menu.php'; echo "<div class=\"bluebox\">"; if (isset($_GET['sted'])) { require_once('graph/jpgraph.php'); require_once('graph/jpgraph_line.php'); include 'connection.php'; // data $sql = mysqli_query($con, "SELECT * FROM diesel WHERE sted = '".$_GET['sted']."' GROUP BY WEEK(dato)"); $ydata = array(); $ydate = array(); while($row = mysqli_fetch_array($sql)) { $ydata[]=$row['krl']; $ydate[]=$row['dato']; } // Create the graph. These two calls are always required $graph = new Graph(600,250); $graph->SetScale('textlin'); $graph->title->Set('Pris Historikk'); $graph->xaxis->title->Set("Dato"); $graph->yaxis->title->Set("Pris"); $graph->xaxis->SetTickLabels($ydate); // Create the linear plot $lineplot=new LinePlot($ydata); $lineplot->SetColor('blue'); //$lineplot->SetFillColor('orange@0.5'); // Add the plot to the graph $graph->Add($lineplot); // Display the graph $graph->Stroke(); } else { echo "Du får ikke tilgang på denne måten."; } echo "</div>"; ?> </body> </html> but I get headers allready sent. I was earlier told to learn and code or make my apps different, but I'm totaly stuck cause I need to make a page with those stats. Not sure how I can make all my code be processed first then put into the html area. Is a function the way to go here ? Thanks for reading and hope someone can enlighten me Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 16, 2014 Share Posted May 16, 2014 The message has a line number in it - that is your problem line. Find the # and identify the line and then show us in the code where it is. Quote Link to comment Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 (edited) It says 21 and thats where the starting <?php tag is. I'm using jpGraph and the error output is slightly different. Here it is JpGraph Error: HTTP headers have already been sent.Caused by output from file history.php at line 21. Explanation:HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).Most likely you have some text in your script before the call to Graph::Stroke(). If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser. For example it is a common mistake to leave a blank line before the opening "<?php". Here is line #21 <?php // Check if session is not registered, redirect back to main page. // Put this code in first line of web page. session_start(); if (!isset($_COOKIE["user"])) { header("location:login.php"); } include 'connection.php'; ?> <html> <head> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="HandheldFriendly" content="true"> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>EBS Service Skjema</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <?php include 'menu.php'; echo "<div class=\"bluebox\">"; if (isset($_GET['sted'])) { require_once('graph/jpgraph.php'); require_once('graph/jpgraph_line.php'); include 'connection.php'; // data $sql = mysqli_query($con, "SELECT * FROM diesel WHERE sted = '".$_GET['sted']."' GROUP BY WEEK(dato)"); $ydata = array(); $ydate = array(); while($row = mysqli_fetch_array($sql)) { $ydata[]=$row['krl']; $ydate[]=$row['dato']; } // Create the graph. These two calls are always required $graph = new Graph(600,250); $graph->SetScale('textlin'); $graph->title->Set('Pris Historikk'); $graph->xaxis->title->Set("Dato"); $graph->yaxis->title->Set("Pris"); $graph->xaxis->SetTickLabels($ydate); // Create the linear plot $lineplot=new LinePlot($ydata); $lineplot->SetColor('blue'); //$lineplot->SetFillColor('orange@0.5'); // Add the plot to the graph $graph->Add($lineplot); // Display the graph $graph->Stroke(); } else { echo "Du får ikke tilgang på denne måten."; } echo "</div>"; ?> </body> </html> Edited May 16, 2014 by Aero77 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 16, 2014 Share Posted May 16, 2014 Are you showing us history.php or is the code above from some other script? Quote Link to comment Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 (edited) Its the code from history.php Edited May 16, 2014 by Aero77 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 16, 2014 Share Posted May 16, 2014 As the message says you can't output anything before your graph package does its output. You are sending the html for your page prior to the graph package calls, so that is your problem. However I'm not entirely sure since line 21 is NOT doing anything so it is a bit confusing. When you said "here is line 21" are we seeing the VERY beginning of the currently running script or a part of the whole thing? Quote Link to comment Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 I updated my post with the full code and line numbers. I just found this post btw http://stackoverflow.com/questions/10487796/jpgraph-error-http-headers-have-already-been-sent Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 16, 2014 Share Posted May 16, 2014 Where is this updated code? I don't see it in your post. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 16, 2014 Share Posted May 16, 2014 looks like you found your solution. good bye Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 16, 2014 Share Posted May 16, 2014 Well, this is one of those famous PHP issues. Always put the application logic to the very top of the script and the HTML to the very bottom. Do not mix logic with markup like you currently do. Once there's output, PHP can no longer send HTTP headers. So your script should look like this: <?php // all the application logic in a single PHP block ?> <!-- Now the HTML, possibly mixed with short PHP blocks --> Quote Link to comment Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 (edited) How can I process something, and then output it somewhere else ? I tried with functions, but didnt seem to work either Edited May 16, 2014 by Aero77 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 16, 2014 Share Posted May 16, 2014 (edited) jpgraph dynamically produces an image (i.e. the selected content type header followed by the binary image data.) you cannot output an image directly on a html web page. you must use an <img src='url_that_results_in_an_image'> tag on your web page to cause an image to be rendered by the browser, where the url_that_results_in_an_image would be a url to a .php script that uses jpgraph inside that .php script. most of the jpgraph examples included with the jpgraph download are the .php code that would go in the file that's supplied as the url to the <img src = ' ... '> tag. you can browse directly to the .php files with the jpgraph code in them, because the browser can figure out what to do based on the content type header/binary image data the the file sends, but in order to put a dynamically created image onto a web page, you must use an <img src = ' ... '> tag. Edited May 16, 2014 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.