0

I am writing a WP-Plugin and now I would like to have a table view of the data in my DB and be able to delete an entry on Button-Click. So what I am trying to do:

--------------------------------------------
#    1    #     Foo      #     Delete      #
--------------------------------------------
#    2    #     Boo      #     Delete      #
--------------------------------------------

I am receiving all of my data from an sql-command executed via $wpdb. This works perfectly fine (and creates the table as I wanted it to do. When the delete Button/Text is pressed, I want the corresponding entry to be deleted (e.g. entry with name "Boo"). Sadly I have no idea how to access that special entry. I provided my code below. Does anyone know how I can access the element/data in the line where the corresponding "delete"-Button is? It would be great if I could define what arguments to pass in the "POST" of the table entries, since I can access the PK of the entry in my database.

<?php

include_once WP_CONTENT_DIR . '/DBService/DBInteractorService.php';
include_once WP_CONTENT_DIR . '/DBService/DBInteractionUtils.php';

class tp_clubView {

    public $_tableData;
    public $_dbInteractorService;
    public $_tableHTML;

    public function __construct() {
        $this->init_process();
    }

    private function init_process() {
        $this->$_dbInteractorService = DBInteractorService::getInstance();
        $this->refreshTableData();
        if (isset($_POST['dropEntry'])) {
            echo($_POST['dropEntry'];
            //what to do here?
        }
    }

    private function refreshTableData() {
        $mySQL= DBInteractionUtils::$_mySQL;
        $this->$_tableData = $this->$_dbInteractorService->executeSelectStatement($mySQL);
        $this->refreshTableEntries();
    }

    private function refreshTableEntries() {
        $counter = 1;
        $tableLayout = '<div>
            <form name="form" action="' . $_SERVER['REQUEST_URI'] . '" method="post">
                <table class="widefat myTable">
                    <thead>
                        <tr>
                            <th> Number </th>
                            <th> Name </th> 
                            <th> Action </th> 
                        </tr>
                    </thead>
                        ';
        $dataSet = $this->$_tableData;
        foreach ($dataSet as $tableSet) {
            $tableLayout = $tableLayout . '<tr>' . 

        '<td>' . $counter . '</td>' . '<td>' . $tableSet->name . '</td>' . 
        '<td><input type="hidden" name="dropEntry" value="' . $tableSet->id .'">' .
        '<button type="submit">Delete</button>'
        . '</input>' . '</tr>';
            $counter = $counter + 1;
        }

        $tableEnd = '</table>
            </form>
        </div>';

        $tableLayout = $tableLayout . $tableEnd;
        $this->$tableHTML = $tableLayout;
    }

    public function createClubsTableDiv() {
        return '<div value='. $this->$_tableHTML;
    }

}
?>

Edit:

I could get data passed, sadly it is only the ID of the last entry passed (the last ID inserted). Can I somehow save the current ID for each entry?

3
  • Why not using wp_list_table ? here is a nice example..check this one github.com/pmbaldha/WP-Custom-List-Table-With-Database-Example/… Commented Jun 3, 2018 at 16:31
  • @SabbirHasan Thanks for the tip, but I am not really sure if that would help me out with my problem, since it seems to create a table based on an SQL-Statement and perform actions on these based on attributes or rownum which is pretty much what I am doing with HTML/PHP in this example. Like in my example I am still facing the problem of "how to bind a specific ID to an Element and pass it on click". Everything to do before/afterwards is no big deal I guess. Or is there anything I am not seeing yet?
    – Rüdiger
    Commented Jun 3, 2018 at 16:51
  • check my answer bellow. If you eager to share your code with me I'll be happy to help you. It's not easy to debug plugin issue without live testing. Commented Jun 4, 2018 at 18:10

1 Answer 1

0

Try making your foreach loop like this one and let me know the result. It should solve your it is only the ID of the last entry passed (the last ID inserted) issue

foreach ($dataSet as $tableSet) {
        $tableLayout .= '<tr>' . '<td>' . $counter . '</td>' . '<td>' . $tableSet->name . '</td>' . 
    '<td><input type="hidden" name="dropEntry" value="' . $tableSet->id .'">' .
    '<button type="submit">Delete</button>'
    . '</input>' . '</tr>';
    $counter = $counter + 1;
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.