0

所以我遇到的问题是我无法将在页面 Results.html 上计算的信息传递给脚本,我必须生成一个饼图,然后获取图像并将其显示在我的结果页面中。我对 PHP 非常陌生(大约 2 周)并且只有最基本的了解,所以我需要非常明确的说明。无论哪种方式,这是我正在使用的代码:

<?php
    $filename = "Results.txt";
    $lines = file($filename);

    $q1 = $_POST['q1']; // stores checked button value
    $q2 = $_POST['q2'];
    $q3 = $_POST['q3'];
    $q4 = $_POST['q4'];
    $q5 = $_POST['q5'];
    $q6 = $_POST['q6'];
    $q7 = $_POST['q7'];
    $q8 = $_POST['q8'];

    $qN = 1;  //question number

    $newLines = '';

    $total = array();

        foreach($lines as $line) {

            $line = trim($line);  //remove excess newlines etc.
            $lineArr = explode(',',$line);  //split line into array by commas
            $index = ${'q'.$qN}-1; //zero based
        if (isset($lineArr[$index])){
            $lineArr[$index]++;  //add to position by one vote.
            $total = array_sum($lineArr); //number of clicks 

            $newLines .= implode(',',$lineArr) . "\r\n"; //newLines contains numbers then also
        $qN++;
        }
    }
//write contents back to file.
file_put_contents($filename, $newLines);

//frequencies for each line without strings
$l[1] = explode(',',$lines[0]);
$l[2] = explode(',',$lines[1]);
$l[3] = explode(',',$lines[2]);
$l[4] = explode(',',$lines[3]);
$l[5] = explode(',',$lines[4]);
$l[6] = explode(',',$lines[5]);
$l[7] = explode(',',$lines[6]);
$l[8] = explode(',',$lines[7]);

//multiply each 
$result = array();
$values = array(1,2,3,4,5);
for($i=0;$i<count($l);$i++){
    for($j=0;$j<count($values);$j++){
    $result[$i+1][$j] = $l[$i+1][$j] * $values[$j];
    }
}
//calculate percentages

$p1 = (explode(',',$lines[0]));
$p2 = (explode(',',$lines[1]));
$p3 = (explode(',',$lines[2]));
$p4 = (explode(',',$lines[3]));
$p5 = (explode(',',$lines[4]));
$p6 = (explode(',',$lines[5]));
$p7 = (explode(',',$lines[6]));
$p8 = (explode(',',$lines[7]));

// calculate averages *****For some reason most recent frequency item updated is off by one, possibly bc not up to date yet???? figure out later!!!
$f1avg = round((array_sum($result[1])/($total-1)),2);
$f2avg = round((array_sum($result[2])/($total-1)),2);
$f3avg = round((array_sum($result[3])/($total-1)),2);
$f4avg = round((array_sum($result[4])/($total-1)),2);
$f5avg = round((array_sum($result[5])/($total-1)),2);
$f6avg = round((array_sum($result[6])/($total-1)),2);
$f7avg = round((array_sum($result[7])/($total-1)),2);
$f8avg = round((array_sum($result[8])/($total-1)),2);
//pie chart 1 information
$_SESSION['$p1f0s']= round((($p1[0]/$total)*360),2);
$_SESSION['$p1f1s']= round((($p1[1]/$total)*360),2);
$_SESSION['$p1f2s']= round((($p1[2]/$total)*360),2);
$_SESSION['$p1f3s']= round((($p1[3]/$total)*360),2);
$_SESSION['$p1f4s']= round((($p1[4]/$total)*360),2);

$p1f0 = $_SESSION['$p1f0s'];
$p1f1 = $_SESSION['$p1f1s'];
$p1f2 = $_SESSION['$p1f2s'];
$p1f3 = $_SESSION['$p1f3s'];
$p1f4 = $_SESSION['$p1f4s'];

echo $p1f2;
?>

所以我尝试将会话中的信息传递给这个:

$p1f0 = $_SESSION['$p1f0s'];
$p1f1 = $_SESSION['$p1f1s'];
$p1f2 = $_SESSION['$p1f2s'];
$p1f3 = $_SESSION['$p1f3s'];
$p1f4 = $_SESSION['$p1f4s'];

header("Content-type: image/png");

//create pie charts
$image=imagecreatetruecolor(101,51);
//_____________________colors____________________________
$my_colorA=imagecolorallocate($image,51,51,255);
$my_colorB=imagecolorallocate($image,100,150,215);
$my_colorC=imagecolorallocate($image,20,20,151);
$my_colorX=imagecolorallocate($image,216,216,255);


$red = imagecolorallocate($image, 255, 0, 0);
$orange = imagecolorallocate($image, 191, 64, 0);
$dark_yellow = imagecolorallocate($image, 128, 128, 0);
$dark_green = imagecolorallocate($image, 64, 191, 0);
$green = imagecolorallocate($image, 0, 255, 0);
//_______________________________________________________
$ptsize=24;$x=20;$y=50;$angle=-10;
imagefill($image,0,0,$my_colorX);

//__________________ , center,  w , h , st, end, clr  ,    type______
imagefilledarc($image,50,25, 100, 50,  0,  $p1f0, $red, IMG_ARC_PIE);
imagefilledarc($image,50,25, 100, 50, $p1f0,  $p1f01, $orange, IMG_ARC_PIE);
imagefilledarc($image,50,25, 100, 50, $p1f1, $p1f2, $dark_yellow , IMG_ARC_PIE);
imagefilledarc($image,50,25, 100, 50, $p1f2,  $p1f3, $dark_green, IMG_ARC_PIE);
imagefilledarc($image,50,25, 100, 50, $p1f3,  $p1f4, $green, IMG_ARC_PIE);
//___________________________________________________________________

imagepng($image);
imagedestroy($image)

但是没有成功。在搜索此内容时,我一直遇到 $base64_data ,我有点理解应该做什么,但不知道这是否偏离了基础。请帮忙!并提前感谢!

4

1 回答 1

0

虽然您可以像这样直接在 HTML 文件中“导出”图像:

// generate image here
ob_start(function($c) {return "<img src=\"data:image/png;base64,".base64_encode($c)."\" />";});
imagepng($image);
ob_end_flush();

这有点痛苦。<img src="myimage.php" />在那里拥有并生成它要容易得多。你甚至可以传递GET变量。

于 2012-12-14T03:05:35.857 回答