开发者

Gray bearded newbie needs php guidance [closed]

开发者_StackOverflow中文版 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I've created a boardgame and there is one element I can create manually but it is a very time consuming task so after much research I decided to learn php to automate the task. This is what I want to do and would appreciate any guidance towards specific learning resources that might help me in this task.

Create 4 10 x 10 tables with each cell being sequentially numbered from 0=99.

Ideally the numbering will be inside the cells but if I really had to I could use grid coordinates instead

I then want to randomly populate the cells, in each table one at a time, with two letter strings from an existing array. e.g table1 0TE 1GR 2MN 3TE 4KN....

I then want to be able to print the tables

BTW I choose php because I want players to be able to create their own tables online, also it will, in the long term, be the most beneficial to me as I will never be a professional programmer.

Cheers Don


First, a superb PHP reference is the online manual.

The grid could most easily be handled by making each box a <div> or, since this is tabular data a <td> in a table. To achieve this in php use a for() loop or two.

You'll have to do both the numbering and the insertion of the random strings inside of this for() loop.

To pick random elements from an array use, array_rand(). ( info on arrays in PHP ).

You'll probably want to do your styling with CSS.

If you do not use HTML / CSS, and decide to just use PHP output in a non HTML environment, then you'll want to make use of the plethora of PHP string functions. str_pad() and printf() are especially useful for formatting your output in these situations.

Finally, for sharing / testing code Codepad & Codepad Viper are fantastic.


First, please read the comment I left.

Second, since you asked for resources:

http://www.php.net/ - The Mothership

http://devzone.zend.com/node/view/id/627 - Old but still usable

There's your start.


how the code would be written greatly depends on where this array of 2 letter strings you have is coming from...but here is a self-contained example with some randomly generated 2 letter strings:

$strings = range('A','Z');
for ($a = 0;$a < 100; $a++) {
  $array[] = $strings[rand(0,25)] . $strings[rand(0,25)];
} 

for ($table = 1; $table <= 4; $table++) {
  $count = 0;
  $tarray = $array;
  echo "table " . $table . "<br/>";
  echo "<table border='1'>";
  for ($x = 0; $x < 10; $x++) {
    echo "<tr>";
    for ($y = 0; $y < 10; $y++) {
      $alpha = array_splice($tarray,rand(0,count($tarray)-1),1);  
      echo "<td>" . $count . $alpha[0] . "</td>";
      $count++;
    }
    echo "</tr>";
  }
  echo "</table>";
}

could shorten this with completely random generated 2 letter strings but i particularly wanted to show use of array_splice() if you have an actual array of 100 (or 400?) 2 letter strings and wanted to do a "pick once" deal, where you randomly select it and can only use it on one cell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜