سؤال

I appear to have the problem - 'HTTP headers have already been sent.' It says they are being send on the line <title><? echo $hall_name['name'];?></title> which I understand is because I have the 'echo'.

I need to have the title of the page echo'd from a database, so I'm not sure how I am supposed to work around this problem.

<?php
// First execute our common code to connection to the   database and start the session 
require("common.php");

//find the university's id from the url
$current_id = $_GET[id]; 

//run a query to find the name of the hall, using the id in the url ($current_id)
  if ($findname = $db->prepare("SELECT * FROM hall WHERE id = :current_id")) { 
  $findname->bindParam(':current_id', $current_id);
  $findname->execute(); // Execute the prepared query.

//search table for all fields and save them in $hall_name
$nametemp = $findname->fetchAll();
foreach( $nametemp as $hall_name) {
?>
<head>

<title><? echo $hall_name['name'];?></title>

</head>

<body>

 <div id="Name">
 <? echo $hall_name['name']; }}?>
 </div>

<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');

$datay1 = array(20,15,23,15);
$datay2 = array(12,9,42,8);
$datay3 = array(5,17,32,24);

// Setup the graph
$graph = new Graph(300,250);
$graph->SetScale("textlin");

$theme_class=new UniversalTheme;

$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing(false);
$graph->title->Set('Filled Y-grid');
$graph->SetBox(false);

$graph->img->SetAntiAliasing();

$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

$graph->xgrid->Show();
$graph->xgrid->SetLineStyle("solid");
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->xgrid->SetColor('#E3E3E3');

// Create the first line
$p1 = new LinePlot($datay1);
$graph->Add($p1);
$p1->SetColor("#6495ED");
$p1->SetLegend('Line 1');

// Create the second line
$p2 = new LinePlot($datay2);
$graph->Add($p2);
$p2->SetColor("#B22222");
$p2->SetLegend('Line 2');

// Create the third line
$p3 = new LinePlot($datay3);
$graph->Add($p3);
$p3->SetColor("#FF1493");
$p3->SetLegend('Line 3');

$graph->legend->SetFrameWeight(1);

// Output line
$graph->Stroke();

?>

</body>
</html>

(p.s. I have removed most of the html code that is not relevant)

هل كانت مفيدة؟

المحلول

You're sending out htmls before your image headers are sent. The headers must be the first thing the browser should receive before any outputs at all.

The solution here would be that you separate the the code that generates the image and the code that echoes the normal html into two different scripts.

Consider the first script foo.php that contains the code ONLY for image generation. Make sure you don't echo anything else here.

Then, have everything else (htmls & page contents) in a different script (lets say bar.php). And place an image tag here such that it displays the image that the first script (foo.php) generates.

<img src="foo.php">
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top