setValue from SESSION variable

setValue from SESSION variable

KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

Hi Im trying to set the value of a session variable and use it in a "where condition",
I've read this https://editor.datatables.net/manual/php/conditions#Setting-field-values

but I just cant get what I want

Im doing this

include( "../../php/DataTables.php" );

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;
    
     $editor->field(
        new Field( 'userid' )
            ->setValue($_SESSION['IdUsuario'])
        );
        
    $db->sql( "SET NAMES 'utf8'" );
Editor::inst( $db, 'altaestudios', 'IdAltaEstudios')
    
           
    ->fields(
        
        Field::inst('altaestudios.FechaEstudio')
        ->validator( 'Validate::dateFormat', array(
                "format"  => Format::DATE_ISO_8601,
                "message" => "Ingrese un formato válido de fecha yyyy-mm-dd"
            ) )
            ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
    
        Field::inst('altaestudios.activo'),
            
        Field::inst('altaestudios.archivo'),
            Field::inst('archivo.web_path'),
    

          Field::inst('altaestudios.IdTipoEstudio'),
                Field::inst('tipoestudio.NombreEstudio')     

    )
    

    ->leftJoin('tipoestudio', 'altaestudios.IdTipoEstudio', '=', 'tipoestudio.IdTipoEstudio')
    ->leftJoin('archivo', 'altaestudios.archivo', '=', 'archivo.IdArchivo')
   
    ->where('altaestudios.IdUsuario','userid') // my new variable if I set a number like 63 I get results.
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

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

    You are using ->setValue($_SESSION['IdUsuario']) which is fine, but you need to include it in the Editor instance that is being used to process the inbound data:

    In fact, are you not getting an error in PHP stating that $editor doesn't exist? It isn't defined anywhere.

    Move that field definition into the fields() call at line 21.

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    Actually yes im getting an error

    So I have to do this?

        Field::inst( 'userid' )
                ->setValue($_SESSION['IdUsuario']),
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Probably need to include the altaestudios. table name as well (assuming that is the correct name), like you have with the other fields, since you are using a join.

    Thanks,
    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    Thanks for your time Allan, I really apreciate your help

    but now Im getting this error
    DataTables warning: table id=paciente - Ajax error. For more information about this error, please see http://datatables.net/tn/7

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Have you followed the diagnostic instructions provided at that link?

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    I think i did.. but not sure

    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
        
        $db->sql( "SET NAMES 'utf8'" );
    Editor::inst( $db, 'altaestudios', 'IdAltaEstudios')
        
               
        ->field(
         
                
            Field::inst('altaestudios.FechaEstudio')
            ->validator( 'Validate::dateFormat', array(
                    "format"  => Format::DATE_ISO_8601,
                    "message" => "Ingrese un formato válido de fecha yyyy-mm-dd"
                ) )
                ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
                ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
        
            Field::inst('altaestudios.activo'),
                
            Field::inst('altaestudios.archivo'),
                Field::inst('archivo.web_path'),
        
    
              Field::inst('altaestudios.IdTipoEstudio'),
                    Field::inst('tipoestudio.NombreEstudio'),
                    
             Field::inst( 'altaestudios.userid' ),
                ->setValue($_SESSION['IdUsuario'])
                
        )
        
    
        ->leftJoin('tipoestudio', 'altaestudios.IdTipoEstudio', '=', 'tipoestudio.IdTipoEstudio')
        ->leftJoin('archivo', 'altaestudios.archivo', '=', 'archivo.IdArchivo')
       
         ->where('altaestudios.IdUsuario','userid') // my new variable if I set a number like 63 I get results.
        ->process( $_POST )
        ->json();
    
    
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    What does the network tab in your browser's inspector tools show? The information in the tech note show how to do that if you aren't sure.

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    Hi allan I had an extra comma and after I removed I got this in network > xhr

    Notice: Undefined variable: _SESSION in /home/unidadgenetica/public_html/SistemaUG/genetica/scripts/r_Paciente.php on line 42

    Fatal error: Uncaught Error: Call to a member function dbField() on null in /home/unidadgenetica/public_html/SistemaUG/php/Editor/Editor.php:1827 Stack trace: #0 /home/unidadgenetica/public_html/SistemaUG/php/Editor/Editor.php(846): DataTables\Editor->_prepJoin() #1 /home/unidadgenetica/public_html/SistemaUG/php/Editor/Editor.php(661): DataTables\Editor->_process(Array) #2 /home/unidadgenetica/public_html/SistemaUG/genetica/scripts/r_Paciente.php(51): DataTables\Editor->process(Array) #3 {main} thrown in /home/unidadgenetica/public_html/SistemaUG/php/Editor/Editor.php on line 1827

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

    Notice: Undefined variable: _SESSION in

    Looks like you need to call session_start().

    Field::inst( 'altaestudios.userid' ),
    ->setValue($_SESSION['IdUsuario'])

    Was that the extra comma you removed?

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    this one

    Field::inst( 'altaestudios.userid' ), // <-- this one
    ->setValue($_SESSION['IdUsuario'])
    

    When I set the comma I get an error

    And even without comma I dont get results

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    So are you calling session_start() anywhere?

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    Hi tangerine, yes at the top

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

    When I set the comma I get an error

    Yes you would there, as that is a PHP syntax error. Remove that comma for it to be valid PHP.

    And even without comma I dont get results

    Do you get any errors, or is it just that the data is an empty array?

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    This is what i get

    [{name: ":where_0", value: "altaestudios.userid", type: null}]
    0
    :
    {name: ":where_0", value: "altaestudios.userid", type: null}
    name
    :
    ":where_0"
    type
    :
    null
    value
    :
    "altaestudios.userid"
    query
    :
    "SELECT  `altaestudios`.`IdAltaEstudios` as 'altaestudios.IdAltaEstudios', `altaestudios`.`FechaEstudio` as 'altaestudios.FechaEstudio', `altaestudios`.`activo` as 'altaestudios.activo', `altaestudios`.`archivo` as 'altaestudios.archivo', `archivo`.`web_path` as 'archivo.web_path', `altaestudios`.`IdTipoEstudio` as 'altaestudios.IdTipoEstudio', `tipoestudio`.`NombreEstudio` as 'tipoestudio.NombreEstudio', `altaestudios`.`userid` as 'altaestudios.userid' FROM  `altaestudios` LEFT JOIN `tipoestudio` ON `altaestudios`.`IdTipoEstudio` = `tipoestudio`.`IdTipoEstudio`  LEFT JOIN `archivo` ON `altaestudios`.`archivo` = `archivo`.`IdArchivo` WHERE `altaestudios`.`IdUsuario` = :where_0 "
    
    

    thanks

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

    That's what the server is returning? That doesn't look like valid JSON. DataTables should be showing an alert for such cases.

    Could you give me a link to the page so I can check it out?

    Allan

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    I'll pm you page and access to the test

  • KeepMovingKeepMoving Posts: 28Questions: 7Answers: 0

    Its working, thanks for your time

  • FaoFao Posts: 1Questions: 0Answers: 0

    Hi all, same problem here: solutions?
    thanks

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

    Same question as above, what is the server returning?

    Also again, if you give me a link to the page I can check it out.

    Allan

This discussion has been closed.