wp admin – How to filter with dropdown in backend for custom data

Question

I have a custom table (Table Name: ssl_transaction_cus) in the database which is used for storing donation information. I fetched all of data from this table in ‘admin menu page’ with a table(With Table tag). I would like to be able to filter those data according to ‘Event Name’.

I am trying to according to this answer . But, there are code for custom post or post. But, I have no CPT for those data, just custom data. I am trying for long time. But could not success.

enter image description here

Here is my code for fetching data.

function my_menu_donation_new() {
add_menu_page('Transaction-donation-page-main', 'Manage Transaction (Donation)', 'manage_options', 'transaction-donation-menu', 'my_options_doanation_new');
  function my_options_doanation_new(){

      global $wpdb;
      $result = $wpdb->get_results( "SELECT * FROM ssl_transaction_cus_donate");  

      echo"<div><h1>Transaction for Donation</h1><table class="quote" style="border: 1px solid #014d6d; width:100%">";
      echo"<tr><th style="border:1px solid #014d6d;">Sl.</th><th style="border:1px solid #014d6d;">Name</th><th style="border:1px solid #014d6d;">Email</th><th style="border:1px solid #014d6d;">Phone No</th><th style="border:1px solid #014d6d;">Event Name</th><th style="border:1px solid #014d6d;">Trans. Date</th><th style="border:1px solid #014d6d;">Trans. Id</th><th style="border:1px solid #014d6d;">Amount</th><th style="border:1px solid #014d6d;">Card Type</th></tr>";
    
      $i=1;
      foreach ( $result as $res ) {
          echo "<tr style="border:1px solid #014d6d;">";
          echo "<td>".$i."</td>"."<td>".$res->Name."</td>"."<td>".$res->Email."</td>"."<td>".$res->PhoneNo."</td>"."<td>".$res->EventName."</td>"."<td>".$res->TransactionDate."</td>"."<td>".$res->TransactionId."</td>"."<td>".$res->Amount."</td>"."<td>".$res->CardType."</td>";

          echo "</tr>";
          $i++;
          }       
      echo"<tr><th style="border:1px solid #014d6d;">Sl.</th><th style="border:1px solid #014d6d;">Name</th><th style="border:1px solid #014d6d;">Email</th><th style="border:1px solid #014d6d;">Phone No</th><th style="border:1px solid #014d6d;">Event Name</th><th style="border:1px solid #014d6d;">Trans. Date</th><th style="border:1px solid #014d6d;">Trans. Id</th><th style="border:1px solid #014d6d;">Amount</th><th style="border:1px solid #014d6d;">Card Type</th></tr>";
      echo "</table></div>";
      }

}

add_action(‘admin_menu’, ‘my_menu_donation_new’);

And here is the code for filtering. I’m sure I wrote the wrong code. Can anybody help me? Thanks.

add_action( 'restrict_manage_posts', function(){
     global $wpdb, $pagenow;
     
         $values = array();
         $EventNameQuery = $wpdb->get_results("SELECT EventName from ssl_transaction_cus");

          foreach ($EventNameQuery as &$data){
             $values[$data->EventName] = $data->EventName;
          }
        
         ?> <select name="admin_event_name">
                <option value="">Event Name</option>

                <?php 
                $current_v = isset($_GET['admin_event_name'])? $_GET['admin_event_name'] : '';
                foreach ($values as $label => $value) {
                    printf(
                        '<option value="%s"%s>%s</option>',
                        $value,
                        $value == $current_v? ' selected="selected"':'',
                        $label
                    );
                }
                ?>
            </select>
         <?php
     
 }); 
 
 add_filter( 'parse_query', function($query){
     global $pagenow;
     if ($pagenow=='admin.php?page=transaction-donation-menu' && isset($_GET['admin_event_name']) && !empty($_GET['admin_event_name'])) {
     }
 }); 

0
Kaizur 1 month 2022-08-15T00:14:02-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse