I’ve been trying to set up a way to take my below code segment 2 and turn it into a while loop. As it stands now Code segment 2 would have to be copyed and pasted 4 times to get the right effect. How can I increment a SQL query that a variable is pointed to?
My problem is trying to make this part be able to work in a loop. So the first time it uses query 9, then query 10, then query 11, and so on. So where the // WHILE LOOP HERE is there would be a loop that runs the whole thing 5 times but changes what to mob to update.
// 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
));
Code Segment 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";
Code Segment 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;
}
}
}
}
Use
and then generate your variables as dynamic or variable variables (http://docs.php.net/manual/en/language.variables.variable.php) like so
etc – or you could make your queries and other variables arrays
and just use