PHP echo不发送头文件

时间:2014-02-19 17:19:12

标签: php html header

我似乎遇到了问题 - 'HTTP标头已经发送过'。它说它们是在<title><? echo $hall_name['name'];?></title>上发送的,我理解这是因为我有'回声'。

我需要从数据库中回显页面的标题,所以我不确定我应该如何解决这个问题。

<?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。我删除了大部分不相关的HTML代码)

1 个答案:

答案 0 :(得分:1)

您在发送图片标头之前发送了htmls。标题必须是浏览器在输出任何输出之前应首先接收的内容。

这里的解决方案是将生成图像的代码和回显普通html的代码分成两个不同的脚本。

考虑第一个脚本foo.php,其中包含用于生成图像的代码 ONLY 。确保你没有回应其他任何东西。

然后,将其他所有内容( htmls&amp; page contents )放在另一个脚本中(假设为bar.php)。并在此处放置一个图像标记,以便显示第一个脚本(foo.php)生成的图像。

<img src="foo.php">