Jump to content

[SOLVED] php help


shergold

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

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

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.