Use a variable in '$columns = array' in ssp script

Use a variable in '$columns = array' in ssp script

bbrindzabbrindza Posts: 316Questions: 73Answers: 1

I am passing a variable from an $ajax call ($taskFormType = $_POST['taskFormType'];) .
I want to use this in an if statement within '$columns = array' .
How can I do this?

<?php

date_default_timezone_set('America/Chicago'); // CDT

$today  = date("Y-m-d");

include('i5db2connect.php');

$taskFormType = $_POST['taskFormType'];
$locationCode = $_POST['locationCode'];
$machineCode = $_POST['machineCode'];

// DB table to use
$table = 'NWFF.MTCTSKH';

// Used for optional WHERE clauses that will be appended to the SQL string
 $extraWhere = "MTLOC ='" .$locationCode. "'and MTCODE = '" .$machineCode. "'" ;

// Table's primary key
$primaryKey = array('MTLOC', 'MTCODE');

// Array of database columns 
$columns = array(

    array( 'db' => 'MTLOC',   'dt' => 'location_code' ),
    
    array( 'db' => 'MTCODE',   'dt' => 'machine_code' ),
    
    array( 'db' => 'MTTSK', 'dt' => 'doc_link',
             'formatter' => function($d, $row){
               
                if($taskFormType == 'viewTask'){
                
                    return '<a id="doc_link" class="link" href="javascript:void(0)" onClick="openTaskForm(\'' . trim($row['MTCODE']).  '\', \''.trim($row['MTLOC']).'\', \'' .trim($d).'\', \'viewTask\' )">' .$d. '</a>';
               
                }else if ($taskFormType == 'copyTask'){
                   
                    return '<a id="doc_link" class="link" href="javascript:void(0)" onClick="openTaskForm(\'' . trim($row['MTCODE']).  '\', \''.trim($row['MTLOC']).'\', \'' .trim($d).'\', \'copyTask\')">' .$d. '</a>';
                }
                
             }
         ),
   
     array( 'db' => 'MTTSK',   'dt' => 'task_number' ),

     array( 'db' => 'MTLPMD',   'dt' => 'last_pm_date',
             'formatter' => function( $d, $row ) {
                 if ($d > 0)
                 {
                     $dt = DateTime::createFromFormat('Ymd', $d);
                     return $dt->format('m-d-Y');
                 }
                 else
                 {
                     return '';
                 }
             },
         ),
     
     array( 'db' => 'MTLSMD',   'dt' => 'next_pm_date',
             'formatter' => function( $d, $row ) {
                 if ($d > 0)
                 {
                     $dt = DateTime::createFromFormat('Ymd', $d);
                     return $dt->format('m-d-Y');
                 }
                 else
                 {
                     return '';
                 }
               },
            ),         
     
     array( 'db' => 'MTLSCH',   'dt' => 'pm_scheduled' ),
     array( 'db' => 'MTINTR',   'dt' => 'interval_to_next_pm' ),
     array( 'db' => 'MTPM#',   'dt' => 'scheduled_pm_number' ),
     
     array( 'db' => 'MTTYPE',   'dt' => 'type_of_pm' ,
         'formatter' => function( $d, $row ) {
        
             if ($d=='T'){   
                 return 'Time';
             }
             
             else if ($d=='M')
             {
               return 'Meter';
               
             }else{
                 return '';
             }
             },
         ),
         
     array( 'db' => 'MTMETR',   'dt' => 'meter_reading' ),
     array( 'db' => 'MTMTRL',   'dt' => 'meter_scheduled_amount' ),
     
  );
require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $connection, $select, $table, $primaryKey, $columns, $extraWhere)

This question has an accepted answers - jump to answer

Answers

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

    Build the array in a more procedural way:

    $columns = array();
    $columns[] = array( 'db' => 'MTLOC',   'dt' => 'location_code' );
    
    if ($taskFormType === 'myType') {
      $columns[] = array( 'db' => 'MTCODE',   'dt' => 'machine_code' );
    }
    
    // etc - add more columns
    

    Allan

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    Excellent. Did not think about that . thanks for the tip Allan.

Sign In or Register to comment.