tennis_mnky Posted April 5, 2010 Share Posted April 5, 2010 I wrote the following code to create a grid with 0s and 1s. When i run the code i get nothing. I'm new to programming and could anybody help me with this problem. Also is there is anyway to make a grid with with squares black and white instead of 1s and 0s. <html> <body> <h1>Make a grid</h1> <?php makegrid(); gridtable($grid); $height = 10; $width = 10; //creates random grid function makegrid(){ global $height, $width; for($i = 0; $i < $width; $i++) { for($n = 0; $n < $height; $n++) { $if = rand(0,1); //if is yes or no $grid[$i][$n] = $if;}}} //end loop, end loop, end funcition function gridtable($grid){ //turns the array to a table global $height, $width; $puzzle = ""; $puzzle .= "<table border = 0>\n"; for ($row = 0; $row < $height; $row++){ $puzzle .= "</tr>\n"; for ($col = 0; $col < $width; $col++){ $puzzle .= " <td width = 10>{$grid[$row][$col]}</td>\n"; } //end row loop $puzzle .= "</tr>\n"; } //end col loop $puzzle .= "</table>\n"; return $puzzle"; } //end grid table print "$puzzle" ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted April 5, 2010 Share Posted April 5, 2010 <html> <body> <h1>Make a grid</h1> <?php makegrid(); gridtable($grid); $height = 10; $width = 10; //creates random grid function makegrid(){ global $height, $width; for($i = 0; $i < $width; $i++) { for($n = 0; $n < $height; $n++) { $if = rand(0,1); //if is yes or no $grid[$i][$n] = $if;}}} //end loop, end loop, end funcition function gridtable($grid){ //turns the array to a table global $height, $width; $puzzle = ""; $puzzle .= "<table border = 0>\n"; for ($row = 0; $row < $height; $row++){ $puzzle .= "</tr>\n"; for ($col = 0; $col < $width; $col++){ $puzzle .= " <td width = 10>{$grid[$row][$col]}</td>\n"; } //end row loop $puzzle .= "</tr>\n"; } //end col loop $puzzle .= "</table>\n"; return $puzzle"; } //end grid table print "$puzzle" ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 I made some modifications to that code to help in readability and to make more efficient. You shouold really avoid GLOBAL if at all possible. And this is not one where it is needed - at all <?php function makegrid($rows, $cols) { $grid = array(); for($row=0; $row<$rows; $row++) { for($col=0; $col<$cols; $col++) { $grid[$row][$col] = rand(0,1); } } return $grid; } function gridtable($grid) { $tableHTML = "<table border=\"0\">\n"; foreach($grid as $row) { $tableHTML .= "<tr>\n"; foreach($row as $col) { $tableHTML .= " <td width=\"10\">{$col}</td>\n"; } $tableHTML .= "</tr>\n"; } $tableHTML .= "</table>\n"; return $tableHTML; } $gridArray = makegrid(10, 10); $puzzle = gridtable($gridArray); ?> <html> <body> <h1>Make a grid</h1> <?php echo $puzzle; ?> </body> </html> Although, unless you need to use the grid array for other purposes, I would suggest combining the funtions into one. function makegrid($rows, $cols) { $tableHTML = "<table border=\"0\">\n"; for($row=0; $row<$rows; $row++) { $tableHTML .= "<tr>\n"; for($col=0; $col<$cols; $col++) { $cellContent = rand(0,1); $tableHTML .= " <td width=\"10\">{$cellContent}</td>\n"; } $tableHTML .= "</tr>\n"; } $tableHTML .= "</table>\n"; return $tableHTML; } Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 5, 2010 Author Share Posted April 5, 2010 Thanks mjdamato, your code helped me. I'm 13 and started programing about 3 weeks ago Is there a way to display this as a grid instead on 0s and 1s. Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted April 5, 2010 Share Posted April 5, 2010 Argh.. copied the wrong thing. <html> <body> <h1>Make a grid</h1> <?php $height = 10; $width = 10; $grid=makegrid($height,$width); $puzzle=gridtable($grid); //creates random grid function makegrid($height,$width){ //global $height, $width; for($i = 0; $i < $width; $i++) { for($n = 0; $n < $height; $n++) { $if = rand(0,1); //if is yes or no $grid[$i][$n] = $if;}} return $grid; } //end loop, end loop, end funcition function gridtable($grid){ //turns the array to a table global $height, $width; $puzzle = ""; $puzzle .= "<table border = 0>\n"; for ($row = 0; $row < $height; $row++){ $puzzle .= "</tr>\n"; for ($col = 0; $col < $width; $col++){ $puzzle .= " <td width = 10>{$grid[$row][$col]}</td>\n"; } //end row loop $puzzle .= "</tr>\n"; } //end col loop $puzzle .= "</table>\n"; return $puzzle; } //end grid table print $puzzle; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted April 5, 2010 Share Posted April 5, 2010 <h1>Make a grid</h1> <?php $height = 10; $width = 10; $grid=makegrid($height,$width); $puzzle=gridtable($grid); $checker=checkergridtable($grid); //creates random grid function makegrid($height,$width){ //global $height, $width; for($i = 0; $i < $width; $i++) { for($n = 0; $n < $height; $n++) { $if = rand(0,1); //if is yes or no $grid[$i][$n] = $if;}} return $grid; } //end loop, end loop, end funcition function gridtable($grid){ //turns the array to a table global $height, $width; $puzzle = ""; $puzzle .= "<table border = 0>\n"; for ($row = 0; $row < $height; $row++){ $puzzle .= "</tr>\n"; for ($col = 0; $col < $width; $col++){ $puzzle .= " <td width = 10>{$grid[$row][$col]}</td>\n"; } //end row loop $puzzle .= "</tr>\n"; } //end col loop $puzzle .= "</table>\n"; return $puzzle; } //end grid table function checkergridtable($grid){ //turns the array to a table global $height, $width; $puzzle = ""; $puzzle .= "<table border = 0>\n"; for ($row = 0; $row < $height; $row++){ $puzzle .= "</tr>\n"; for ($col = 0; $col < $width; $col++){ $color= $grid[$row][$col] ? "FFFFFF" : "000000"; $puzzle .= " <td width = 10 "; $puzzle .= "bgcolor=".$color; $puzzle .= "> </td>\n"; } //end row loop $puzzle .= "</tr>\n"; } //end col loop $puzzle .= "</table>\n"; return $puzzle; } //end grid table print $puzzle; print $checker; ?> </body> </html> The trick is setting the bgcolor element of the td. what I used here was known as ternary assignment. A couple of apps with this assignment and you will be the envy of your pre-teen peers Oh yeah, change the global references to: $height=count($grid[0]); $width=count($grid); Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted April 5, 2010 Share Posted April 5, 2010 Err... $height=count($grid); $width=count($grid[0]); Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 5, 2010 Author Share Posted April 5, 2010 Thanks andrewguager, the second grid is just like i wanted it. Also, is there a way to make each table cell even smaller. I can get the width smaller but the height is still always big. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 There's some inefficency and invalid HTML in the code above Also, as stated before, you should not be using GLOBAL. Instead, pass the height/width in the function call. The reason you cannot make the cell height smaller is that the code is using a single space character. Since that character has a size, the cell will only shrink to the size of the content of the cell. You can either change the font-size or use a transparent image. In the code below I gave the font a size of 1 pt and set the size of the cells within the function. <?php function checkergridtable($tableHeight, $tableWidth) { //Height and width of the cells $cellHeight = '5pt;'; $cellWidth = '5pt;'; $cellStyle = "height:{$cellHeight};width:{$cellWidth};"; $tableHTML = "<table border=\"0\" style=\"font-size:1pt;\">\n"; for ($row=0; $row<$tableHeight; $row++) { $tableHTML .= "<tr>\n"; for ($col=0; $col<$tableWidth; $col++) { $cellColor= (rand(0,1)==1) ? "FFFFFF" : "000000"; $tableHTML .= " <td width=\"10\" style=\"{$cellStyle}background-color:{$cellColor};\"> </td>\n"; } $tableHTML .= "</tr>\n"; } $tableHTML .= "</table>\n"; return $tableHTML; } ?> <html> <head> <h1>Make a grid</h1> <?php echo checkergridtable(10, 10); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 5, 2010 Author Share Posted April 5, 2010 Thanks, you guys both helped a bunch. Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 5, 2010 Author Share Posted April 5, 2010 one more thing, I was going to make a version of conway's game of life. For that i would need to know whether a cell is on or off. I tried changing the code to make an array with 0 or 1 for each cell. But it doesn't work. Here is the code i used <?php function checkergridtable($tableHeight, $tableWidth) { //Height and width of the cells $cellHeight = '1pt;'; $cellWidth = '1pt;'; $cellStyle = "height:{$cellHeight};width:{$cellWidth};"; $tableHTML = "<table border=\"0\" style=\"font-size:1pt\";>\n"; for ($row=0; $row<$tableHeight; $row++) { $tableHTML .= "<tr>\n"; for ($col=0; $col<$tableWidth; $col++) { $grid[$row][$col] = "rand(0,1)"; if ($grid[$row][$col] = 1){ $cellColor = "FFFFFF" ; } else { $cellColor = "000000";} $tableHTML .= " <td width=\"10\" style=\"{$cellStyle}background-color:{$cellColor};\"> </td>\n"; } $tableHTML .= "</tr>\n"; } $tableHTML .= "</table>\n"; return $tableHTML; } ?> <html> <head> <h1>Make a grid</h1> <?php echo checkergridtable(100, 100); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 I assume you will need the grid saved to seesion, or something similar, so you can access it on subsequent page loads function checkergridtable($tableHeight, $tableWidth) { //Height and width of the cells $cellHeight = '5pt;'; $cellWidth = '5pt;'; $grid = array(); $cellStyle = "height:{$cellHeight};width:{$cellWidth};"; $tableHTML = "<table border=\"0\" style=\"font-size:1pt;\">\n"; for ($row=0; $row<$tableHeight; $row++) { $tableHTML .= "<tr>\n"; for ($col=0; $col<$tableWidth; $col++) { $grid[$row][$col] = rand(0,1); $cellColor= ($grid[$row][$col]==1) ? "FFFFFF" : "000000"; $tableHTML .= " <td width=\"10\" style=\"{$cellStyle}background-color:{$cellColor};\"> </td>\n"; } $tableHTML .= "</tr>\n"; } $tableHTML .= "</table>\n"; //Save the grid to session $_SESSION['grid'] = $grid; //Return the table content return $tableHTML; } Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 5, 2010 Author Share Posted April 5, 2010 When i tried the knew code i got unexpected $end. Ive gotten this error before and didn't know how to fix it. What does it mean and how do you fix it. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 That's typicaly a problem with a missing closing paren ), bracket ], semi-colon, etc. I copied the last function into the code and it worked fine. Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 5, 2010 Author Share Posted April 5, 2010 thanks, i think i had a problem when copying and pasting. I tried to see if i could access the information in the grid with echo $grid[1][5]; but nothing happened. How could i see what is in the grid array. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted April 5, 2010 Share Posted April 5, 2010 thanks, i think i had a problem when copying and pasting. I tried to see if i could access the information in the grid with echo $grid[1][5]; but nothing happened. How could i see what is in the grid array. //After the code print '<pre>' . print_r($grid, 1) . '</pre>'; That should give you a clean heads up of what is inside the array. Does grid[1][5] Exist in the outputted content? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 In the code I provided, the function is storing the grid data in a session value. If you modified the code to store in a local variable then you need to ensure the variable is created in the proper scope. Again, II assume you would want to use the grid in a subsequent page load if you are doing some type of game, so a session variable would make sense. Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 6, 2010 Author Share Posted April 6, 2010 Thanks everybody. I finally got it to work. I combined much of the code you guys gave me. And the best part is i understand it, which means i can use it. Here is the code i'm using. h1>Make a grid</h1> <?php $height = 10; $width = 10; $grid=makegrid($height,$width); $checker=checkergridtable($grid,$height,$width); function makegrid($height,$width){ for($i = 0; $i < $width; $i++) { for($n = 0; $n < $height; $n++) { $if = rand(0,1); //if is yes or no $grid[$i][$n] = $if;}} return $grid; } //end loop, end loop, end funcition function checkergridtable($grid,$tableHeight, $tableWidth) { //Height and width of the cells $cellHeight = '5pt;'; $cellWidth = '5pt;'; $cellStyle = "height:{$cellHeight};width:{$cellWidth};"; $tableHTML = "<table border=\"0\" style=\"font-size:1pt;\">\n"; for ($row=0; $row<$tableHeight; $row++) { $tableHTML .= "<tr>\n"; for ($col=0; $col<$tableWidth; $col++) { $cellColor= ($grid[$row][$col]==1) ? "000000" : "FFFFF"; $tableHTML .= " <td width=\"10\" style=\"{$cellStyle}background-color:{$cellColor};\"> </td>\n"; } $tableHTML .= "</tr>\n"; } $tableHTML .= "</table>\n"; return $tableHTML; } print $checker; print '<pre>' . print_r($grid, 1) . '</pre>'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
tennis_mnky Posted April 7, 2010 Author Share Posted April 7, 2010 One more thing that is off subject. One of my dads friends recommended eclipse and i downloaded it. But i have just been using a plain text pad. How would using eclipse help me. Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted April 8, 2010 Share Posted April 8, 2010 I use eclipse, and it does help when I have projects that span multiple files. So I have one file that is my class declarations. Now I am trying to use a function that is defined in that file, but I don't want to open the file and look for the function. Eclipse "knows" what is in the files related to the project can tell me what type of input the function wants. I would advise that you get familiar with a good IDE of your choosing (Eclipse works for me, but everyone has their reasons for preference.) Quote Link to comment 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.