Hi, i need to make sudoku games in a website, i have database in mysql about the sudoku, each row contain sufficient information about one game.

| 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. The numbers in each row are separated by commas.

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

anyway, i have another problem. how to make a sudoku grid table and put this database value inside it?
so let say after we select the level of the game that we want, the data will fill the sudoku table, but only if there is data on it. Not every box will be filled with the data. The empty box will have small input type=text for us to fill.
How can I do that?


Recommended Answers

All 12 Replies

$sql="select * from `game` where `game_no`='3' ORDER BY `game_row`;
$result=mysql_query($sql) or exit(mysql_error());
print '<table>'
while ($row=mysql_fetch_assoc($result))
{ $curGR="gamerow$i";
  print '<tr>';
  { print '<td>';
    {  print [...input...]
    {  print $gamerow[$c];
    print '</td>';
  print '</tr>';
print '</table>';
Member Avatar for diafol

>anyway, i have another problem
What was the first problem again?

As given away above, you have to use a loop. Show us your code so far. We're here to help, not to provide you with complete solutions. Perhaps do some research first - 'while loops'.

You should consider using a different table structure. Especially if each game is to be played more than once.

Hi thanks for the reply.
actually, i don't really understand about the specifiaction, but now i have read it and understand it.
Basicly, i get the database from my school server.

in the database, there is 9 number of game and 3 type of level. Each number of game represents a set of 9*9 number. Each type of level, can contain more than 1 number of game. I have finished the code to choose the game number and the game level.
Here is the code:

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

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

<p><label for="rating">Choose Game Level</label>
<select name="rating" id="rating">
<option value>Select Level</option>
  $link = mysql_connect("dbName","user","password");
  $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>';

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

And then after the user chooses the option, the website will prompt the user to a sudoku game based on their choice. This is where i got the problem. I cannot print out the table for sudoku. I have used your code, but it cannot work.
What should I put for this part? --> { print [...input...]

this is the code based on the code given by you:


      $sql="select * from sudoku where game_no="3"";
      $result=mysql_query($sql) or exit(mysql_error());
      print '<table>';
      while ($gamerow=mysql_fetch_assoc($result))
         print '<tr>';
            print '<td>';
               print [...input...]; //i don't know what to put here, but even if i just want to print some integer, it still does not work out
               print $gamerow[$c];
            print '</td>';
          print '</tr>';
      print '</table>';

in the code above, I just want to try if I get the game set for game number = 3.

And as for the explanation of the database. row1-row9 is the completed game set, i want to disregard this for now. gamerow1-gamerow9 is the game set that have "0" value that represent the blank field (input type) that needs to be filled by the user. So I think the key is in gamerow1-gamerow9. After the user choose a particular game number, the sudoku table will contain information from gamerow1-gamerow9.
That's what i think, but i really can't figure out how to work on it.

Sorry, but i am really new to this.
Please help me.

By the way, I dont know if you can connect to the database as well.

Hi wewehalim,
I don't need to connect to the database.
Also I made a small error in the code, see below:

{ print "<input type=\"text\" name=\"game[{$$curGR}][$c]\" />";

I didn't check my code thouroughly so you may need to sift out some errors.

You SHOULD NOT publish your school's server details.

Sorry about this. I have deleted it.
I will try, thanks

Hi, it still did not work though.

Member Avatar for diafol

Is this supposed to be an interactive sudoku table - where a player plays it or is it just for creating a hardcopy?

I take it that this is a school project?

If you want an interactive page - you'll need to use Ajax or it will become unplayable.

Yes it is an interactive sudoku table where players can play.
players will need to put the value in the blank field (where gamerow value=0), and the value should be check by ajax (if i do not get it wrong)
and yeah, it needs to use java.

Indeed this is a school project. As this is my first time taking web development subject. And i have difficulties for this. So I am trying to get help.
First step is in generating the sudoku table. I have tried it this past 2 days, still can't move on.


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.