WordPress自定义数据库表与DataTable服务器端处理

时间:2020-10-28 08:40:37

标签: php mysql wordpress datatable

我遵循了this toturial,并设法创建了一个简短代码,该简短代码显示了带有过滤器选项的数据表。该表适用于wordpress的本机发布类型。

我需要相同的数据表,但它应该从我添加的 setData(String k, String v) async { await prefs.setString(k, v); } void _incrementCounter() async { prefs = await SharedPreferences.getInstance(); String jsonString = ...; var resBody = json.decode(jsonString); Map jsonMap = resBody[0]; jsonMap.forEach((k, v) => setData(k, v)); print(prefs.getString("first_name")); print(prefs.getString("dept_code"));

的自定义数据库表中获取数据

自定义表格: enter image description here

所以我需要更改以下函数以从上述自定义表而不是自定义帖子类型中获取数据

I/flutter ( 6692): firstname
I/flutter ( 6692): HR

1 个答案:

答案 0 :(得分:0)

因此,如果其他人正在寻找相同的解决方案,我将在此处发布。我必须添加global $wpdb并从我的自定义数据库表中获取数据。它与分页/排序/搜索完美配合。

function datatables_server_side_callback_subscriber_db() {
 
    header("Content-Type: application/json");

    global $wpdb;

    // Table name
    $table_name = $wpdb->prefix . "lubuvna_subscribers";

    // Request
    $request= $_GET;

    // Columns
    $columns = array(
        0   => 'ID',
        1   => 'first_name',
        2   => 'last_name',
        3   => 'email',
        4   => 'phone'
        
    );

    // Datatable Filters
    $column = $columns[$request['order'][0]['column']];
    $offset = $request['start'];
    $length = $request['length'];
    $order = $request['order'][0]['dir'];
    
    // Search all columns
    $sql_where = "";
    if ( !empty($request['search']['value']) ) {

        $sql_where .= "WHERE ";

        foreach ($columns as $column) {

             $sql_where .= $column . " LIKE '%" . sanitize_text_field($request['search']['value']) . "%' OR ";

        }

        $sql_where = substr($sql_where, 0, -3);
    }

    // Total Records in the datatable
    $total_table_records = "SELECT count(*) as count FROM {$table_name}";
    $total_fetched_records = $wpdb->get_results($total_table_records, OBJECT);
    $total_records = $total_fetched_records[0]->count;

    // Total Records Search
    $total_table_records_search = "SELECT count(*) as count FROM $table_name $sql_where";
    $total_fetched_records_search = $wpdb->get_results($total_table_records_search, OBJECT);
    $total_records_search = $total_fetched_records_search[0]->count;

    // Query
    $total_results = $wpdb->get_results( "SELECT * FROM $table_name $sql_where ORDER BY $column $order LIMIT $offset, $length" );

    if ( !empty($total_results) ) {
        foreach ($total_results as $row){
            $nestedData = array();
            $nestedData[] = $row->ID;
            $nestedData[] = $row->first_name;
            $nestedData[] = $row->last_name;
            $nestedData[] = $row->email;
            $nestedData[] = $row->phone;
            $data[] = $nestedData;
        }

        $json_data = array(
            "draw" => intval($request['draw']),
            "recordsTotal" => intval($total_records),
            "recordsFiltered" => intval($total_records_search),
            "data" => $data
        );
    
        echo json_encode($json_data);

    } else {

        $json_data = array(
            "data" => array()
        );
 
        echo json_encode($json_data);

    }
    
    wp_die();
 
}