SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    May 2011
    Posts
    191
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Adding a checkbox in each row to a PHP table

    Hi guys,

    Here a table I made using PHP:

    print ("<TABLE $table_format>\n");

    // table header, two columns:
    print("<TR>\n");
    print("<TH>Title</TH>");
    print("<TH>Price</TH>");
    print("</TR>\n");

    //table body:
    while($row = mysql_fetch_row($result)) {
    print("<TR ALIGN=CENTER VALIGN=TOP>");
    for($column_num = 0; $column_num < $column_count; $column_num++) {
    print("<TD>$row[$column_num]</TD>\n");
    }
    print("</TR>\n");
    }
    print("</TABLE>\n");

    How could I add a checkbox at the left of every row?
    An then, make a new table considering only checked rows.

    As a final request, I have been suggested to replace print() instructions by echo ones. I have tried but I guess I don't know the syntaxis of echo sentences very well.
    How should I write these ones, for instance:
    print("</TR>\n");
    print("<TD>$row[$column_num]</TD>\n");

    Thanks a lot!!!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    How could I add a checkbox at the left of every row?
    Add a new column to table, and put a checkbox in each cell.

    How should I write these ones, for instance:
    print("</TR>\n");
    print("<TD>$row[$column_num]</TD>\n");
    PHP Code:
    echo "</TR>\n");
    echo 
    "<TD>$row[$column_num]</TD>\n"

  3. #3
    SitePoint Zealot
    Join Date
    May 2011
    Posts
    191
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Guido,

    "Add a new column to table, and put a checkbox in each cell."

    Could you please help me with the PHP code for going what you suggested?

    Thanks a lot!!!

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Why don't you start by adding a new column to the table? You wrote the code that creates a two column table, I'm sure you can add a third.

  5. #5
    SitePoint Zealot
    Join Date
    May 2011
    Posts
    191
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yed, done. Now I have a third column, with only the header and with no values in the table body.
    How can I fill it with checkboxes?

    I already changed print sentences by echo ones!

    Thanks!!!


    echo "<table $table_format>\n";
    // table header:
    echo "</tr>\n";
    echo "<th>Title</th>";
    echo "<th>Price</th>";
    echo "<th>Checkbox</th>";
    echo "</tr>\n";

    //table body:
    while($row = mysql_fetch_row($result)) {
    echo "<tr ALIGN=CENTER VALIGN=TOP>";
    for($column_num = 0; $column_num < $column_count; $column_num++) {
    echo "<td>$row[$column_num]</td>\n";
    }
    echo "</tr>\n";
    }
    echo "</table>\n";

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Ok, now in the table body part add an echo after the for loop:
    PHP Code:
    echo '<td><input type="checkbox" name="check[]" value="..."/></td>"; 
    If you want to show the checked rows once the form has been submitted, then you'll have to put a value where I put '...' that identifies the row (for example the ID).

    If you google for 'php form' you'll find lots of tutorials and examples. This one for example: http://www.html-form-guide.com/php-f...-checkbox.html

  7. #7
    SitePoint Zealot
    Join Date
    May 2011
    Posts
    191
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Great!!

    I will take a look.
    How can I writh the sentence in order checkboxes appear selected?

    Thanks!!


Bookmarks

Posting Permissions

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