Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/
Share on other sites

<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>

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1036993
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037003
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037013
Share on other sites

<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);

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037023
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037279
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037304
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037343
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037431
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1037434
Share on other sites

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>

 

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1038065
Share on other sites

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.)

Link to comment
https://forums.phpfreaks.com/topic/197592-help-with-grid/#findComment-1039206
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.