2011-12-17 69 views
0

我有一个具有以下结构的数据库。从数据库获取颜色多边形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); 
?> 

感谢

+0

它看起来像你从数据库中获取每个颜色单独。一次收集所有12个人不是更有意义吗?此外,'imagefilledpolygon($ image,$ values,6,$ red'缺少关闭''''。 – Herbert 2011-12-17 23:01:22

回答

0

数学应有助于一点点!如果我们计算一个给定的数字应该出现在哪一列和哪一行,那么其余的应该相当简单。这两条线应该有所帮助:

$col = ($i-1)%3; 
$row = floor(($i-1)/3); 

并把它们放在背景下,我认为像下面应该工作:

while ($db_field = mysql_fetch_assoc($result)) { 
    switch($db_field['Result']) { 
     case 1: $colour = $blue; break; 
     case 2: $colour = $red; break; 
     case 3: $colour = $green; break; 
    } 
    $i = $db_field['index']; 
    $col = ($i-1)%3; 
    $row = floor(($i-1)/3); 
    imagefilledpolygon($image, array(
     $row*10, $col*10, 
     $row*10, $col*10+10, 
     $row*10+10, $col*10+10, 
     $row*10+10, $col*10 
    ),4,$colour); 
} 
+0

$ col =($ i-1)%3应该是一些其他变量名称,因为$ col是颜色 – Ank 2011-12-19 19:15:36