Moishy
Moishy

Reputation: 3648

Delete row wordpress custom database table

I have a page showing all transactions made via paypal on my site. I want to add a button that could delete that specific row.

The transaction id is unique.

how would I be able to when user clicks on the button to delete just that row? also to run a jquery alert to confirm delete?

my code:

<table class"widefat">
<?php  global $wpdb;
    $result = $wpdb->get_results ( "SELECT * FROM wp_donations" );
    if (!$result){?>
    <tr>
    <td>There are no donations to show</td>
    </tr>
<?php }else{?> 
<thead>
<tr>
 <th>Name</th>
 <th>Email</th>
 <th>Phone Number</th>
 <th>Address</th>
 <th>Amount</th>
 <th>Method</th>
 <th>Date</th>
 <th>Transaction ID</th>
 <th></th>
</tr>
</thead>
  <?php foreach ( $result as $print )   {?>
    <tr>
    <td><?php echo $print->name;?></td>
    <td><?php echo $print->email;?></td>
    <td><?php $phones = $print->phone; if($phones == 0){echo 'No Number Provided';}else{echo $phones;}?></td>
    <td><?php echo $print->address;?></td>
    <td>$<?php echo $print->amount;?></td>
    <td><?php echo $print->method;?></td>
    <td><?php echo $print->dates;?></td>
    <td><?php echo $print->txid;?></td>
    <td><input type="button" id="<?php echo $print->txid;?>" class="delete" title="Delete" value="delete" /></td>
    </tr>
        <?php }?>
       </table>

Upvotes: 1

Views: 3187

Answers (2)

Moishy
Moishy

Reputation: 3648

I ended up running an action. using the unique ID that is AUTO_INCREMENT in the database.

and then called the action on page load.

$actions = array(

            'delete' => sprintf('<a href="?page=%s&action=delete&id=%s">%s</a>', $_REQUEST['page'], $item['id'], __('Delete', 'custom_table_example')),
        );

   function process_bulk_action()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . 'donations'; // do not forget about tables prefix

        if ('delete' === $this->current_action()) {
            $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
            if (is_array($ids)) $ids = implode(',', $ids);

            if (!empty($ids)) {
                $wpdb->query("DELETE FROM $table_name WHERE id IN($ids)");
            }
        }
    }

Upvotes: 1

ProllyGeek
ProllyGeek

Reputation: 15846

If jquery is an option , then it is very simple :

$(".delete").on('click',function(){
$(this).parents("tr").remove();
})

check this

Upvotes: 2

Related Questions