2011-12-17 20 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 

私は(ちょっとチックタックつま先のゲームのように、出力に応じてそれらをペイント)の正方形の3×4行列を作成するために探しています。出力が1の場合は正方形の赤を塗ります。出力が2の場合は正方形の青を塗ります。出力が3の場合は正方形の緑を塗ります。四角はちょっと私が描くと図形を埋めるためにポリゴン機能を使用できることを理解

-- -- -- 
| | | | 
-- -- -- 
| | | | 
-- -- -- 
| | | | 
-- -- -- 
| | | | 
-- -- -- 

(私はグラフィックスを使用してい除く)はこのように見えるが、私は4つの座標の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

それはあなたが個別DBから各色を得るように見えます。一度に全部12個を集めるのは理にかなっていませんか?また、 'imagefilledpolygon($ image、$ values、6、$ red'は閉じていません') ' – Herbert

答えて

0

数学の少しは役立つはず!与えられた数字がどの列と行に現れるかを計算すると、残りはかなりシンプルになるはずです。これらの2行は助けるべきである:

$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

関連する問題