Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author ka2

    (@ka2)

    Thank you for your inquiry.

    It will be possible to do by following.

    1. Firstly, it’ll add a filter into 909th line of plugin core class (lib/cdbt.class.php).

    $where_clause = apply_filters( 'cdbt_get_data_conditions', $where_clause, $table_name, $conditions );

    2. Next, please enabled a filter by “functions.php” of your theme into the user access page (post to use the shortcode cdbt-view).

    function custom_get_data_conditions( $where_clause, $table_name, $conditions ){
      $current_user = wp_get_current_user();
    
      if ('your_created_table' === $table_name) {
        if (empty($conditions)) {
          $where_clause = sprintf( "WHERE userID = %d ", $current_user->ID);
        } else {
          $where_clause .= sprintf( "AND userID = %d ", $current_user->ID);
        }
      }
      return $where_clause;
    }
    add_filter( 'cdbt_get_data_conditions', 'custom_get_data_conditions', 10, 3);

    Please try by all means.
    Thank you,

    Thread Starter alsayyed

    (@alsayyed)

    Hi I tried but nothing changed.

    I placed the where clause to 909th line, and add the filter in the Function , but nothing changed.

    Plugin Author ka2

    (@ka2)

    Hmm…that’s troubling, isn’t it.

    Please confirm one point.
    Do you specify a table name that you want to filter the table name in the condition of “if” statement on the source that you have added to “functions.php” in your theme?

    If there is no problem in the above table name specification, please let me know the results of the debug code below.

    function custom_get_data_conditions( $where_clause, $table_name, $conditions ){
      $current_user = wp_get_current_user();
    
      if ('your_created_table' === $table_name) {
        if (empty($conditions)) {
          $where_clause = sprintf( "WHERE userID = %d ", $current_user->ID);
        } else {
          $where_clause .= sprintf( "AND userID = %d ", $current_user->ID);
        }
      }
      var_dump($where_clause);
      return $where_clause;
    }
    add_filter( 'cdbt_get_data_conditions', 'custom_get_data_conditions', 10, 3);

    By this code, I have put a debugging code at “var_dump” to display the variable of $where_clause before that is returned.

    Thank you,

    Thread Starter alsayyed

    (@alsayyed)

    I dont see any changes, where can i see the debugging code result ?

    Is there a way to contact you privately ?

    Thread Starter alsayyed

    (@alsayyed)

    I want to make sure is this the place where i place the filter in the 909th line in cdbt.class ?

    if (!empty($client_host)) {
    
    					list($client_addr, ) = gethostbynamel($client_host);
    				} else {
    					$where_clause = apply_filters( 'cdbt_get_data_conditions', $where_clause, $table_name, $conditions );
    
    					$client_addr = $_SERVER['SERVER_ADDR'];
    
    				}
    Plugin Author ka2

    (@ka2)

    Oh, It differences is insertion place of the filter.
    Did you use plugin versions 1.1.15?

    The filter needs to be added to the get_data within the method of cdbt.class.php.
    The right place is as follows:

    function get_data($table_name, $columns='*', $conditions=null, $order=array('created'=>'desc'), $limit=null, $offset=null) {
    	global $wpdb;
    	list(, , $table_schema) = $this->get_table_schema($table_name);
    	$select_clause = is_array($columns) ? implode(',', $columns) : (!empty($columns) ? $columns : '*');
    	$where_clause = $order_clause = $limit_clause = null;
    	if (!empty($conditions)) {
    		$i = 0;
    		foreach ($conditions as $key => $val) {
    			if (array_key_exists($key, $table_schema)) {
    				if ($i == 0) {
    					$where_clause = "WHERE <code>$key</code> = '$val' ";
    				} else {
    					$where_clause .= "AND <code>$key</code> = '$val' ";
    				}
    				$i++;
    			} else {
    				continue;
    			}
    		}
    	}
    $where_clause = apply_filters( 'cdbt_get_data_conditions', $where_clause, $table_name, $conditions );
    	if (!empty($order)) {
    		$i = 0;
    		foreach ($order as $key => $val) {
    			if (array_key_exists($key, $table_schema)) {
    				$val = strtoupper($val) == 'DESC' ? 'DESC' : 'ASC';
    				if ($i == 0) {
    					$order_clause = "ORDER BY <code>$key</code> $val ";
    				} else {
    					$order_clause .= ", <code>$key</code> $val ";
    				}
    				$i++;
    			} else {
    				continue;
    			}
    		}
    	}
    	if (!empty($limit)) {
    		$limit_clause = "LIMIT ";
    		$limit_clause .= (!empty($offset)) ? intval($offset) .', '. intval($limit) : intval($limit);
    	}
    	$sql = sprintf(
    		"SELECT %s FROM <code>%s</code> %s %s %s",
    		$select_clause,
    		$table_name,
    		$where_clause,
    		$order_clause,
    		$limit_clause
    	);
    	return $wpdb->get_results($sql);
    }

    After correct the position of the filter, please try again.

    Thread Starter alsayyed

    (@alsayyed)

    Thank you Very Very Much, It is working Now ??

    Thread Starter alsayyed

    (@alsayyed)

    Now in insert data, how to get the userID of the current user and insert it in the userID field automatically?

    How can this be done in Version: 2.0.12?

    Thanks!

    Plugin Author ka2

    (@ka2)

    In the latest version, it is as follows.

    e.g. shortcode:

    [cdbt-view table="your_created_table"]

    e.g. filter hook (as like “functions.php” in your theme):

    function custom_filter_get_data_sql( $sql, $table_name, $sql_clauses ) {
      if ( ! is_admin() && 'your_created_table' === $table_name ) {
        $_current_user_id = 0; // For guest user
        if ( is_user_logged_in() ) {
          $current_user = wp_get_current_user();
          $_current_user_id = $current_user->ID;
        }
        $_new_sql = <<<SQL
    SELECT %s
    FROM %s
    WHERE user_id=%s
    %s %s
    SQL;
    	$sql = sprintf( $_new_sql, $sql_clauses[0], $table_name, $_current_user_id, $sql_clauses[2], $sql_clauses[3] );
      }
      return $sql;
    }
    add_filter( 'cdbt_crud_get_data_sql', 'custom_filter_get_data_sql', 10, 3 );

    Thank you,

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘show data table by user id’ is closed to new replies.