Custom select

Custom select

vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
edited September 2012 in Editor
Hi,
In join table php , i could see a code that get an array for lable and value for drop down menu. This is as follow
[code]if ( !isset($_POST['action']) ) {
// Get department details


$out['access'] = $db
->select( 'access', 'id as value, name as label' )///select id and name from ALL rows of 'access' table.
->fetchAll();
}
[/code]
I have to restrict the selection with a where condition ,For example -to select id and name from those rows of 'access' table where id<3 not all as given in this example.
What modification I have to do here in this code. Hope @Allen will not ignore my request.

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Sounds like you want the `where` method: http://editor.datatables.net/docs/current/php/class-DataTables.Editor.html#_where :-)

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Thank you Allen , You are right, i m searching for WHERE.

    Yeah i have seen it, but not working for me.
    [code] $where=where( $key = 'id', $value = '3', $op = '=' );
    $out['access'] = $db
    ->select( 'access', 'id as value, name as label',$where )
    ->fetchAll();[/code]
    what is wrong with this?
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    edited September 2012
    What is the `where` function you are using? I'd be surprised if that isn't generating a PHP error. Is it not?

    The third parameter of the `select` method is an array ( http://editor.datatables.net/docs/current/php/class-DataTables.Database.html#_select ). It looks like you just need to change first line to be `$where = array( ... );` .

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Thank you Allen,
    I tried lot of combinations to include the third parameter in SELECT method as an array but ended up with json errors.
    Hence I used simple php direct solution

    if ( !isset($_POST['action']) ) {


    $accessList = $db->select( 'access', 'id, name' );

    $out['accessList'] = array();
    while ( $row = $accessList->fetch() ) {
    if($row['id']>=3)///used this condition here to limit the array out put .
    {
    $out['accessList'][] = array(
    "value" => $row['id'],
    "label" => $row['name']
    );}
    }
    }

    It works well. Thank you
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Allen, Can you please tell me how to filter the table, using default parameter, for any particular column.
    Suppose I want to display the rows in which browser is Firefox(as in example). What is the pre-filtering method I should use in data table.
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Looking at the where code in detail, I think there might be a bug in there, as that should have worked. However, there is another form that you can use for the where condition, using an associative array:

    [code]
    $out['dept'] = $db
    ->select( 'dept', 'id as value, name as label', array('id'=>'3') )
    ->fetchAll();
    [/code]

    That should do the job nicely.

    > Allen, Can you please tell me how to filter the table, using default parameter, for any particular column.

    Use oSearch for the global search and aoSearchCols for columns.

    Allan
  • vinod_ccvvinod_ccv Posts: 75Questions: 0Answers: 0
    Hi Allan,

    if ( !isset($_POST['action']) ) {


    $accessList = $db->select( 'access', 'id, name,dept_id' );

    $out['accessList'] = array();
    while ( $row = $accessList->fetch() ) {
    if($row['dept_id']==_________________________)///comparing the dept id is derived from table
    {
    $out['accessList'][] = array(
    "id" => $row['id'],
    "name" => $row['name']
    "dept_name" => _________________________///dept_name is value from joined table
    );}
    }
    }
    How can i use Join instance in this case. Is it possible?
This discussion has been closed.