使用WP_List_Table对Wordpress中的表进行排序将数字列排序为文本

时间:2015-04-24 17:32:07

标签: php wordpress sorting

我正在使用WP_List_Table来创建自定义插件。我正在使用从其他源复制的代码,一切正常,除非根据ID列或订单号列对表进行排序,这是在我的SQL数据库中定义的“int”。

这是继承WP_List_Table的类代码类:

 <?php


if(!class_exists('WP_List_Table')){
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class TT_Example_List_Table extends WP_List_Table {
    function __construct(){
        global $status, $page;

        //Set parent defaults
        parent::__construct( array(
        'singular'  => 'movie',     //singular name of the listed records
        'plural'    => 'movies',    //plural name of the listed records
        'ajax'      => false        //does this table support ajax?
       ) );       
    }
    function column_default($item, $column_name){
    switch($column_name){
        case 'id':
        case 'name_english':
        case 'name_arabic':
        case 'ordering_number':
            return $item[$column_name];
        default:
            return print_r($item,true); //Show the whole array for troubleshooting purposes
        }
    }
    function column_id($item){    
    //Build row actions
        $actions = array(
            'edit'      => sprintf('<a href="?page=%s&action=%s&movie=%s">Edit</a>',$_REQUEST['page'],'edit',$item['id']),
            'delete'    => sprintf('<a href="?page=%s&action=%s&movie=%s">Delete</a>',$_REQUEST['page'],'delete',$item['id']),
        );
        //Return the title contents
        return sprintf('%1$s <span style="color:silver">%2$s</span>%3$s',
        /*$1%s*/ $item['id'],
        /*$2%s*/ $item['ID'],
        /*$3%s*/ $this->row_actions($actions)
        );
    }
    function column_cb($item){
        return sprintf(
            '<input type="checkbox" name="%1$s[]" value="%2$s" />',
            /*$1%s*/ $this->_args['singular'],  //Let's simply repurpose the table's singular label ("movie")
            /*$2%s*/ $item['id']                //The value of the checkbox should be the record's id
        );
    }
    function get_columns(){
        $columns = array(
        'cb'        => '<input type="checkbox" />', //Render a checkbox instead of text
        'id'     => 'ID',
        'name_english'    => 'English Name',
        'name_arabic'  => 'Arabic Name',
        'ordering_number' => 'Order Number'
        );
        return $columns;
    }
function get_sortable_columns() {
    $sortable_columns = array(
        'id'     => array('id',true),     //true means it's already sorted
        'name_english'    => array('name_english',true),
        'name_arabic'  => array('name_arabic',true),
        'ordering_number'  => array('ordering_number',true)
    );
        return $sortable_columns;
    }
    function get_bulk_actions() {
    $actions = array(
        'delete'    => 'Delete'
    );
    return $actions;
    }
    function process_bulk_action() {

    //Detect when a delete action is being triggered...
    if( 'delete'===$this->current_action() ) {

       if( is_array ($_GET['movie'])) {
              foreach ($_GET['movie'] as $value) {
             global $wpdb;
               $wpdb->query($wpdb->prepare ("DELETE FROM " .$GLOBALS['categoriestables']. " WHERE id = " .$value));
                    if (file_exists(plugin_dir_path( __FILE__ ) . 'appfiles/images/categories/' . $value . '.jpg' )) {
                        unlink(plugin_dir_path( __FILE__ ) . 'appfiles/images/categories/' . $value . '.jpg');
                        }
                 }
               echo '<div class="updated">  <p>Many Categories have been deleted.</p></div>';

           } else{
             global $wpdb;
            $wpdb->query($wpdb->prepare ("DELETE FROM " .$GLOBALS['categoriestables']. " WHERE id = " .$_GET['movie']));
                   if (file_exists(plugin_dir_path( __FILE__ ) . 'appfiles/images/categories/' . $_GET['movie'] . '.jpg' )) {
                        unlink(plugin_dir_path( __FILE__ ) . 'appfiles/images/categories/' . $_GET['movie'] . '.jpg');
                        }
               echo '<div class="updated">  <p>One Category has been deleted.</p></div>';
           }
 }

    //Detect when a edit action is being triggered...
      if( 'edit'===$this->current_action() ) {
            global $wpdb;
            $english_name = $wpdb->get_var( $wpdb->prepare("select name_english from " .$GLOBALS['categoriestables']. " where id= " .$_GET['movie']) );
            $arabic_name = $wpdb->get_var( $wpdb->prepare("select name_arabic from " .$GLOBALS['categoriestables']. " where id= " .$_GET['movie']) );
            $order_number = $wpdb->get_var( $wpdb->prepare("select ordering_number from " .$GLOBALS['categoriestables']. " where id= " .$_GET['movie']) );
         echo "<h1> Edit Category </h1><br><br><br><br><br>"; ?> 

<form class="cp-club-form" method="POST" enctype="multipart/form-data" action="<?PHP echo admin_url( 'admin.php?page=categories-tropicana-plugin&action=editing' ); ?>">

             <table style="width:50%">
           <tr> <td>Category ID:</td> <td><input name="editID" type="text" value='<?PHP echo $_GET['movie']; ?>' readonly> </td> <td> Do not Touch</td></tr>
           <tr> <td>English Name:</td> <td><input name="english_name" type="text" value='<?PHP echo $english_name; ?>'> </td> <td> First Language</td></tr> 
           <tr> <td>Arabic Name:</td> <td><input name="arabic_name" type="text" value='<?PHP echo $arabic_name; ?>'> </td><td> Second Language</td></tr>
           <tr> <td>Order Number:</td> <td><input name="order_number" type="number" value='<?PHP echo $order_number; ?>'> </td><td> Its Order in appearance in the mobile app</td></tr>
<tr><td>Assigned Image</td><td> <img src="<?php echo plugins_url(); ?>/tropicana-delivery/appfiles/images/categories/<?php echo $_GET['movie'] ?>.jpg" alt="Category Image" style="width:150px;"> </td><td>Best Dimensions: </td></tr>
 <tr> <td></td><td><input type="file" name="file" id="file"></td><td> <input type="submit" name="submitediting" value='Save'> </td>    
         </table>
 </form>
         <?php
        wpdie();
        }
 // Detect if editing is saved & done
  if ('editing'===$this->current_action()) {
      if(isset($_POST['submitediting'])) {

         if ($_FILES['file']['size'] != 0) {
        // Check filetype
         if($_FILES['file']['type'] != 'image/jpeg'){
               die('Unsupported filetype uploaded. Only JPEG is allowed for Mobile Technical Reasons');
            }
             // Upload file
           if(!move_uploaded_file($_FILES['file']['tmp_name'], plugin_dir_path( __FILE__ ) . 'appfiles/images/categories/' . $_POST['editID'] . '.jpg')){
            die('Error uploading file - check destination is writeable.!!!');
             }
          }
         global $wpdb;
        $wpdb->query($wpdb->prepare ("UPDATE " .$GLOBALS['categoriestables']. " SET name_english= '".$_POST['english_name']."', name_arabic = '".   $_POST['arabic_name']."', ordering_number= ".$_POST['order_number']." WHERE id = " .$_POST['editID']));
          echo '<div class="updated">  <p>Edited Successfully</p></div>';
      }}
// If Add new Button is Pressed
  if ('new'===$this->current_action()) {
       echo "<h1> Add New Category </h1><br><br><br><br><br>"; ?> 

  <form class="cp-club-form" method="POST" enctype="multipart/form-data" action="<?PHP echo admin_url( 'admin.php?page=categories-tropicana-plugin&action=newadded' ); ?>">

             <table style="width:50%">
           <tr> <td>Category ID:</td> <td><input name="editID" type="text" value='Automatic' readonly> </td> <td> Do not Touch</td></tr>
           <tr> <td>English Name:</td> <td><input name="english_name" type="text" value=''> </td> <td> First Language</td></tr> 
           <tr> <td>Arabic Name:</td> <td><input name="arabic_name" type="text" value=''> </td><td> Second Language</td></tr>
           <tr> <td>Order Number:</td> <td><input name="order_number" type="number" value=''> </td><td> Its Order in appearance in the mobile app</td></tr>
           <tr><td>Assigned Image</td><td>  </td><td>Best Dimensions: </td></tr>
           <tr> <td></td><td><input type="file" name="filenew" id="file"></td><td> <input type="submit" name="submitnew" value='Save'> </td>    
         </table>
         </form>
         <?php

         wpdie();
          }

    // Handling New Category insert

     if ('newadded'===$this->current_action()) {
        if(isset($_POST['submitnew'])) {

          global $wpdb;
          $wpdb->query($wpdb->prepare ("INSERT INTO " .$GLOBALS['categoriestables']. " (name_english,name_arabic,ordering_number) VALUES ( '".$_POST['english_name']."', '".$_POST['arabic_name']."', ".$_POST['order_number'].")"));

           if ($_FILES['filenew']['size'] != 0) {
          // Check filetype
         if($_FILES['filenew']['type'] != 'image/jpeg'){
               die('Unsupported filetype uploaded. Only JPEG is allowed for Mobile Technical Reasons');
            }
             // Upload file
           if(!move_uploaded_file($_FILES['filenew']['tmp_name'], plugin_dir_path( __FILE__ ) . 'appfiles/images/categories/' . $wpdb->insert_id . '.jpg')){
            die('Error uploading file - check destination is writeable.!!!');
             }
          }

        echo '<div class="updated">  <p>Added Successfully</p></div>';
       }}

      }


     function prepare_items() {
        global $wpdb; //This is used only if making any database queries

       $per_page = 10;

      $columns = $this->get_columns();
      $hidden = array();
      $sortable = $this->get_sortable_columns();

       $this->_column_headers = array($columns, $hidden, $sortable);

      $this->process_bulk_action();

      global $wpdb;
        $data = $wpdb->get_results( "SELECT * FROM " .$GLOBALS['categoriestables'], ARRAY_A);

         if( isset($_GET['s']) ){ echo $_GET['s'];
         $data = $wpdb->get_results( "SELECT * FROM " .$GLOBALS['categoriestables']. " WHERE name_english LIKE '%".$_GET['s']."%' OR name_arabic LIKE '%".$_GET['s']."%'" , ARRAY_A);
          }


       function usort_reorder($a,$b){
          $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'id'; //If no sort, default to title
          $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
          $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
          return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
        }
      usort($data, 'usort_reorder');

      $current_page = $this->get_pagenum();

      $total_items = count($data);

       $data = array_slice($data,(($current_page-1)*$per_page),$per_page);

      $this->items = $data;

       $this->set_pagination_args( array(
        'total_items' => $total_items,                  //WE have to calculate the total number of items
        'per_page'    => $per_page,                     //WE have to determine how many items to show on a page
        'total_pages' => ceil($total_items/$per_page)   //WE have to calculate the total number of pages
       ) );
      }


      }

以下是我们如何使用此类

//Create an instance of our package class...
$testListTable = new TT_Example_List_Table();
//Fetch, prepare, sort, and filter our data...
$testListTable->prepare_items();

?>
<div class="wrap">

    <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
    <form id="movies-filter" method="get">
        <div id="icon-users" class="icon32"></div>
        <h2>Manage Your App Categories <a href="<?PHP echo admin_url( 'admin.php?page=categories-tropicana-plugin&action=new' ); ?>" class="add-new-h2"><?php echo esc_html_x('Add New', 'link'); ?></a></h2>
        <!-- For plugins, we also need to ensure that the form posts back to our current page -->
        <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
        <?php $testListTable->search_box('Search Table', 'your-element-id'); ?>
        <!-- Now we can render the completed list table -->
        <?php $testListTable->display() ?>
    </form>

 </div>
<?php

但是当我点击ID时,我会以这种方式排序行

1 10 11 12 13 2 21 ..等等。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

我找到了解决方案。只需在函数usort_reorder中用strnatcmp替换strcmp

function usort_reorder($a,$b){
  $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'id'; //If no sort, default to title
  $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
  $result = strnatcmp($a[$orderby], $b[$orderby]); //Determine sort order
  return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
}