我有一个具有以下结构的数据库。从数据库获取颜色多边形PHP
Index Output
1 1
2 2
3 1
4 1
5 3
6 1
7 2
8 1
9 3
10 2
11 2
12 3
我期待创造正方形的3X4矩阵(有点像一个井字游戏,并根据输出画他们)。所以如果输出是1,我会画红色方块,如果输出是2,我会画这个方块蓝色,如果输出是3,我会画那个方块绿色。正方形还挺像这样(除非我正在使用的图形)
-- -- --
| | | |
-- -- --
| | | |
-- -- --
| | | |
-- -- --
| | | |
-- -- --
我明白,我可以使用多边形函数来绘制和填充形状,但我写的12个数组定义12组不同的四个坐标或有现在简单的方法
我做了以下
<?php
$user_name = "root";
$password = "pass";
$database = "db";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
$blue = imagecolorallocate($image, 0, 0, 255);
$red = imagecolorallocate($image, 255, 0, 0);
$green = imagecolorallocate($image, 0, 255, 0);
$image = imagecreatetruecolor(400, 300);
$col_poly = imagecolorallocate($image, 255, 255, 255);
if ($db_found) {
for ($i = 0 ; $i<=12 ; $i=$i+1) {
$SQL = "SELECT * FROM table1 where index like " . $i
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
if ($db_field['Result'] == 1) { // and similarly for blue and green
imagepolygon($image, array(
0, 0,
0, 10,
10, 10,
10, 0
),
4,
$col_poly);
imagefilledpolygon($image, $values, 6, $red
}
// and so on for others
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
感谢
它看起来像你从数据库中获取每个颜色单独。一次收集所有12个人不是更有意义吗?此外,'imagefilledpolygon($ image,$ values,6,$ red'缺少关闭''''。 – Herbert 2011-12-17 23:01:22