joecooper Posted September 5, 2011 Share Posted September 5, 2011 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 More sharing options...
joecooper Posted September 5, 2011 Author Share Posted September 5, 2011 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 https://forums.phpfreaks.com/topic/246497-loop-though-db-untill-row-value-0/#findComment-1265743 Share on other sites More sharing options...
voip03 Posted September 5, 2011 Share Posted September 5, 2011 i think this will work is it working? Link to comment https://forums.phpfreaks.com/topic/246497-loop-though-db-untill-row-value-0/#findComment-1265755 Share on other sites More sharing options...
sohaibshaheen Posted September 5, 2011 Share Posted September 5, 2011 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 https://forums.phpfreaks.com/topic/246497-loop-though-db-untill-row-value-0/#findComment-1265756 Share on other sites More sharing options...
joecooper Posted September 5, 2011 Author Share Posted September 5, 2011 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 https://forums.phpfreaks.com/topic/246497-loop-though-db-untill-row-value-0/#findComment-1265758 Share on other sites More sharing options...
voip03 Posted September 6, 2011 Share Posted September 6, 2011 Try to echo $row['boat'] in different place, so you will know the value. $row['boat'] < 1 Link to comment https://forums.phpfreaks.com/topic/246497-loop-though-db-untill-row-value-0/#findComment-1265832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.