Jump to content


Photo

selecting records by using checkbox


  • Please log in to reply
1 reply to this topic

#1 molly31

molly31
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 18 March 2006 - 07:55 PM

I have a database already set up and want to display records from it on another page, selected by 'ticking' a checkbox

How do i do it?

Thanks in advance for any help

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 18 March 2006 - 08:11 PM

This will print off all records in the database, along with column names in a single table. It will also place a checkbox at the beginning of each row. You will have to wrap it in your form tags. When it is submittted, you will have in $_POST all of the checkboxes and the ones that are selected will have a value of "on".

Do a print_r on $_POST to see what the result is.

You will also have to supply the db connection and the tablename as $tablename.

<?php
$query = "SHOW COLUMNS FROM tablename";
$result = mysql_query($query);

$html = '
    <table>
        <tr>';
    
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if ($row['Key'] == "PRI") {
        $primarykey = $row['Field'];
    }
    
    $html .= "<th>" . $row['Field'] . "</th>";
}

$html .= "
        </tr>";

$query = "SELECT * FROM tablename";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $html .= "<tr><td><input type=\"checkbox\" name=\"" . $row[$primarykey] . "\"></td>";
    foreach ($row as $value) {
        $html .= "<td>$value</td>";
    }
    $html .= "</tr>";
}
$html .= "
    </table>";





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users