Jump to content

loop though DB untill row value = 0?


joecooper

Recommended Posts

I have a table of 1024 rows. Basically i need to select a random cell where the $row['clicked'] = 0.

 

If its more than 0, then try again.

 

I cant think of to use GOTO's or while loop or for...

 

$randcell = rand(1,1024);

$sql="SELECT * FROM grid WHERE `ID` = $randcell";
$result=mysql_query($sql);
$row = mysql_fetch_array( $result );

if($row['boat'] > 0){  //does the cell already contain a boat?

}

 

 

Link to comment
https://forums.phpfreaks.com/topic/246497-loop-though-db-untill-row-value-0/
Share on other sites

while($gotone <1){
              $randcell = rand(1,1024);

              $sql="SELECT * FROM grid WHERE `ID` = $randcell";
              $result=mysql_query($sql);
              $row = mysql_fetch_array( $result );

              // 0 = no boat
              // 1 = boat start
              // 2 = boat middle
              // 3 = boat end

              if($row['boat'] = 0){  //no boat in this cell?

                   $gotone = 1;
                   $startcell = $randcell

              }
}

 

i think this will work :)

I didn't get your question completely but I guess you should add the where clause in sql statement for clicked=0 or boat=0. Also you can't put anything in ID randomly.. because ID 1000 may not exist (i.e. if deleted or too large).

$sql="SELECT * FROM grid";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$randcell = rand(0,$count-1);

$sql="SELECT * FROM grid WHERE ID='$randcell' && clicked='0'";
$result=mysql_query($sql);
$row = mysql_fetch_array( $result );

 

Hope this helps.

My code sort of works...

 

right,

 

the game im creating is a battleship type game, and im scripting the part where the server resets the grid, and places the boats, so finds a random cell (1 - 1024), and checks if that cell already has a boat there, if not, then go 1 down, or 1 across and check that has a boat etc...

 

once it finds like 5 empty squares, sticks a boat there, then goes onto the next... the code i have so far is below, but some reason not echoing the "if ($row['boat'] == 0){" part at all, not even the else:

 

http://interbitlotto.com/grid/index.php

 

echo("Starting boat 1 placement<br/>");
while($foundone < 1){
              $randcell = rand(1,1024);

              $sql="SELECT * FROM grid WHERE `ID` = '$randcell'";
              $result=mysql_query($sql);
              $row = mysql_fetch_array( $result );

              // 0 = no boat
              // 1 = boat start
              // 2 = boat middle
              // 3 = boat end


                   $startcell = $randcell;

                   $orientation = rand(1,2); //get random orientation  - 1 = down, 2 = across.
                   if ($orientation == 1){
                      $multiply = 32;  //cells till next row
                   }else{
                      $multiply = 1;  //across, so next cell
                   }
                    //if the orientation is down
                     for($i=0; $i==4; $i++){ //loop for each cell down
                         $nextcell = $startcell + ($multiply * $i);  //calculate the next cell down (jumps 32 cells per boat section)
                         $sql="SELECT * FROM grid WHERE `ID` = '$nextcell'";
                         $result=mysql_query($sql);
                         $row = mysql_fetch_array( $result );
                         if ($row['boat'] == 0){  //no boat here! check the next one!
                            //dont think any code needs to go here... probs to add the cell to a list of cells to change to the boat status in the db..
                            echo("no boat<br/>");
                         }else{ //BOAT! ahh! start again! not worth it! 

                            echo("BOAT!<br/>");
                         }

                     }





$foundone = 1;

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.