统计图表/模仿PHP和WebMysql 21章例子

                                          统计图表/模仿PHP和WebMysql 21章例子

//rorger

//2010


 

在mysql中创建数据库:

create database language ;

use language ;

create table language_results

(

   language_name varchar(30),

   language_nums int ;

);

 

 

插入数据:

insert into language_results values("Java",0);

insert into language_results values("C#",0);

insert into language_results values("C++",0);

制作统计界面:

Which language do you like better?

Java
C#
C++

<html> <head> <title> </title> </head> <body> <h3>Which language do you like better?</h3> <form method="post" action="show_my_poll.php"> <input type="radio" name="vote" value="Java" />Java<br /> <input type="radio" name="vote" value="C#" />C#<br /> <input type="radio" name="vote" value="C++" />C++<br /><br /> <input type="submit" value="submit" /> </form> </body> </html>

 

show_my_poll.php的代码如下:

思路:取得总数,然后计算百分比

注意:php中使用round和intval进行相关数据转换


<?php header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); header("Cache-Control: no-cache, must-revalidate" ); $vote = $_REQUEST['vote']; $link = mysql_connect('127.0.0.1','root','123','lanugage'); if(!$link){ echo "<h3>Sorry, Cannot connect to database language</h3><br />"; exit; } @mysql_select_db('language'); $vote = mysql_escape_string($vote); $update_query="update language_results set language_nums=language_nums+1 where language_name='$vote'"; $update_result = mysql_query($update_query,$link) ; if(!$update_result) { echo "Vote Error:".mysql_error($update_result)."<br />"; echo "<h3>Note! Your voted failed"."<br />"; } $query="select * from language_results"; $result = mysql_query($query,$link); if(!$result){ $message = 'Invalid query: '.mysql_error()."<br />"; $message = "whole query: ".$query."<br />"; die($message); } $num_rows = mysql_num_rows($result); $total_num=0; $java_rate=0; $csharp_rate=0; $cplusplus_rate = 0 ; $java_rate_percent = 0.0; $csharp_rate_percent = 0.0; $cplusplus_rate_percent = 0.0; while($row = mysql_fetch_assoc($result)){ if($row['language_name'] == 'Java') $java_rate=$row['language_nums'] ; else if ($row['language_name'] == 'C#') $csharp_rate=$row['language_nums'] ; else if ($row['language_name'] == 'C++') $cplusplus_rate=$row['language_nums'] ; $total_num+=intval($row['language_nums']); } $java_rate_percent = intval(round($java_rate *100 / $total_num)) ; $csharp_rate_percent = intval(round($csharp_rate *100 / $total_num)) ; $cplusplus_rate_percent = intval(round($cplusplus_rate *100 / $total_num)) ; echo "<h3>The resuls are as follows(In format-text):</h3>"; echo "java_rate:".$java_rate."/".$total_num."<br />" ; echo "csharp_rate:".$csharp_rate."/".$total_num."<br />" ; echo "cplusplus_rate:".$cplusplus_rate."/".$total_num."<br />" ; echo "java_rate_perent:".$java_rate_percent."<br />"; echo "csharp_rate_percent:".$csharp_rate_percent."<br />"; echo "cplusplus_rate_percent:".$cplusplus_rate_percent."<br />"; //now draw the image $image_file="result.png"; $image_width = 700 ; $image_height = 340 ; $image = imagecreatetruecolor($image_width,$image_height) ; $white=imagecolorallocate($image,255,255,255) ; $black = imagecolorallocate($image,0,0,0); $bar_color = imagecolorallocate($image,65,72,204) ; $rate_color=imagecolorallocate($image,136,0,21); $title = "Language Results" ; $font_file = "C://windows//fonts//arial.ttf"; $title_box = imagettfbbox(20,0,$font_file,$title) ; $title_x = $title_box[0] ; $title_y = $title_box[1] ; $title_width = $title_box[2]-$title_box[0] ; $title_height = $title_box[7]-$title_box[1] ; $title_height=abs($title_height); echo "height:"; echo "$title_height"."<br />"; imagefill($image,0,0,$white); imagerectangle($image,2,2,698,338,$black) ; imagettftext($image,20,0, ($image_width - $title_width)/2, $title_height+abs($title_y), $black,$font_file,$title) ; $offset = $title_height+abs($title_y) +50 ; $unit = ($image_height - $title_height - abs($title_y ))/$num_rows; mysql_data_seek($result,0); $i=0; while($row=mysql_fetch_assoc($result)) { $y=$offset+$unit*$i + 5; imagettftext($image,15,0,40,$y,$black,$font_file,$row['language_name']); $i++; $x1=150 ; $y1=$y-30; $x2 = $image_width-100; $y2 = $y+$unit-80 ; $percent=intval(round($row['language_nums']/$total_num *100)); imagettftext($image,12,0,$x2+40,$y,$black,$font_file,sprintf("%d%%",$percent)); imagerectangle($image,$x1-2,$y1-2,$x2+2,$y2+2,$black) ; $x2=intval(round(($x2-$x1)*($percent /100)))+ $x1; imagefilledrectangle($image,$x1,$y1,$x2,$y2,$bar_color); imagettftext($image,12,0,($x2==$image_width-100)?$x2-40:$x2+20,$y,$rate_color,$font_file,sprintf("%d/%d",intval($row['language_nums']),$total_num)); } imagepng($image,$image_file); imagedestroy($image); echo "<h3>The result shows as follows(In format-graphic):</h3>"; echo "<img src="$image_file" mce_src="$image_file" />"; mysql_free_result($result); mysql_close($link); ?>

 

结果将以两种形式输出:

一种是文本形式;

一种是图形形式;

 

我想需要注意的是:

图形输出格式应该是灵活的,自己可以进行微调;

没有必要把每个像素进行计算的很精确,但是有一点很重要,

那就是对齐;至少要对齐吧;

 

 

需要补充的是:

 

$vote = mysql_escape_string($vote);

 

这里必须对mysql语句进行特殊处理,防止注入攻击!

 

 

你可能感兴趣的:(java,mysql,PHP,image,File,query)