Hi, i am making a website which prove a sudoku interactive game that enables users to play it.

the game data is in the form of database and have the following field:

+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| game_no | mediumint(5) | NO | PRI | NULL | auto_increment |
| rating | varchar(14) | YES | | NULL | |
| row1 | varchar(20) | YES | | NULL | |
| row2 | varchar(20) | YES | | NULL | |
| row3 | varchar(20) | YES | | NULL | |
| row4 | varchar(20) | YES | | NULL | |
| row5 | varchar(20) | YES | | NULL | |
| row6 | varchar(20) | YES | | NULL | |
| row7 | varchar(20) | YES | | NULL | |
| row8 | varchar(20) | YES | | NULL | |
| row9 | varchar(20) | YES | | NULL | |
| game_row1 | varchar(20) | YES | | NULL | |
| game_row2 | varchar(20) | YES | | NULL | |
| game_row3 | varchar(20) | YES | | NULL | |
| game_row4 | varchar(20) | YES | | NULL | |
| game_row5 | varchar(20) | YES | | NULL | |
| game_row6 | varchar(20) | YES | | NULL | |
| game_row7 | varchar(20) | YES | | NULL | |
| game_row8 | varchar(20) | YES | | NULL | |
| game_row9 | varchar(20) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+

The columns labelled row1,row2, ... row9, are the 9 rows of the completed game. The 9 columns labelled game_row1, game_row2,... game_row9 show the rows of the game with numbers missing (value "0" show the missing number and users need to put the number in it). The numbers in each row are separated by commas.

there is 9 different games altogether (game_no) and 3 type of difficult level (rating).

Here is an example of one game stored in the table.

Column Data
Name Value
================================
game_no 3
rating medium
row1 9,8,6,4,2,5,7,1,3
row2 3,7,5,6,8,1,4,2,9
row3 2,1,4,7,3,9,8,6,5
row4 4,6,1,8,7,3,5,9,2
row5 5,2,7,9,1,6,3,8,4
row6 8,9,3,2,5,4,1,7,6
row7 1,3,8,5,9,2,6,4,7
row8 6,5,2,1,4,7,9,3,8
row9 7,4,9,3,6,8,2,5,1
game_row1 0,8,0,0,0,0,0,1,3
game_row2 0,0,5,6,0,1,0,2,0
game_row3 0,0,4,0,3,0,8,0,0
game_row4 0,0,1,8,0,3,5,0,0
game_row5 0,0,0,0,0,0,0,0,0
game_row6 0,0,3,2,0,4,1,0,0
game_row7 0,0,8,0,9,0,6,0,0
game_row8 0,5,0,1,0,7,9,0,0
game_row9 7,4,0,0,0,0,0,5,0

the first page will direct user to choose either game_no or level of difficulty, after that, a sudoku game with the value inside will appear to be played by the user. The 'gamerow1' - 'gamerow9' should fill the value in each box in sudoku. If the value is "0", then the box will become [input type="text"] to be filled with user. Whenever a user enters a number in any of these input textfields an AJAX request will be sent to the server to check that the number entered is correct.
So basicly i need to do this using ajax.

I am able to generate the first page, but it can't generate the sudoku table, actually i don't know how to generate the table that consist the value from database.
The database is provided by a server, so i wont write the code for linking the database here.

Here is my code so far:
This is the code that generate the first page:

<script type="text/javascript">
function validate_fields() {
  var gameNum = document.getElementById("game_no").value;
  var gameLvl = document.getElementById("rating").value;
  if (gameNum && gameLvl) {
     alert("Please select either a game number OR a level of difficulty only.\nDo not select both.");
     return false;
  }
  return true;
 }
</script>

<form method="post" action="index.php" onsubmit="return validate_fields()">
<p><label for="game_no">Enter Game Number</label>
<select name="game_no" id="game_no">
<option value>Select Number</option>
<?php
  $link = mysql_connect(..., ..., ...);
  mysql_select_db(...., $link);
  $query_string = "select distinct game_no from sudoku order by game_no";
  $game_no = mysql_query($query_string);
  while($rowNumber = mysql_fetch_assoc($game_no)){
  echo '<option 
value="'.$rowNumber['game_no'].'">'.$rowNumber['game_no'].'</option>';
  }
?>
</select></p>

<p><label for="rating">Choose Game Level</label>
<select name="rating" id="rating">
<option value>Select Level</option>
<?php
  $link = mysql_connect(..., ..., ...);
  mysql_select_db(...., $link);
  $query_string = "select distinct rating from sudoku order by rating";
  $rating = mysql_query($query_string);
  while($rowLevel = mysql_fetch_assoc($rating)){
  echo'<option value="'.$rowLevel['rating'].'">'.$rowLevel['rating'].'</option>';
  }
?>
</select></p>

<p><input type="submit" value="Search"></p>
</form>

I am still doing the code to produce the second page, but it does not work at all. I don't if it is true already, can I get some helps?
Here is my code so far (in this code, i try to generate the game from game_no="3"):

<?php
      $link = mysql_connect(..., ..., ...);
      mysql_select_db(...., $link);

      $sql="select * from sudoku where game_no="3"";
      $result=mysql_query($sql) or exit(mysql_error());
      print '<table>';
      $i=1;
      while ($gamerow=mysql_fetch_assoc($result))
      {
         $curGR="gamerow$i";
         $gamerow=explode(',',$$curGR);
         print '<tr>';
         for($c=0;$c<9;$c++)
         {
            print '<td>';
            if($gamerow[$c]==0)
            {
               print "<input type=\"text\" name=\"game[{$$curGR}][$c]\" />";
            }
            else
            {
               print $gamerow[$c];
            }
            print '</td>';
          }
          print '</tr>';
      }
      print '</table>';
?>

thanks

As far as I can see your are pretty close to finishing this. You should check the following things:

A) Is the table correctly printed
B) Are the options for difficulty and level select printed properly
C) Does the function validate_fields() work properly

If the above are all good, you should now focus on a function that uses an AJAX call to retrieve the table from the last script and then print it within a div: GetSudokuPuzzle() or something like that. It should be called instead of the form submit, so in the form tag you put: onsubmit="GetSudokuPuzzle(); return false;"

A small start:

function GetSudokuPuzzle() {
 var valid = validate_fields();
 if (valid == true) {
  //....
  //.... Here comes the AJAX call. You need to save the responseText in a variable, lets say "sudokuTable"
  //....

  // Now you put the sudokuTable within the div
  document.getElementById("sudoku").innerHTML = sudokuTable;
 }
}

You should look for tutorials on the internet on using AJAX: http://www.w3schools.com/ajax/default.asp

~G

The option of difficulty and game number are correctly printed.
The validate_field() also work properly.

But i can't print out the table.
So that's why i tried to print the table with game_no=3, but it still does not want to work.
Is there something wrong with my code to print the table??
I have changed everything since yesterday,. but it still does not want to work.

in your sql query you have double quotes around the the 3:

$sql="select * from sudoku where game_no="3"";

They should be single qoutes:

$sql="select * from sudoku where game_no='3'";

Hi,

that's correct, it should be single quote..
But then it only produces 9 input table in a row, there is nothing in it too..
So i guess, the way i code the output is wrong, sigh*
will try to get it work

I did not pay attention to syntax errors, but anyway, here are a few:

>> Line 11 - This might be incorrect, not sure. Perhaps use $curGr = "gamerow".$i;

>> The $i , what is this supposed to do? It is not incremented but only added to a name?

>> line 19 - What do you want as name? Beginning with "game" then add $curGR behind that and behind that $c?????

~G

Hi,

I have change my code, because the initial code can't work.
now with this code, it can produce the output and table (but still wrong):

<?php
	require_once "db_settings.inc";
	$dbc = new mysqli(DB_HOST,DB_QUERY_USER,DB_QUERY_PASS,WEA_DB);

	$result=$dbc->query("select * from sudoku where game_no=3;");

	print '<table border=1>';
	while ( $a_row = $result->fetch_row())
	{ 
		for($c=2;$c<11;$c++)
		{
			print "\t<tr><td>$a_row[$c]</td></tr>\n";
		}
	}
	print '</table>';
?>

$c=2 - $c=11 is because that are the arrays that contain the sudoku 9 digits set separated by coma.
So the output of this code will be 9 rows, each row contain 9 digits separated by coma.

I want to throw the comas, i know this can be done using explode function. But i have tried it anywhere, and it still does not want to work.

Can you help me?
thanks
I want to throw the comas

Anyway, i can solve it now.
Now i am going to do the ajax part.

Wil contact u if there is any difficulty

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.