# Help understanding a script

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I was wondering if anyone can help explain this code for me

[CODE
]<table width="<?= \$TABLE_WIDTH ?>" border="0" cellspacing="0" cellpadding="0">
<?php
for (\$x=1; \$x<=pow(2, \$COLUMNS); \$x++) {
// open the row
printf('<tr>');

// draw each of the columns in this row...
for (\$y=1; \$y<=\$COLUMNS; \$y++) {

// begin with a default style
\$style = "line";
\$value = "";

// Determine the content for the data cells (if there is any) and
// add the underline style always
if ((\$x % pow(2, \$y)) == pow(2, \$y-1)) {
[/CODE]

From what I try to understand this code

[CODE]
if ((\$x % pow(2, \$y)) == pow(2, \$y-1)) {
[/CODE]

would be a smaller number divided by a larger number and would give the remainder. I have tried a smaller number divided by a larger number in a simple script, but the remainder always seems to equal the smaller number.

[CODE]
<?php
\$a=6;
\$b=11;
\$c=\$a%\$b;

echo "\$a % \$b = \$c";

?>

## Re: Help understanding a script

contained the following:

well...yeah.

six divided by eleven = zero, remainder six

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

## Re: Help understanding a script

Interesting. Back when I was taught this in school you would skip the units
and put a decimal and move to the tenths and change six to sixty, thereby
never knowing this. Any whoo still dont understand script.

## Re: Help understanding a script

the following:

What is it supposed to do?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

## Re: Help understanding a script

Geoff Berrow wrote:

I can't figure out what it's supposed to do.  Do you have more code?
There is definitely a regular relationship as the code below shows.
But what is that relationship used for?

<?php
echo '<tr><th>\$x</th><th>\$y</th><th>\$x % pow(2, \$y)</th><th>pow(2,
\$y-1)</th>';
echo
'<th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th>';
echo '<th>8</th><th>9</th><th>10</th></tr>';
for (\$x=1; \$x<=pow(2, 10); \$x++) {
for (\$y=1; \$y<=10; \$y++) {

\$xcmp = \$x % pow(2, \$y);
\$ycmp = pow(2, \$y-1);
if (\$xcmp == \$ycmp) {
echo "<tr><td>\$x</td><td>\$y</td><td
align=\"center\">".\$xcmp."</td>";
echo "<td align=\"center\">".\$ycmp."</td>";
for (\$i=1; \$i<=10; \$i++){
if (\$i == \$ycmp) echo "<td>X</td>";
else echo "<td>&nbsp</td>";
}
echo "</tr>";
}
}
}
echo '</table>';
?>

## Re: Help understanding a script

On Sun, 19 Mar 2006 23:52:54 +0000, newbie wrote:

Not always.  \$x is often bigger than pow(2, \$y).  It looks like code you'd
use to layout the draw in an N-round knockout tournament (N == \$COLUMNS):

The cells selected by that test alternate in the first column, are one in
four of the second, one in eight in the third and so on...  All lined up
so no row has more than one selected cell.  It is quite clever but it
could do with a comment or two, though!

What is it actually for?

--
Ben.

## Re: Help understanding a script

Yes it is for a round knockout tournament, it uses a ini file for data. I
want to convert the script to be used in a database for a site. Here is the
rest of the code. I found it on the internet, the guys was giving the code
is actually the ini file that was name .txt that i switch to .php thinking i
could use it for variable and getting info from the db, which i have found I
can't, and need to recode everything but the math.

[code]
________________________________________________________________________________________________________________

[bracket]
columns = 5;
column_width = 120;

[column7]
1 = ""
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""
9 = ""
10 = ""
11 = ""
12 = ""
13 = ""
14 = ""
15 = ""
16 = ""
17 = ""
18 = ""
19 = ""
20 = ""
21 = ""
22 = ""
23 = ""
24 = ""
25 = ""
26 = ""
27 = ""
28 = ""
29 = ""
30 = ""
31 = ""
32 = ""
33 = ""
34 = ""
35 = ""
36 = ""
37 = ""
38 = ""
39 = ""
40 = ""
41 = ""
42 = ""
43 = ""
44 = ""
45 = ""
46 = ""
47 = ""
48 = ""
49 = ""
50 = ""
51 = ""
52 = ""
53 = ""
54 = ""
55 = ""
56 = ""
57 = ""
58 = ""
59 = ""
60 = ""
61 = ""
62 = ""
63 = ""
64 = ""

[column6]
1 = ""
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""
9 = ""
10 = ""
11 = ""
12 = ""
13 = ""
14 = ""
15 = ""
16 = ""
17 = ""
18 = ""
19 = ""
20 = ""
21 = ""
22 = ""
23 = ""
24 = ""
25 = ""
26 = ""
27 = ""
28 = ""
29 = ""
30 = ""
31 = ""
32 = ""

[column5]
1 = "team name between quotes"
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""
9 = ""
10 = ""
11 = ""
12 = ""
13 = ""
14 = ""
15 = ""
16 = ""

[column4]
1 = ""
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""

[column3]
1 = ""
2 = ""
3 = ""
4 = ""

[column2]
1 = ""
2 = ""

[column1]
1 = "<i>Champion</i>"

________________________________________________________________________________________________________________

<?php
//----------------------------------------------------------------------
\$DATA         = parse_ini_file("data_sheet.php", true);
\$COLUMNS      = \$DATA["bracket"]["columns"];
\$COLUMN_WIDTH = \$DATA["bracket"]["column_width"];
\$TABLE_WIDTH  = \$COLUMN_WIDTH * \$COLUMNS;
//----------------------------------------------------------------------
?>
<html>
<title><?= htmlspecialchars(\$DATA["bracket"]["title"]); ?></title>
<style type="text/css">
P {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
color : Black;
}
EM {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
color : #CC0000;
}
A {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
color : Blue;
}
.line {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;

}
.line-right {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;
border-right : 1px solid Red;
}
.line-under {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;
border-bottom : 1px solid Green;
}
.line-under-right {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;
border-bottom : 1px solid Blue;
border-right : 1px solid Grey;
}
.right {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : bold;
color : Green;
}
.wrong {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : bold;
color : Red;
}
</style>

<body bgcolor="blue" leftmargin="20" topmargin="20" rightmargin="20"
bottommargin="20" marginwidth="20">
<div align="center">
bgcolor="gray">
<tr>
<td align="center" valign="top">
<p>

<h1 style="font-family: Arial, Helvetica, sans-serif;">
<?= htmlspecialchars(\$DATA["bracket"]["title"]); ?>
</h1>

<table width="<?= \$TABLE_WIDTH ?>" border="0" cellspacing="0"
<?php
for (\$x=1; \$x<=pow(2, \$COLUMNS); \$x++) {
// open the row
printf('<tr>');

// draw each of the columns in this row...
for (\$y=1; \$y<=\$COLUMNS; \$y++) {

// begin with a default style
\$style = "line";
\$value = "";

// Determine the content for the data cells (if there is any) and
// add the underline style always
if ((\$x % pow(2, \$y)) == pow(2, \$y-1)) {
\$round    = "column" . (\$COLUMNS - \$y +1);
\$posit    = ceil(\$x/pow(2, \$y));
\$data    = (isset(\$DATA[\$round][\$posit])) ?
trim(\$DATA[\$round][\$posit]) : '';
\$parts    = split("\|", \$data);
\$value    = array_shift(\$parts);
\$value    = sprintf(\$value);
\$style .= "-under";
}

// Add the right bar line for cells that need it...
if (\$y != \$COLUMNS) {
if (((\$x + pow(2, \$y-1) - 1) % pow(2, \$y+1)) >= pow(2, \$y)) {
\$style .= "-right";
}
}

// make sure we don't print empty content
if (empty(\$value)) {
\$value = "&nbsp;";
}

// draw this cell
printf('<td width="%d" class="%s">%s</td>',
\$COLUMN_WIDTH, \$style, \$value);
print("\n");
}

// close the row
printf("</tr>\n");
}
?>
</table>
</p>

<p align="left">
****NOTE****<br>
</p>
</td>
</tr>
</table>

</div>

</body>
</html>
[/code]

Any help with converting code to be used with a db would be greatly
appreciated.

## Re: Help understanding a script

Yes it is for a round knockout tournament, it uses a ini file for data. I
want to convert the script to be used in a database for a site. Here is the
rest of the code. I found it on the internet, the guys was giving the code
is actually the ini file that was name .txt that i switch to .php thinking i
could use it for variable and getting info from the db, which i have found I
can't, and need to recode everything but the math.

[code]
________________________________________________________________________________________________________________

[bracket]
columns = 5;
column_width = 120;

[column7]
1 = ""
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""
9 = ""
10 = ""
11 = ""
12 = ""
13 = ""
14 = ""
15 = ""
16 = ""
17 = ""
18 = ""
19 = ""
20 = ""
21 = ""
22 = ""
23 = ""
24 = ""
25 = ""
26 = ""
27 = ""
28 = ""
29 = ""
30 = ""
31 = ""
32 = ""
33 = ""
34 = ""
35 = ""
36 = ""
37 = ""
38 = ""
39 = ""
40 = ""
41 = ""
42 = ""
43 = ""
44 = ""
45 = ""
46 = ""
47 = ""
48 = ""
49 = ""
50 = ""
51 = ""
52 = ""
53 = ""
54 = ""
55 = ""
56 = ""
57 = ""
58 = ""
59 = ""
60 = ""
61 = ""
62 = ""
63 = ""
64 = ""

[column6]
1 = ""
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""
9 = ""
10 = ""
11 = ""
12 = ""
13 = ""
14 = ""
15 = ""
16 = ""
17 = ""
18 = ""
19 = ""
20 = ""
21 = ""
22 = ""
23 = ""
24 = ""
25 = ""
26 = ""
27 = ""
28 = ""
29 = ""
30 = ""
31 = ""
32 = ""

[column5]
1 = "team name between quotes"
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""
9 = ""
10 = ""
11 = ""
12 = ""
13 = ""
14 = ""
15 = ""
16 = ""

[column4]
1 = ""
2 = ""
3 = ""
4 = ""
5 = ""
6 = ""
7 = ""
8 = ""

[column3]
1 = ""
2 = ""
3 = ""
4 = ""

[column2]
1 = ""
2 = ""

[column1]
1 = "<i>Champion</i>"

________________________________________________________________________________________________________________

<?php
//----------------------------------------------------------------------
\$DATA         = parse_ini_file("data_sheet.php", true);
\$COLUMNS      = \$DATA["bracket"]["columns"];
\$COLUMN_WIDTH = \$DATA["bracket"]["column_width"];
\$TABLE_WIDTH  = \$COLUMN_WIDTH * \$COLUMNS;
//----------------------------------------------------------------------
?>
<html>
<title><?= htmlspecialchars(\$DATA["bracket"]["title"]); ?></title>
<style type="text/css">
P {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
color : Black;
}
EM {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
color : #CC0000;
}
A {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
color : Blue;
}
.line {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;

}
.line-right {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;
border-right : 1px solid Red;
}
.line-under {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;
border-bottom : 1px solid Green;
}
.line-under-right {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : normal;
padding : 1px 3px 1px 3px;
border-bottom : 1px solid Blue;
border-right : 1px solid Grey;
}
.right {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : bold;
color : Green;
}
.wrong {
font-family : Arial, Helvetica, sans-serif;
font-size : 11px;
font-weight : bold;
color : Red;
}
</style>

<body bgcolor="blue" leftmargin="20" topmargin="20" rightmargin="20"
bottommargin="20" marginwidth="20">
<div align="center">
bgcolor="gray">
<tr>
<td align="center" valign="top">
<p>

<h1 style="font-family: Arial, Helvetica, sans-serif;">
<?= htmlspecialchars(\$DATA["bracket"]["title"]); ?>
</h1>

<table width="<?= \$TABLE_WIDTH ?>" border="0" cellspacing="0"
<?php
for (\$x=1; \$x<=pow(2, \$COLUMNS); \$x++) {
// open the row
printf('<tr>');

// draw each of the columns in this row...
for (\$y=1; \$y<=\$COLUMNS; \$y++) {

// begin with a default style
\$style = "line";
\$value = "";

// Determine the content for the data cells (if there is any) and
// add the underline style always
if ((\$x % pow(2, \$y)) == pow(2, \$y-1)) {
\$round    = "column" . (\$COLUMNS - \$y +1);
\$posit    = ceil(\$x/pow(2, \$y));
\$data    = (isset(\$DATA[\$round][\$posit])) ?
trim(\$DATA[\$round][\$posit]) : '';
\$parts    = split("\|", \$data);
\$value    = array_shift(\$parts);
\$value    = sprintf(\$value);
\$style .= "-under";
}

// Add the right bar line for cells that need it...
if (\$y != \$COLUMNS) {
if (((\$x + pow(2, \$y-1) - 1) % pow(2, \$y+1)) >= pow(2, \$y)) {
\$style .= "-right";
}
}

// make sure we don't print empty content
if (empty(\$value)) {
\$value = "&nbsp;";
}

// draw this cell
printf('<td width="%d" class="%s">%s</td>',
\$COLUMN_WIDTH, \$style, \$value);
print("\n");
}

// close the row
printf("</tr>\n");
}
?>
</table>
</p>

<p align="left">
****NOTE****<br>
</p>
</td>
</tr>
</table>

</div>

</body>
</html>
[/code]

Any help with converting code to be used with a db would be greatly
appreciated.