This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

sudoku checker in html/php

Started by Exerro, 06 May 2013 - 07:13 AM
Exerro #1
Posted 06 May 2013 - 09:13 AM
Hi,

I am trying to get into php and html a bit more so I decided to make a sudoku checker.
I know how to do the php code and I have already sorted the html tables out ( http://sudoku-checke...om/default.html )
Spoiler
<!DOCTYPE html>
<html>
<body>

<form action="check.php" method="post">
<table name="boxes">
<tr>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
</tr>
<tr>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
</tr>
<tr>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
</tr>
</table>
<input type="submit" value="Check">
</form>

</body>
</html>
but I have no idea how to get the data from the table into an array or equivalent in php.
Can anyone help?

Thanks in advance
GravityScore #2
Posted 06 May 2013 - 09:42 AM
Simplest way would be to use an HTML form and submit it to a PHP script. Just surround your table in <form> tags, set the action and method (I would recommend the post method) attributes of the form tag, and give each of your inputs a name attribute, which you can use to access it in the PHP script.



<!DOCTYPE html>
<html>
<body>

<form action="myphpscript.php" method="post">
<table>
<tr>
<!-- Notice each of the inputs now has a name attribute. You'd need to go fill all of them in -->
<td><table><tr><td><input type="text" name="1-1" maxlength=1 size=1></td><td><input type="text" name="1-2" maxlength=1 size=1></td><td><input type="text" name="1-3" maxlength=1 size=1></td></tr><tr><td><input type="text" name="1-4" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
</tr>
<tr>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
</tr>
<tr>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
<td><table><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr><tr><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td><td><input type="text" maxlength=1 size=1></td></tr></table></td>
</tr>
</table>
</form>

</body>
</html>

After that, you put your PHP in the script you specified in the action attribute. Then you can access all the table data from the $_POST superglobal in PHP. In this example, this would be how you access the data in your myphpscript.php:

$input11 = $_POST["1-1"];
$input12 = $_POST["1-2"];

// Or you could use a loop to put it all into an array (I think this will work, not sure, you might need to experiment a bit)
$input = array();
for ($i = 1; $i < 9; $i ++) {
  array_push($input, array());
  for ($i2 = 1; $i < 9; $i ++) {  
	array_push($input[$i - 1], $_POST[$i2."-".$i]);
  }
}
Exerro #3
Posted 06 May 2013 - 09:44 AM
I did figure out I had to surround it with <form> tags but didn't know each box needed to have a name
This is going to take a while…is there any easier way of doing it?

Thanks :D/>

edit: I modified the loop array thing to this:
Spoiler
<?php
  $input = array( );
  for ( $i = 1; $i < 9; $i++ ) 
  {
    $input[$i] = array( );
    for ( $i2 = 1; $i < 9; $i++ ) 
    {  
          $input[$i][$i2] = $_POST[$i2."-".$i];
    }
  }
?>
as array_push crashed for some reason
Should this work?
GravityScore #4
Posted 06 May 2013 - 10:00 AM
I did figure out I had to surround it with <form> tags but didn't know each box needed to have a name
This is going to take a while…is there any easier way of doing it?

Thanks :D/>

I was able to give them all name="" tags using regex in ST2, but incrementing numbers in regex isn't possible to my knowledge. You could write a really simple Lua script to do it for you (using the 3rd argument of string.gsub, docs, go to the string.gsub section, and check out the part on the 3rd argument you can pass to it).



<!DOCTYPE html>
<html>
<body>

<form action="myphpscript.php" method="post">

<table>
<tr>
<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>

<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>

<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>
</tr>

<tr>
<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>

<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>

<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>
</tr>

<tr>
<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>

<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>

<td><table><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr><tr><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td><td><input type="text" name="" maxlength=1 size=1></td></tr></table></td>
</tr>

</table>

</form>

</body>
</html>
Exerro #5
Posted 06 May 2013 - 10:01 AM
wow thanks, I'm on a different computer that doesn't have cc installed but this will help a lot
Exerro #6
Posted 06 May 2013 - 11:31 AM
Ok, I have made it so the php script gets what is inputted but now I'm having an even more confusing error
In the checkColumn function I get it to echo input[1][1] and it doesn't do anything but at the end if I do the same it prints 2 ( the number in that box )
I have no idea what is causing this to happen as the part that gets the input array is above all of the code
code:
Spoiler
<!DOCTYPE html>
<html>
<body>

<?php
  $input = array( );
  for ( $i = 1; $i < 9; $i++ )
  {
	$input[$i] = array( );
	for ( $i2 = 1; $i < 9; $i++ )
	{  
		  $input[$i][$i2] = $_POST[$i2."-".$i];
	};
  };
  function tostring( $bool ) {
	if ( $bool == false ) {
	  return "false";
	}
	else if ( $bool == true ) {
	  return "true";
	};
	return "nil";
  };
  function checkBox( $n ){
	if ( $n < 1 or $n > 9 ) {
	  return nil;
	};
	$x = 0;
	$y = 1;
	while ( $n >= 4 ){
	  $y = $y+1;
	  $n = $n-3;
	};
	$x = $x+$n;
	$n = array( );
	for ( $i = ( $x*3 ) - 2; $i <= $x*3; $i++ ) {
	  for ( $k = $y*3-2; $k <= $y*3; $k++ ) {
		$n[$input[$k][$i]] = true;
	  };
	 };
	 for ( $i = 1; $i <= 9; $i++ ) {
	   if ( !$n[i] or $n[i] != true ) {
		return false;
	   };
	 };
	 return true;
   };
   function checkColumn( $n ) {
	 $num = array( );
	 for ( $i = 1; $i<=9; $i++ ) {
	   $num[$input[$n][$i]] = true;
	 };
	 for ( $i = 1; $i<=9; $i++ ) {
	   if ( !$num[i] or $num[i] != true ) {
		 return false;
	   };
	 };
	 return true;
   };
   function checkRow( $n ) {
	 $num = array( );
	 for ( $i = 1; $i<=9; $i++ ) {
	   $num[$input[$i][$n]] = true;
	 };
	 for ( $i = 1; $i<=9; $i++ ) {
	   if ( !$num[i] or $num[i] != true ) {
		return false;
	   };
	 };
	 return true;
   }
   function check( ) {
	 for ( $i=1; $i<=9; $i++ ) {
	   $box = checkBox( $i );
	   $row = checkRow( $i );
	   $col = checkColumn( $i );
	   $data = tostring( $row ) . tostring( $col ) . tostring( $box );
	   echo $data;
	   if ( !$row or !col or !box ) {
		 return "false";
	   }
	 }
	 return "true";
   }
   echo tostring( checkColumn( 1 ) );
?>

</body>
</html>
Lyqyd #7
Posted 06 May 2013 - 12:38 PM
Moved to General.
Smiley43210 #8
Posted 06 May 2013 - 08:03 PM
I found a problem which is certainly hindering your progress. Your second initial for loop checks the $i variable instead of the $i2 variable. Also, you were only looping until $i and $i2 were not less than 9. Meaning it would never reach 9, stopping at 8.
SpoilerOld:
$input = array( );
  for ( $i = 1; $i < 9; $i++ ) {
		$input[$i] = array( );
		for ( $i2 = 1; $i < 9; $i++ ) {  
			  $input[$i][$i2] = $_POST[$i2."-".$i];
		}
  }


Corrected:
$input = array( );
  for ( $i = 1; $i <= 9; $i++ ) {
		$input[$i] = array( );
		for ( $i2 = 1; $i2 <= 9; $i2++ ) {  
			  $input[$i][$i2] = $_POST[$i2."-".$i];
		}
  }

For some reason, the $input array doesn't seem to exist after the for loop executes. See here: http://smiley43210.k...com/sudokuTest/
Try filling in the first column and click "check".
Click the "View the source code" button to see what i currently have in check.php.
Exerro #9
Posted 04 June 2013 - 12:00 PM
I found a problem which is certainly hindering your progress. Your second initial for loop checks the $i variable instead of the $i2 variable. Also, you were only looping until $i and $i2 were not less than 9. Meaning it would never reach 9, stopping at 8.
SpoilerOld:
$input = array( );
  for ( $i = 1; $i < 9; $i++ ) {
		$input[$i] = array( );
		for ( $i2 = 1; $i < 9; $i++ ) {  
			  $input[$i][$i2] = $_POST[$i2."-".$i];
		}
  }


Corrected:
$input = array( );
  for ( $i = 1; $i <= 9; $i++ ) {
		$input[$i] = array( );
		for ( $i2 = 1; $i2 <= 9; $i2++ ) {  
			  $input[$i][$i2] = $_POST[$i2."-".$i];
		}
  }

For some reason, the $input array doesn't seem to exist after the for loop executes. See here: http://smiley43210.k...com/sudokuTest/
Try filling in the first column and click "check".
Click the "View the source code" button to see what i currently have in check.php.
Thanks for replying, I gave up a long time ago because it was only inputting the first column, not the others. I am guessing I was doing the html wrong or something. It is also possible that I messed up with the numbering ( of the boxes html side ) which would cause the same problem. I did realise that it was going 1-8 and not doing nine and I think the i, i2 as well, but as it turned out I am supposed to be learning android programming ( Java but modified ) so this was a bit of a waste of time.