www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Code optimization

  1. #1
    Join Date
    Jan 2014
    Location
    Boston, Massachusetts
    Posts
    64

    resolved [RESOLVED] Code optimization

    Hi all. I'm pretty new to php. I'm doing a basic table and trying to write 20 rows of 20 <td> (totalling 400).

    What I'm doing is working, but I know there's a way to do it better; I just can't figure out how.

    I appreciate any help.

    PHP Code:
    <h2 class="ribbon"><span class="ribbon-content">Lego Points</span></h2>
    <table class="table table-striped table-bordered">
    <tr><th colspan="20"><h2>Lego Points</h2></th></tr>

    <?php 

    $num 
    1;
    echo 
    "<tr>";
    while ( 
    $num <=20 ) {

    echo 
    '<td class="box"><input type="checkbox" name="radio">' $num "</td>";
    $num++;

    }
    echo 
    "</tr>";

    $num 21;
    echo 
    "<tr>";
    while ( 
    $num <=40 ) {

    echo 
    '<td class="box"><input type="checkbox" name="radio">' $num "</td>";
    $num++;

    }
    echo 
    "</tr>";

    $num 41;
    echo 
    "<tr>";
    while ( 
    $num <=60 ) {

    echo 
    '<td class="box"><input type="checkbox" name="radio">' $num "</td>";
    $num++;

    }
    echo 
    "</tr>";

    $num 61;
    echo 
    "<tr>";
    while ( 
    $num <=80 ) {

    echo 
    '<td class="box"><input type="checkbox" name="radio">' $num "</td>";
    $num++;

    }
    echo 
    "</tr>";

    $num 81;
    echo 
    "<tr>";
    while ( 
    $num <=100 ) {

    echo 
    '<td class="box"><input type="checkbox" name="radio">' $num "</td>";
    $num++;

    }
    echo 
    "</tr>";

    ?>
    </table>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,225
    Just to show the basic framework without all the other bells and whistles:
    PHP Code:
    <table>
    <?php
    for($row 0$row 20$row++) {
        echo 
    "<tr>";
        for(
    $col 1$col <= 20$col++) {
            echo 
    "<td>".( ($row 20) + $col)."</td>";
        }
        echo 
    "</tr>\n";
    }
    ?>
    </table>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2014
    Location
    Boston, Massachusetts
    Posts
    64
    Hey, thanks! Worked like a charm. I'm definitely digging this forum; the people seem helpful.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles