Filtering... What am I missing?

I’m having a time trying to understand the filter documentation for the connector. I need to populate a grid with rows but only where the ‘uid’ field is some digit. I’ll use 2 for the uid in the example. I must have the filtering occur on the server. It must be untouchable by the client.

Will someone please advise on how to do this?

Here’s the connector php code I came up with …It doesn’t work and throws an exception:

            ....
	$gridConn = new GridConnector($this->db,"MySQL"); 
	global $_log_dir;
	$gridConn->enable_log($_log_dir . "/deployments_dhtmx.out");
	$gridConn->dynamic_loading(10);
	
	$gridConn->event->attach("beforeFilter","user_filter");

	$gridConn->render_table('deployments','id','uid,searchengine,id,ysm_customerid,onBehalfOfUsername,onBehalfOfPassword,google_customerid,google_accountid');
	
}
function user_filter($filter_by){
		$filter_by->add('uid',$this->uid,'=');
}

…never mind, I figured it out.

Can anyone please post the answer that he figured out?
I need to see it also.

Nevermind, I think I must change connector.php, to render_sql instead of render_table:

$grid->render_table(“project_list”,“project_id”,“project_name,domain_desc,user_id”);

becomes instead

$grid->render_sql(“Select * from project_list where project_type=‘2’”,“project_id”,“project_name,user_id”);

Not quite sure, but original code has a problem with “$this->uid” which will not point to the usefull data. All other looks correct

  • filtering event attached
  • from filtering event default filter is added

I think I must change connector.php, to render_sql
Yep, it is one of possible alternatives.

I didn’t understand still, how to use render_table.
Could you post how a code would be written for selecting only certain rows in table?

I am trying to select certain rows from table, and then add some defaults values, like so:

Table

1   Book            $2
2   Book            $3
3   Calendar        $2
4   Calendar        $3

Output a grid, that shows only Books, but has default Quantity 1:

Quantity   Name    Price

1           Book       $2
1           Book       $3
$grid->render_sql("SELECT * FROM mytable WHERE name='Book'","id","Quantity,Name,Price")

or

$grid->event->attach("beforeFilter", function($filters){
    $filters->add("name","Book","=");
})
$grid->render_sql("Book'","id","Quantity,Name,Price");

How will this add a default quantity of 1, if that quantity is not in the database?

Sorry, somehow I have missed the original criteria :blush:

$grid->render_sql("SELECT * FROM mytable WHERE Quantity  =1 ","id","Quantity,Name,Price")

or

$grid->event->attach("beforeFilter", function($filters){ $filters->add("Quantity","1","="); }) $grid->render_sql("Book'","id","Quantity,Name,Price");

I’m sorry, I am unclear.

If your database is like this:

Book1 $1.50 Book2 $1.75 Book3 $1.90 Book4 $1.60

Now say you want to drag from the Books Grid to a Shopping Cart Grid. Your Shopping Cart has:

Quantity   Book   Price

But your Book Grid had only two columns. You want to set the default Quantity to be 1. I tried to make invisible column in the Book Grid, and “1” inside the invisible column.

$grid->render_sql("Select * from books where itemtype='hardback'","book_id","book_name,book_price","1");

The “1” doesn’t populate the invisible column. Can you suggest a way? Do I have to go through and add “1” to the entire books database table?

Even better would be if I can retrieve the “1” from another table that’s user_preferences, even though the two tables share no other common traits.

Thanks!

Change your code as

$grid->render_sql("Select *, 1 as quantity from books where itemtype='hardback'","book_id","book_name,book_price,quantity");

Beware that in such way it will not be good for saving ( because it will not be able to save quantity field )