2012-03-08 8 views
0

私は以下のコードセグメント2を取得してwhileループにする方法を設定しようとしています。今のところ、コードセグメント2は、適切な効果を得るために4回コピーして貼り付けなければなりません。変数が指されているSQLクエリをどのようにインクリメントできますか?SQLクエリからwhileループを作成する

私の問題は、この部分をループで動かせるようにしようとしていることです。最初にクエリ9を使用した後、クエリ10、クエリ11などを使用します。ここで//どこにループがあるかは、5回全体を実行するループがありますが、更新するために何をmobに変更しますか?

  // Update the Mob Position 
     $statement9 = $db->prepare($query9); 
     $result9 = $statement9 -> execute(array(
     'mob_list_id' =>$id, 
     'mob_1_x' =>$mob_number_y[0], 
     'mob_1_y' =>$mob_number_y[0] - 32 
     )); 

コードセグメント1

$query9 = "UPDATE mob_1 
     SET mob_1_x = :mob_1_x, mob_1_y =:mob_1_y 
     WHERE mob_list_id = :mob_list_id"; 

$query10 = "UPDATE mob_1 
     SET mob_2_x = :mob_2_x, mob_2_y =:mob_2_y 
     WHERE mob_list_id = :mob_list_id"; 

$query11 = "UPDATE mob_1 
     SET mob_3_x = :mob_3_x, mob_3_y =:mob_3_y 
     WHERE mob_list_id = :mob_list_id"; 

$query12 = "UPDATE mob_1 
     SET mob_4_x = :mob_4_x, mob_4_y =:mob_4_y 
     WHERE mob_list_id = :mob_list_id"; 

$query13 = "UPDATE mob_1 
     SET mob_5_x = :mob_5_x, mob_5_y =:mob_5_y 
     WHERE mob_list_id = :mob_list_id"; 

コードセグメント2

$roll = 0; 

// WHILE LOOP HERE 
{ 
$roll = mt_rand(1,6); 
if ($roll == 1 || $roll == 5) 
{ 
$direction = mt_rand(1,4); 

if ($direction == 1) 
{ 
    if ($mob_number_y[0] > 126) 
    { 
     // Update the Mob Position 
     $statement9 = $db->prepare($query9); 
     $result9 = $statement9 -> execute(array(
     'mob_list_id' =>$id, 
     'mob_1_x' =>$mob_number_x[0], 
     'mob_1_y' =>$mob_number_y[0] - 32 
     )); 

     $mob_number_y[0]-= 32; 
    } 
} 

if ($direction == 2) 
{ 
    if ($mob_number_x[0] > 240) 
    { 
    // Update the Mob Position 
    $statement9 = $db->prepare($query9); 
    $result9 = $statement9 -> execute(array(
    'mob_list_id' =>$id, 
    'mob_1_x' =>$mob_number_x[0] - 32, 
    'mob_1_y' =>$mob_number_y[0] 
    )); 

    $mob_number_x[0]-= 32; 
    } 
} 

if ($direction == 3) 
{ 
    if ($mob_number_x[0] < 848) 
    { 
     // Update the Mob Position 
     $statement9 = $db->prepare($query9); 
     $result9 = $statement9 -> execute(array(
     'mob_list_id' =>$id, 
     'mob_1_x' =>$mob_number_x[0] + 32, 
     'mob_1_y' =>$mob_number_y[0] 
     )); 

     $mob_number_x[0]+= 32; 
    } 
} 

if ($direction == 4) 
{ 
    if ($mob_number_y[0] < 734) 
    { 
     // Update the Mob Position 
     $statement9 = $db->prepare($query9); 
     $result9 = $statement9 -> execute(array(
     'mob_list_id' =>$id, 
     'mob_1_x' =>$mob_number_x[0], 
     'mob_1_y' =>$mob_number_y[0] + 32 
     )); 

     $mob_number_y[0]+= 32; 
    } 
} 
} 
} 
+1

いくつかの正規化を使用することができます。ええ、私は知っている、これは例、または何かです。しかし、それぞれのクエリが 'update mob_pos set mob_x =:mob_x、mob_y =:mob_y where mob_id =:mob_id'などのように単純なものになることができれば、これらの状況で使用できるクエリは1つだけです。 – cHao

+0

これはこの部分のための私の正確なコードです。 –

+0

FYI - あなたのクエリ '$ query9'、' $ query10'などを命名し、ステートメントと結果と同じように、今後も必然的に大きな混乱を招くでしょう。 $ updateMob1Query'、 '$ updateMob1Stmt'、' $ updateMob1Result'などのように、意味のある名前を変数に代入する必要があります。 –

答えて

0

使用

$direction = mt_rand(9,13); 

、その後、そのような動的または変数変数(http://docs.php.net/manual/en/language.variables.variable.php)として、あなたの変数を生成

$statement{$direction} = $db->prepare($query{$direction}); 

など - またはあなたのクエリや他の変数アレイ

$query[9] = "UPDATE mob_1 
    SET mob_1_x = :mob_1_x, mob_1_y =:mob_1_y 
    WHERE mob_list_id = :mob_list_id"; 

を作り、ちょうどこの*ロット*がより簡単になるだろう

$direction = mt_rand(9,13); 
$statement[$direction] = $db->prepare($query[$direction]); 
+0

私の意図したものではありません。私の質問をやり直す –

関連する問題