0

我正在使用 php 连接到数据库并将表格的所有行显示为网页上的表格。现在我想要的是在每一行中都有一个复选框和一个隐藏按钮,这样当我点击一行中的一个复选框时,该行的相应按钮变得可见,当我点击该按钮时,该行将被删除。

这是我的php代码。我正在使用 wordpress,因为 wordpress 不允许在页面上使用 php,所以我使用插件 shortcode-exec-php 在 wordpress 页面上启用 php。但我认为这更像是一个编程问题,而不是 wordpress 支持问题。

// Connect to the database
$dbLink = new mysqli('127.0.0.1', 'abc', '123', 'test');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
} 
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
 // Make sure there are some files in there
if($result->num_rows == 0) {
    echo '<p>There are no files in the database</p>';
}
else {
    // Print the top of a table
    echo '<table width="100%">
            <tr>
                <td><b>Name</b></td>
                <td><b>Mime</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
                <td><b>&nbsp;</b></td>
            </tr>';
    // Print each file
    while($row = $result->fetch_assoc()) {
    echo "
            <tr>
                <td>{$row['name']}</td>
                <td>{$row['mime']}</td>
                <td>{$row['size']}</td>
                <td>{$row['created']}</td>
                <td><a href='get_file.php?id={$row['id']}'>Download</a></td>
               //here i want a code that insert a checkbox and a hidden button with                             the above mentioned functionalty
             </tr>";    
}
    // Close table
   echo "</table>";
}
// Free the result
$result->free();
}
else 
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
} 
// Close the mysql connection
$dbLink->close();

有谁能够帮我?

如果您提到 javascript,请告诉我一种将它与现有代码一起使用的方法。

4

1 回答 1

1

这是一个可能对您有帮助的代码(使用 jquery):

php:

 echo "
        <tr>
            <td>{$row['name']}</td>
            <td>{$row['mime']}</td>
            <td>{$row['size']}</td>
            <td>{$row['created']}</td>
            <td><a href='get_file.php?id={$row['id']}'>Download</a></td>
             <td><input type='checkbox' class='checkbox'><input type='button' style='display:none' value='delete' name='delete'><input type='button' style='display:none' value='edit'  name='edit'></td>
         </tr>";

查询:

$(document).ready(function(){
 $("input[type=checkbox]").on("click", function () {
   ($(this).is(":checked")) ? $(this).nextAll("input").show() : $(this).nextAll("input").hide();
 });
 $("input[name='delete']").on("click", function () {
   $(this).closest("table tr").remove();
 });
   $("input[name='edit']").on("click", function () {
    $(this).closest("tr").attr("contenteditable",true).focus();
  });
});

这是一个演示:http: //jsfiddle.net/hctyP/2/

于 2013-11-05T04:59:00.233 回答