Page 1 of 1

PHP Grid

Posted: Mon Aug 15, 2011 11:51 am
by VZdemon
Could someone help me with this code for makeing a grid in php. thanks.

Code: Select all

<?php
	$rows = 3;
	$cols = 4;
	
	$black = false;
	$white = false;
	
	$width = 20;
	$height = 20;
	
	$text[5][5] = array(
					"tile","tile","tile","tile",
					"tile","tile","tile","tile",
					"tile","tile","tile","tile",
					"tile","tile","tile","tile"
					);
	echo $array[0][1];
	
	for($c=0;$c<$cols;$c++){
		$x=$c*40;
		if($black==false){
		echo "<spam style='
					left:$x;
					width:40;
					height:20;
					background-color:WHITE;
					border:1px solid GRAY; 
				'>Tile</spam>";
		$black = true;
		}else{
		echo "<spam style='
					left:$x;
					width:40;
					height:20;
					background-color:BLACK;
					color:WHITE;
					border:1px solid GRAY; 
				'>Tile</spam>";
		$black = false;	
		}
		for($t=0;$t<$rows;$t++){
			$y=$t*20;
			if($white==false){
			echo "<br/><spam style='
						top:$y;
						width:40;
						height:20;
						background-color:BlACK;
						color:WHITE;
						border:1px solid GRAY; 
					'>Tile</spam>";
			$white=true;
			}else{
			echo "<br/><spam style='
						top:$y;
						width:40;
						height:20;
						background-color:WHITE;
						color:BLACK;
						border:1px solid GRAY; 
					'>Tile</spam>";
			$white=false;
			}
		}
	}
	
	?>

Re: PHP Grid

Posted: Mon Aug 15, 2011 12:12 pm
by Xaleph
What is that code? It`s a mess currently. Try doing in small steps. You want something with colors, but it`s not working correct?

Anyway, you have an X axis and an Y axis. So rows == y and cols == x.

So in order for the grid to display, you have to go trough all Y, in each Y ( as long as Y <= $rows ) go trough each X as X <= $cols.

So :

Code: Select all

for($y = 0; $y <= $cols; $y ++ ){
      for($x = 0; $x <= $rows ; $x ++ )
      { 
           // print a block
      }
} 

Re: PHP Grid

Posted: Mon Aug 15, 2011 12:18 pm
by Xaleph

Code: Select all

<?php
error_reporting(E_ALL);
ini_set("display_errors", true);

       $rows = 3;
       $cols = 4;

       $black = false;
       $white = false;

       $width = 20;
       $height = 20;


       $text[5][5] = array(
                   "tile","tile","tile","tile",
                   "tile","tile","tile","tile",
                   "tile","tile","tile","tile",
                   "tile","tile","tile","tile"
                   );

       echo "<div style='width:300px; height: 500px;'>
             ";
       for($yt = 0; $yt <= $cols ; $yt++ )
       {
             echo '<div style="height: 60px; margin: 10px; background-color: red; width: 100%;">
                   ';
            for($xt = 0 ; $xt <= $rows ; $xt++ )
            {
                  echo '
                        <div style="float: left; font-size: 11px; color: white; width: 50px; height: 40px; margin: 10px; background: green;">
                              Y = '.$yt.' <br/> X = '.$xt.'
                        </div>
                  ';
            }
            echo "
                  </div>";
       }

       echo "
             </div>";

?>


Re: PHP Grid

Posted: Thu Sep 15, 2011 7:56 pm
by VZdemon
thanks. :)

Re: PHP Grid

Posted: Thu Sep 15, 2011 9:19 pm
by Jackolantern
And just for the record, it is span, not spam ;)