shahinshyd
جمعه 04 شهریور 1390, 22:50 عصر
با سلام
من یک مشکلی تو این کد دارم و اون هم کار با تابع header هست .من تا اونجا که اطلاع دارم هر صفحه php دارای یک header هست که شامل اطلاعاتی از ساختار صفحه است و برای استفاده از تابع Header نباید قبلش هیچ خروجی داشته باشیم و باید اول صفحه فراخوانی بشه ولی گاهی میشه که ما میخوایم تحت شرایط خاصی از Header استفاده کنیم
مثلا
<?php
function ali()
{
/*******************************************
Database query to get poll info
*******************************************/
// get vote from form
$vote=$_REQUEST['vote'];
// log in to database
if (!$db_conn = mysql_connect('localhost','root', ''))
{
echo 'Could not connect to db<br />';
exit;
};
@mysql_select_db('db_news');
if (!empty($vote)) // if they filled the form out, add their vote
{
$vote = addslashes($vote);
$query = "update poll_results
set num_votes = num_votes + 1
where candidate = '$vote'";
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
};
// get current results of poll, regardless of whether they voted
$query = 'select * from poll_results';
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
$num_candidates = mysql_num_rows($result);
// calculate total number of votes so far
$total_votes=0;
while ($row = mysql_fetch_object ($result))
{
$total_votes += $row->num_votes;
}
mysql_data_seek($result, 0); // reset result pointer
/*******************************************
Initial calculations for graph
*******************************************/
// set up constants
putenv('GDFONTPATH=C:\WINDOWS\Fonts');
$width=500; // width of image in pixels - this will fit in 640x480
$left_margin = 50; // space to leave on left of graph
$right_margin= 50; // ditto right
$bar_height = 40;
$bar_spacing = $bar_height/2;
$title_size= 16; // point
$main_size= 12; // point
$small_size= 12; // point
$text_indent = 10; // position for text labels from edge of image
// set up initial point to draw from
$x = $left_margin + 60; // place to draw baseline of the graph
$y = 50; // ditto
$bar_unit = ($width-($x+$right_margin)) / 100; // one "point" on the graph
// calculate height of graph - bars plus gaps plus some margin
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;
/*******************************************
Set up base image
*******************************************/
// create a blank canvas
$im = ImageCreateTrueColor($width,$height);
// Allocate colors
$white=ImageColorAllocate($im,255,255,255);
$blue=ImageColorAllocate($im,0,64,128);
$black=ImageColorAllocate($im,0,0,0);
$pink = ImageColorAllocate($im,255,78,243);
$text_color = $black;
$percent_color = $black;
$bg_color = $white;
$line_color = $black;
$bar_color = $blue;
$number_color = $pink;
// Create "canvas" to draw on
ImageFilledRectangle($im,0,0,$width,$height,$bg_co lor);
// Draw outline around canvas
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);
// Add title
$title = 'Poll Results';
$title_dimensions = ImageTTFBBox($title_size, 0, "arial.ttf", $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x = ($width-$title_length)/2; // center it in x
$title_y = ($y - $title_height)/2 + $title_above_line; // center in y gap
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, "arial.ttf", $title);
// Draw a base line from a little above first bar location
// to a little below last
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);
/*******************************************
Draw data into graph
*******************************************/
// Get each line of db data and draw corresponding bars
while ($row = mysql_fetch_object ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;
// display percent for this value
$percent_dimensions = ImageTTFBBox($main_size, 0, "arial.ttf", $percent.'%');
$percent_length = $percent_dimensions[2] - $percent_dimensions[0];
ImageTTFText($im, $main_size, 0, $width-$percent_length-$text_indent,
$y+($bar_height/2), $percent_color, "arial.ttf", $percent.'%');
if ($total_votes > 0)
$right_value = intval(round(($row->num_votes/$total_votes)*100));
else
$right_value = 0;
// length of bar for this value
$bar_length = $x + ($right_value * $bar_unit);
// draw bar for this value
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);
// draw title for this value
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/2),
$text_color, "arial.ttf", "$row->candidate");
// draw outline showing 100%
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);
// display numbers
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)-50, $y+($bar_height/2),
$number_color, "arial.ttf", $row->num_votes.'/'.$total_votes);
// move down to next bar
$y=$y+($bar_height+$bar_spacing);
}
/*******************************************
Display image
*******************************************/
header('Content-type:image/png');
ImagePNG($im);
/*******************************************
Clean up
*******************************************/
ImageDestroy($im);
}
?>
<html>
<body>
<p style="float:right">Its like comparing apples to oranges.</p>
<?php ali();?>
</body>
</html>
تو این مثال بعد از دریافت اطلاعات از یک فرم نظر سنجی نتیجه اون نمایش داده میشه ولی مشکل من این هست که پیغام خطا زیر رو میده
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\d\show_poll.php:174) in C:\xampp\htdocs\d\show_poll.php on line 162
من تو سایت php.net گشتم اونجا چند تا تابع معرفی کرده بود
ob_start("")
ob_end_flush()
ob_end_clean()
ولی من هر کاری کردم نتونستم در این کد ازشون استفاده کنم
یه جای دیگه هم یه راه دیگه نوشته بود
که باید encoding فایل رو به uft-8 without bom تبدیل کنیم ولی این روش جواب نداد
من یک مشکلی تو این کد دارم و اون هم کار با تابع header هست .من تا اونجا که اطلاع دارم هر صفحه php دارای یک header هست که شامل اطلاعاتی از ساختار صفحه است و برای استفاده از تابع Header نباید قبلش هیچ خروجی داشته باشیم و باید اول صفحه فراخوانی بشه ولی گاهی میشه که ما میخوایم تحت شرایط خاصی از Header استفاده کنیم
مثلا
<?php
function ali()
{
/*******************************************
Database query to get poll info
*******************************************/
// get vote from form
$vote=$_REQUEST['vote'];
// log in to database
if (!$db_conn = mysql_connect('localhost','root', ''))
{
echo 'Could not connect to db<br />';
exit;
};
@mysql_select_db('db_news');
if (!empty($vote)) // if they filled the form out, add their vote
{
$vote = addslashes($vote);
$query = "update poll_results
set num_votes = num_votes + 1
where candidate = '$vote'";
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
};
// get current results of poll, regardless of whether they voted
$query = 'select * from poll_results';
if(!($result = @mysql_query($query, $db_conn)))
{
echo 'Could not connect to db<br />';
exit;
}
$num_candidates = mysql_num_rows($result);
// calculate total number of votes so far
$total_votes=0;
while ($row = mysql_fetch_object ($result))
{
$total_votes += $row->num_votes;
}
mysql_data_seek($result, 0); // reset result pointer
/*******************************************
Initial calculations for graph
*******************************************/
// set up constants
putenv('GDFONTPATH=C:\WINDOWS\Fonts');
$width=500; // width of image in pixels - this will fit in 640x480
$left_margin = 50; // space to leave on left of graph
$right_margin= 50; // ditto right
$bar_height = 40;
$bar_spacing = $bar_height/2;
$title_size= 16; // point
$main_size= 12; // point
$small_size= 12; // point
$text_indent = 10; // position for text labels from edge of image
// set up initial point to draw from
$x = $left_margin + 60; // place to draw baseline of the graph
$y = 50; // ditto
$bar_unit = ($width-($x+$right_margin)) / 100; // one "point" on the graph
// calculate height of graph - bars plus gaps plus some margin
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;
/*******************************************
Set up base image
*******************************************/
// create a blank canvas
$im = ImageCreateTrueColor($width,$height);
// Allocate colors
$white=ImageColorAllocate($im,255,255,255);
$blue=ImageColorAllocate($im,0,64,128);
$black=ImageColorAllocate($im,0,0,0);
$pink = ImageColorAllocate($im,255,78,243);
$text_color = $black;
$percent_color = $black;
$bg_color = $white;
$line_color = $black;
$bar_color = $blue;
$number_color = $pink;
// Create "canvas" to draw on
ImageFilledRectangle($im,0,0,$width,$height,$bg_co lor);
// Draw outline around canvas
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);
// Add title
$title = 'Poll Results';
$title_dimensions = ImageTTFBBox($title_size, 0, "arial.ttf", $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x = ($width-$title_length)/2; // center it in x
$title_y = ($y - $title_height)/2 + $title_above_line; // center in y gap
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, "arial.ttf", $title);
// Draw a base line from a little above first bar location
// to a little below last
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);
/*******************************************
Draw data into graph
*******************************************/
// Get each line of db data and draw corresponding bars
while ($row = mysql_fetch_object ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;
// display percent for this value
$percent_dimensions = ImageTTFBBox($main_size, 0, "arial.ttf", $percent.'%');
$percent_length = $percent_dimensions[2] - $percent_dimensions[0];
ImageTTFText($im, $main_size, 0, $width-$percent_length-$text_indent,
$y+($bar_height/2), $percent_color, "arial.ttf", $percent.'%');
if ($total_votes > 0)
$right_value = intval(round(($row->num_votes/$total_votes)*100));
else
$right_value = 0;
// length of bar for this value
$bar_length = $x + ($right_value * $bar_unit);
// draw bar for this value
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);
// draw title for this value
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/2),
$text_color, "arial.ttf", "$row->candidate");
// draw outline showing 100%
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);
// display numbers
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)-50, $y+($bar_height/2),
$number_color, "arial.ttf", $row->num_votes.'/'.$total_votes);
// move down to next bar
$y=$y+($bar_height+$bar_spacing);
}
/*******************************************
Display image
*******************************************/
header('Content-type:image/png');
ImagePNG($im);
/*******************************************
Clean up
*******************************************/
ImageDestroy($im);
}
?>
<html>
<body>
<p style="float:right">Its like comparing apples to oranges.</p>
<?php ali();?>
</body>
</html>
تو این مثال بعد از دریافت اطلاعات از یک فرم نظر سنجی نتیجه اون نمایش داده میشه ولی مشکل من این هست که پیغام خطا زیر رو میده
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\d\show_poll.php:174) in C:\xampp\htdocs\d\show_poll.php on line 162
من تو سایت php.net گشتم اونجا چند تا تابع معرفی کرده بود
ob_start("")
ob_end_flush()
ob_end_clean()
ولی من هر کاری کردم نتونستم در این کد ازشون استفاده کنم
یه جای دیگه هم یه راه دیگه نوشته بود
که باید encoding فایل رو به uft-8 without bom تبدیل کنیم ولی این روش جواب نداد