dongsui8162 2018-06-21 13:55
浏览 58

使选项表wordpress中的输出数组值生成列表表

class Kv_subscribers_list extends WP_List_Table {

function __construct(){
    global $status, $page;                

    parent::__construct( array(
        'singular'  => 'option',  
        'plural'    => 'options',   
        'ajax'      => false      
    ) );        
}


function column_default($item, $column_name){
    switch($column_name){
        case 'option_id':
        case 'option_name':
        case 'option_value': 


            return $item[$column_name];
        default:
            return print_r($item,true); //Show the whole array for troubleshooting purposes
    }
}


function column_cb($item){
    return sprintf(
        '<input type="checkbox" name="%1$s[]" value="%2$s" />',
        /*$1%s*/ $this->_args['singular'],  
        /*$2%s*/ $item['option_id']                
    );
}

function get_columns(){
    $columns = array(
        'cb'        => '<input type="checkbox" />', 
        'option_id'=>__('الرقم'),  
        'option_name'=>__('المصدر'),  
        'option_value'=>__('النقطة'),

            );
    return $columns;
}
public function get_sortable_columns() {
    $sortable_columns = array(
        'option_id'   => array('option_id',true),
        'option_name'   => array('option_name',true)
            );
    return $sortable_columns;
}
public function get_bulk_actions() {
    $actions = array(
        'delete'    => 'حذÙ',
        'email'     => 'Email'
    );
    return $actions;
}

public function process_bulk_action() {

    global $wpdb; 
    $options_tbl = $wpdb->prefix.'options';

    if( 'delete'===$this->current_action() ) {
        foreach($_POST['option'] as $single_val){
            $wpdb->delete( $options_tbl, array( 'option_id' =>    (int)$single_val ) );             

        }
        $redirect_url =  get_admin_url( null, 'admin.php?page=options' );
        wp_safe_redirect($redirect_url); 
        wp_die('Items deleted (or they would be if we had items to delete)!'); 
    } 
    if( 'email'===$this->current_action() ) {           
        $result_email_ar = implode("-",$_POST['option']);
        $redirect_url =  get_admin_url( null, 'admin.php?page=kvcodes&ids='.$result_email_ar  );
        wp_safe_redirect($redirect_url);        

        wp_die('  ');
    }       
}

function prepare_items() {
    global $wpdb; //This is used only if making any database queries
    $database_name = $wpdb->prefix.'options' ;
    $per_page = 10;
    $query = "SELECT * FROM $wpdb->options WHERE option_name LIKE 'agl_data_%_%' ";

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

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

    $this->process_bulk_action();

    $data =  $wpdb->get_results($query, ARRAY_A );

    function usort_reorder($a,$b){
        $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //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
    ) );
}
}//class

function o_add_menu_items(){
    //add_menu_page('Plugin List Table', 'Sub', 'activate_plugins', 'options', 'o_render_list_page');
    add_menu_page( 'تقرير التتبع', 'تقرير التتبع', 'manage_options', 'options', 'o_render_list_page', 'dashicons-welcome-widgets-menus', 81 );
} 
add_action('admin_menu', 'o_add_menu_items');



    function ns_contact_form_csv_pull() {

       global $wpdb;

       $table = 'ns_contact_form';// table name
       $file = 'سجلات_التتبع_csv'; // csv file name
       $results = $wpdb->get_results("SELECT * FROM $wpdb->options WHERE option_name LIKE 'agl_data_%_%' ",ARRAY_A );

       if(count($results) > 0){
          foreach($results as $result){
          $result = array_values($result);
          $result = implode(", ", $result);
          $csv_output .= $result."
";
        }
      }

      $filename = $file."_".date("Y-m-d_H-i",time());
      header("Content-type: application/vnd.ms-excel");
      header("Content-disposition: csv" . date("Y-m-d") . ".csv");
      header( "Content-disposition: filename=".$filename.".csv");
      print $csv_output;
      exit;

    }
   add_action('wp_ajax_csv_pull','ns_contact_form_csv_pull');

function o_render_list_page() {
    $mydownloads = new Kv_subscribers_list();
    $title = __("سجلات نظام التتبع","my_plugin_slug");
    ?>
    <div class="wrap">
        <h1>
            <?php echo esc_html( $title );?>
             <!-- Check edit permissions -->
             <a href="<?php echo admin_url( 'admin-ajax.php?action=csv_pull' ); ?>" class="page-title-action">
                <?php echo esc_html_x('تصدير كاÙØ© البيانات', 'my-plugin-slug'); ?>
            </a>
            <?php
            ?>
        </h1>
    </div>
    <?php $mydownloads->views(); ?>
    <form method="post">
    <?php 

        $mydownloads->prepare_items(); 
        $mydownloads->display();
    ?>
    </form>
<?php
}

above is code that filter an data stores in options table

option_value field is contain an array data

example :

a:14:{s:8:"latitude";s:10:"51.5841086";s:9:"longitude";s:17:"89.21324279999999";s:8:"altitude";s:3:"NaN";s:8:"accuracy";s:4:"1612";s:16:"altitudeAccuracy";s:3:"NaN";s:7:"heading";s:3:"NaN";s:5:"speed";s:3:"NaN";s:10:"error_code";s:0:"";s:13:"error_message";s:0:"";s:8:"php_time";i:1529454899;s:8:"php_date";s:19:"2018-06-20 00:34:59";s:15:"php_date_format";s:11:"Y-m-d H:i:s";s:7:"user_id";s:1:"0";s:9:"user_mail";s:1:"0";}

ineed to get this option_value array and make a table of the variable

column header = altitude| longitude| altitude

value = altitude => 21.5841086| longitude => 39.21324279999999| altitude => NaN

please some one help me to adjust my code above to fit data in array column header and row data

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号