shergold Posted August 25, 2009 Share Posted August 25, 2009 Hey guys, basicly im trying to make a tile based game, but the tiles arnt displaying as i want them to, there should be 10 rows of 10 but it is displaying with the first row in the table containing 11 tiles and the last containing 9. I have tried changing the values in the if loops that add <tr> and </tr> but just cant seem to get it. Thanks allot for any answers as i really appriciate any help that can be given. <?php //Test Array function draw_game() { for ($counter = 0; $counter <= 99; $counter++) { $tiles["$counter"] = "test"; } $count = 0; echo "<table frame=\"border\" width=\"500\" height=\"500\" cellpadding=\"0\" />\n"; //for each of the tiles execute the code foreach( $tiles as $id => $tile) { //add start of table row tag if count is equal to 10,20,30 if ($count == 0 || $count == 11 || $count == 21 || $count == 31 || $count == 41 || $count == 51 || $count == 61 || $count == 71 || $count == 81 || $count == 91) { echo "<tr>\n"; } echo "<td width=\"50\" height=\"50\" alight=\"left\" valign=\"bottom\" />\n"; //check what tile image to output, default is grass switch($id) { case 999: echo "<img src=\"$id.gif\" alt=\"$tile\"/>\n"; break; default: { echo "<img src=\"grass.gif\" alt=\"$tile\"/>\n"; echo $count; } } echo "</td>\n"; //add end of table row tag if count is equal to 10,20,30 if ($count == 10 || $count == 20 || $count == 30 || $count == 40 || $count == 50 || $count == 60 || $count == 70 || $count == 80 || $count == 90 || $count == 100) { echo "</tr>\n"; } ++$count; } echo "</table>\n"; } ?> thanks, shergold. Quote Link to comment https://forums.phpfreaks.com/topic/171856-solved-php-help/ Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 instead of if ($count == 0 || $count == 11 || $count == 21 || $count == 31 || $count == 41 || $count == 51 || $count == 61 || $count == 71 || $count == 81 || $count == 91) just do if (($count % 10) == 0) It should work much better. Also, the first has 11 because you check if the count is 11 (which will result in 11 columns), than check in increments of 10 Quote Link to comment https://forums.phpfreaks.com/topic/171856-solved-php-help/#findComment-906165 Share on other sites More sharing options...
shergold Posted August 25, 2009 Author Share Posted August 25, 2009 no, if (($count % 10) == 0) doesnt work. The if statements are to put a <tr> to start a new row after every 10 tiles have been places in the table. So what im trying to do is at the start put a <tr> for the first row then put a </tr> when 10 have been placed then put <tr> for the next row and so on. Quote Link to comment https://forums.phpfreaks.com/topic/171856-solved-php-help/#findComment-906169 Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 function drawGame($rows = 10, $cols = 10, $tileWidth = 50, $tileHeight = 50) { $gameWidth = $tileWidth * $cols; $gameHeight = $tileHeight * $rows; $game = "<table frame=\"border\" width=\"$gameWidth\" height=\"$gameHeight\" cellpadding=\"0\" cellspacing=\"0\">"; for ($i = 0; $i < $rows; ++$i) { $game .= '<tr>'; for ($j = 0; $j < $cols; ++$j) { $game .= "<td width=\"$tileWidth\" height=\"$tileHeight\"><img src=\"tile.gif\" alt=\"tile\"></td>"; } $game .= '</tr>'; } print $game . '</table>'; } Quote Link to comment https://forums.phpfreaks.com/topic/171856-solved-php-help/#findComment-906175 Share on other sites More sharing options...
Cezar708 Posted August 25, 2009 Share Posted August 25, 2009 or: <?php //Test Array function draw_game() { for ($counter = 0; $counter < 100; $counter++) { $tiles[$counter] = "test"; } echo "<table border=\"1\" width=\"500\" height=\"500\" cellpadding=\"0\" />\n"; //for each of the tiles execute the code echo "<tr>\n"; foreach( $tiles as $id => $tile) { if(!($id % 10)) { echo "</tr>\n<tr>"; } echo "<td width=\"50\" height=\"50\" alight=\"left\" valign=\"bottom\" />\n"; // check by yourself what tile image to output, default is grass // you can use $id variable whatever you want echo "[$id]"; echo "</td>\n"; } echo "</tr>"; echo "</table>\n"; } Regards Cezar708 Quote Link to comment https://forums.phpfreaks.com/topic/171856-solved-php-help/#findComment-906177 Share on other sites More sharing options...
shergold Posted August 25, 2009 Author Share Posted August 25, 2009 Wow Ignace much simpler, i just fixed my code before you posted that but i think i will be using yours, thankyou very much! Also thankyou very much Cezar for your help. Shergold. Quote Link to comment https://forums.phpfreaks.com/topic/171856-solved-php-help/#findComment-906178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.