简体   繁体   English

PHP和数据表行删除/更新

[英]php and datatable row delete/update

I use DataTable to display table here is a code: 我使用DataTable显示表,这里是一个代码:

$(document).ready(function() {
$('#example').dataTable( {
        "iDisplayLength": 50,
    "sPaginationType": "full_numbers",
     "bStateSave": true
} );
} );
</head>
<body>
             <table class="display" id="example">
            <thead>
                <tr>
                <th class="id-th"><strong>ID</strong></th>
                <th class="date-th"><strong>Date Added</strong></th>
                <th class="fullname-th"><strong>Full name</strong></th>
                <th class="email-th"><strong>Email</strong></th>
                <th class="visits-th"><strong>Visits</strong></th>
                <th class="totalleads-th"><strong>Total Leads</strong></th>
                <th class="uniqueleads-th"><strong>Unique Leads</strong></th>
                <th class="unpaid-th"><strong>Unpaid Leads</strong></th>
                <th class="action-th"><strong>Actions</strong></th>
              </tr>
</thead>
<tbody>
<?php$query = "SELECT * FROM publishers";
$result = mysql_query($query) or die("Query failed: " . mysql_error());while ($row = mysql_fetch_array($result))
{
$publisher_id = $row['id'];
$publisher_datetime = $row['date_time'];
$publisher_firstname = $row['first_name'];
$publisher_lastname = $row['last_name'];
$publisher_email = $row['email']; ?>
                <tr class="">
                <td class="id-th"><strong><?php echo $publisher_id; ?></strong></td>
                <td class="date-th"><strong><?php echo $publisher_datetime; ?></strong></td>
                <td class="fullname-th"><strong><?php echo "$publisher_firstname $publisher_lastname"; ?></strong></td>
                <td class="email-th"><strong><?php echo $publisher_email; ?></strong></td>

                <td class="visits-th"><strong><?php echo $publisher_visits; ?></strong></td>
                <td class="totalleads-th"><strong><?php echo $publisher_total; ?></strong></td>
                <td class="uniqueleads-th"><strong><?php echo $publisher_unique; ?></strong></td>
                <td class="unpaid-th"><strong><?php echo $publisher_unpaid; ?></strong></td>
                <td class="action-th">
                    <ul class="button-table-head">
                        <li><div class="button-head edit-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Edit" data-style-tooltip="tooltip-mini-slick"><span>Edit</span></a></div></li>
                        <li><div class="button-head delete-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Delete" data-style-tooltip="tooltip-mini-slick" onclick="jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function(r) {
        jAlert('Publisher deleted');
    });"><span>Delete</span></a></div></li>
                    </ul>
                </td>
              </tr><?php } ?></tbody></table>

So now my question is - when I edit or delete row - do I have to RELOAD page each time in order for update to show ? 所以现在我的问题是-当我编辑或删除行时-是否每次都必须重新加载页面才能显示更新? I tried reading the instructions on DataTable, but I really don't understand it. 我尝试阅读有关DataTable的说明,但我真的不理解。

Thanks for tips ! 感谢您的提示!

If you want to use pure PHP, then yes . 如果要使用纯PHP,则可以

If you are open to other methods, like calling PHP scripts with AJAX, then no : 如果您愿意接受其他方法,例如使用AJAX调用PHP脚本,则

Make a PHP scripts that updates/adds/removes data from the DB, and make another script that retrieves the required data from the DB. 创建一个PHP脚本以从数据库中更新/添加/删除数据,并创建另一个脚本以从DB中检索所需的数据。 You can call both kind of scripts as much as you like with AJAX, and keep using the retrieve-data script (again called with AJAX) to display the updated data. 您可以使用AJAX随意调用两种脚本,并继续使用检索数据脚本(再次使用AJAX调用)来显示更新的数据。

you can make a jquery ajax call for deleting a row and refresh the datatables to show proper pagination and proper records. 您可以进行jquery ajax调用来删除行并刷新数据表以显示正确的分页和正确的记录。 I ve searched many methods to refresh data table but the function which i finally made use of does everything you want. 我已经搜索了许多刷新数据表的方法,但最终使用的功能可以满足您的所有需求。 function takes the row no. 函数采用行号。 as the input to perform delete row operation and refresh data table. 作为执行删除行操作和刷新数据表的输入。 This is what i did. 这就是我所做的。

while creating table in for loop. 在for循环中创建表时。 assign no. 分配编号 from 0 onwards as id of tr. 从0开始,作为tr的ID。 > >

<script>
    table = $('#table_id').datatables();
    $(delete_row).click(function(){
     ..
     var row_no=$(this).closest("tr").attr("id");
     $.ajax({
        ...
        success : function(data) {
        ...
        table.fnDeleteRow(row_no);
     }
     });
     });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM