Where condition in php script don't work /solved/ and how I can reload table with new condition

Where condition in php script don't work /solved/ and how I can reload table with new condition

VitalizVitaliz Posts: 71Questions: 7Answers: 1
edited December 2015 in Editor

I want use additonal condition for my selected data with ->where and I send variable $cur_id for this. I have read forum and found this topic https://datatables.net/forums/discussion/25876/join-tables-displaying-single-value-from-second-table#latest

Edited: This is solved. But I can`t reload table after condition was changed.

This is my code:

include( "lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

$cur_id=$_GET['my_id'];

Editor::inst( $db, 'modx_vkrzakaz','id' )
    ->fields(
        Field::inst( 'modx_vkrzakaz.no_zak' ) ,
        Field::inst( 'modx_vkrzakaz.kli_zak' )
            ->options( 'modx_vkrklient', 'id', 'fio' ),
        Field::inst( 'modx_vkrklient.fio' ),
        Field::inst( 'modx_vkrzakaz.avt_zak' )
            ->options( 'modx_vkravtor', 'id', 'name' ),
        Field::inst( 'modx_vkravtor.name' ),
        Field::inst( 'modx_vkrzakaz.vid_zak' )
            ->options( 'modx_vkrvidzak', 'id', 'vidname' ),
        Field::inst( 'modx_vkrvidzak.vidname' ),
        Field::inst( 'modx_vkrzakaz.is_step1' ),
        Field::inst( 'modx_vkrzakaz.is_step2' ),
        Field::inst( 'modx_vkrzakaz.is_step3' ),
        Field::inst( 'modx_vkrzakaz.is_step4' ),
        Field::inst( 'modx_vkrzakaz.is_step5' ),
        Field::inst( 'modx_vkrzakaz.is_step6' ),
        Field::inst( 'modx_vkrzakaz.is_step7' ),
        Field::inst( 'modx_vkrzakaz.tema_zak' ),
                Field::inst( 'modx_vkrzakaz.predpr_zak' ),
                Field::inst( 'modx_vkrzakaz.srok_zak' )
                ->validator( 'Validate::dateFormat', array(
                    "empty" => false,
                    "format" => Format::DATE_ISO_8601,
                    "message" => "Введите дату в формате гггг-мм-дд"
            ) )
            ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
                Field::inst( 'modx_vkrzakaz.sum' ),
        Field::inst( 'modx_vkrzakaz.sum_opl' ),
        Field::inst( 'modx_vkrzakaz.priority' ),
                Field::inst( 'modx_vkrzakaz.avt_list' )
    )  
        ->where( function ( $q ) use ( $cur_id ) {
 $q->where('modx_vkrzakaz.avt_zak',$cur_id); 
} )
    ->leftJoin( 'modx_vkrklient','modx_vkrklient.id', '=', 'modx_vkrzakaz.kli_zak' )
    ->leftJoin( 'modx_vkravtor' ,'modx_vkravtor.id' , '=', 'modx_vkrzakaz.avt_zak' )
    ->leftJoin( 'modx_vkrvidzak','modx_vkrvidzak.id', '=', 'modx_vkrzakaz.vid_zak' )
    ->process( $_POST )
    ->json();

Replies

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited December 2015

    I have test variant with constant in condition.

     ->where('modx_vkrzakaz.avt_zak','3')
    

    It`s work clear.

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited December 2015

    Solved: I used unnecessary quotation marks in script parameter string).
    This is the right way:

    var table2 = $('#modx_vkrzakaz').DataTable( {
            dom: 'Bfrtip',
            ajax: 'php/table.modx_vkrzakaz_sm.php?my_id='+my_id,
            columns: [ .....
    

    Next related problem found:

    I select my_id variable on Bootstrap new tab selected and
    I want update table data after my_id changed, testing this code:

     $('#avt_tabs li a').click(function (e) {
      e.preventDefault();
       my_id='3';
      $(this).tab('show');
      table2.ajax.reload();
    
    });
    

    Event is work, but table not reload data.

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited December 2015
  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited December 2015

    Yes!)
    Than I use ajax.data, this code

    table2.ajax.reload();
    

    always reload table.

    I changed my table definition:

    var table2 = $('#modx_vkrzakaz').DataTable( {
            dom: 'Bfrtip',
            ajax: {
                       url: 'php/table.modx_vkrzakaz_var.php',
                       type: "POST",
                       data: function ( d ) {
                       d.my_id = my_id;
                }
    

    and edit table.modx_vkrzakaz_var.php:

    $my_id=$_POST['my_id'];
    ......
      ->where('modx_vkrzakaz.avt_zak',$my_id)
    

    My problem is completely solved.

    Thanks, Allan.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    Nice monologue ;-). Good to hear that you have resolved the issue now. It looks like your solutions are exactly what I would have suggested.

    Regards,
    Allan

  • VitalizVitaliz Posts: 71Questions: 7Answers: 1
    edited December 2015

    Hi, Allan.

    your solutions are exactly what I would have suggested

    Yes, I use your advice from this topic:
    https://www.datatables.net/forums/discussion/30286/ajax-reload-is-not-sending-updated-params

    Thank you for advice.

    Vitaliy.

This discussion has been closed.