Help understanding a script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Help understanding a script

    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)) {
    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";

    ?>


  • Geoff Berrow

    #2
    Re: Help understanding a script

    Message-ID: <q9mTf.1326$3t1 .818@trndny08> from <newbie@php.wor ld>
    contained the following:
    [color=blue]
    >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.[/color]

    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/

    Comment

    • ME@newbie.com

      #3
      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.

      Comment

      • Geoff Berrow

        #4
        Re: Help understanding a script

        Message-ID: <5FmTf.1327$3t1 .1243@trndny08> from ME@newbie.com contained
        the following:
        [color=blue]
        >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.[/color]

        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/

        Comment

        • Roger Dodger

          #5
          Re: Help understanding a script

          Geoff Berrow wrote:[color=blue]
          > Message-ID: <5FmTf.1327$3t1 .1243@trndny08> from ME@newbie.com contained
          > the following:
          >[color=green]
          > >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.[/color]
          >
          > 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/[/color]

          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 '<table border="1" cellspacing="5" cellpadding="5" >';
          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>';
          ?>

          Comment

          • Ben Bacarisse

            #6
            Re: Help understanding a script

            On Sun, 19 Mar 2006 23:52:54 +0000, newbie wrote:
            [color=blue]
            > 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)) {
            >
            >
            > From what I try to understand this code
            >
            >
            Code:
            >  if (($x % pow(2, $y)) == pow(2, $y-1)) {
            >
            >
            > 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.[/color]

            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.

            Comment

            • ME@newbie.com

              #7
              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
              for people to download for free. The top part ot the code above the php 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;
              title = "Your Title Here";
              
              [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>
              <head>
              <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>
              </head>
              
              <body bgcolor="blue" leftmargin="20" topmargin="20" rightmargin="20"
              bottommargin="20" marginwidth="20">
              <div align="center">
              <table width="700" border="0" cellspacing="0" cellpadding="20"
              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"
              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)) {
              $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>
              Any help with converting code to be used with a db would be greatly
              appreciated.

              Comment

              • Jamer

                #8
                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
                for people to download for free. The top part ot the code above the php 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;
                title = "Your Title Here";
                
                [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>
                <head>
                <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>
                </head>
                
                <body bgcolor="blue" leftmargin="20" topmargin="20" rightmargin="20"
                bottommargin="20" marginwidth="20">
                <div align="center">
                <table width="700" border="0" cellspacing="0" cellpadding="20"
                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"
                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)) {
                $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>
                Any help with converting code to be used with a db would be greatly
                appreciated.
                <newbie@php.wor ld> wrote in message news:q9mTf.1326 $3t1.818@trndny 08...[color=blue]
                >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)) {
                >
                >
                > 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";
                >
                > ?>
                >
                >[/color]


                Comment

                Working...