Vẽ biểu đồ bằng PHP từ mysql 2007-08-11 02:17:48
Yêu cầu server: co hổ trợ thư viện GD
1. MySQL:
Tạo databse: php_chart
Tạo table: basic_chart
2. Code PHP:
file config.php
<?
$servername='localhost';
$dbusername='root';
$dbpassword='123';
$dbname='php_chart';
$link = mysql_connect($servername,$dbusername,$dbpassword) or die("Could not connect to MySQL");
mysql_select_db($dbname) or die ("could not open db".mysql_error());
?>
file index.php
<?php
include("config.php");
$qt=mysql_query("select * from basic_chart");
header ("Content-type: image/jpg");
$x_gap=40;
$x_max=$x_gap*13;
$y_max=250;
$im = @imagecreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, 234, 234, 234);
$text_color = imagecolorallocate ($im, 233, 14, 91);
$graph_color = imagecolorallocate ($im,25,25,25);
$x1=0;
$y1=0;
$first_one="yes";
while($nt=mysql_fetch_array($qt)){
$x2=$x1+$x_gap;
$y2=$y_max-$nt[sales];
imagestring($im,2,$x2,$y2,$nt[month],$graph_color);
if($first_one=="no"){
imageline ($im,$x1, $y1,$x2,$y2,$text_color);
}
$x1=$x2;
$y1=$y2;
$first_one="no";
}
imagejpeg($im);
?>
Tra loi 6 comment(s) 2007-08-11 02:17:48
tranlongpc 2008-04-24 08:43:27
Tra loi
TG 2008-04-24 06:38:37
Đây là phần sql của phần trên:
`month` char(3) NOT NULL default '',
`sales` int(3) NOT NULL default '0'
);
INSERT INTO `php_chart` VALUES ('Jan', 120);
INSERT INTO `php_chart` VALUES ('Feb', 100);
INSERT INTO `php_chart` VALUES ('Mar', 15);
INSERT INTO `php_chart` VALUES ('Apr', 110);
INSERT INTO `php_chart` VALUES ('May', 100);
INSERT INTO `php_chart` VALUES ('Jun', 115);
INSERT INTO `php_chart` VALUES ('Jul', 198);
INSERT INTO `php_chart` VALUES ('Aug', 120);
INSERT INTO `php_chart` VALUES ('Sep', 130);
INSERT INTO `gd_graph` VALUES ('Oct', 110);
INSERT INTO `gd_graph` VALUES ('Nov', 145);
INSERT INTO `gd_graph` VALUES ('Dec', 150);
Tra loi
NguyenVanNgoc 2008-12-11 02:59:24
Mình ko hiểu hàm này lấy kiểu gì
Tra loi
changtraingheo 2008-12-11 03:37:24
Tra loi
he`he` 2008-12-12 09:31:28
INSERT INTO `gd_graph` VALUES ('Oct', 110);
Ông TG chịu khó thật đấy. Chắc php_Manual ông phải đọc và học hết rồi ^_^
Tra loi
TK 2008-12-12 07:25:57
Tra loi
Y kien