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
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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.