If statement for datatables editor
If statement for datatables editor
davidjmorin
Posts: 101Questions: 31Answers: 0
I am trying to add an if statement inside the editor PHP file for the DB connection and query.
if($_SESSION['role'] != 'Admin') {
->where( 'locations.district', $location )
}else{}
->leftJoin( 'locations', 'allUpPlanChange.Agent_ID', '=','locations.Outlet_ID' )
This throws an ajax error.
I want to remove that where clause if user is admin. Where and how can I achieve this?
Full Query
<?php
session_start();
$location = $_SESSION['district'];
require '../../../vendor/autoload.php'; //Dont forget to edit this
include( "lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
Editor::inst( $db, 'allUpPlanChange' )
->fields(
Field::inst( 'allUpPlanChange.Agent_ID' ),
Field::inst( 'allUpPlanChange.Tran_Year' ),
Field::inst( 'allUpPlanChange.Tran_Period' ),
Field::inst( 'allUpPlanChange.Original_Mobile_ID' ),
Field::inst( 'allUpPlanChange.Mobile_ID' ),
Field::inst( 'allUpPlanChange.Alt_Mobile_ID' ),
Field::inst( 'allUpPlanChange.Device_ID' ),
Field::inst( 'allUpPlanChange.Account_Number' ),
Field::inst( 'allUpPlanChange.Device_Change_Date' )
->validator( Validate::dateFormat( 'Y-m-d' ) )
->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
->setFormatter( Format::dateFormatToSql('Y-m-d' ) ),
Field::inst( 'allUpPlanChange.Previous_Price_Plan' ),
Field::inst( 'allUpPlanChange.Current_Price_Plan' ),
Field::inst( 'allUpPlanChange.Previous_Access_Charge' ),
Field::inst( 'allUpPlanChange.Current_Access_Charge' ),
Field::inst( 'allUpPlanChange.Agent_SSO_ID' ),
Field::inst( 'allUpPlanChange.Kicker_Eligible' ),
Field::inst( 'allUpPlanChange.Kicker_Amount2' ),
Field::inst( 'locations.district' ),
Field::inst( 'locations.RQ_Name' ),
)
if($_SESSION['role'] != 'Admin') {
->where( 'locations.district', $location )
}else{}
->leftJoin( 'locations', 'allUpPlanChange.Agent_ID', '=','locations.Outlet_ID' )
->debug( true )
->process( $_POST )
->json();
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
At a glance, your "else" condition is empty.
Also, what does your error actually say?
@tangerine
I also tried this and same error
The link provided in the error message would be where to start debugging.
@tangerine I have done this. It provided me no information in my instance. Im getting a 500 error when using the if statement.
It provided this information:
@tangerine Thank you for your input. I am aware of what that says. Thanks.
Anyone else?
If your original post had explained that you had followed the diagnostic steps and understood that you needed to check your server's error logs, then I needn't have bothered replying at all. I won't be bothering again.
@tangerine and as I explained that yielded no results for me. Nothing on my server log. I am frustrated by this simple task that should be working and for whatever reason datatables editor is making it impossible.
Don't know anything about PHP syntax but my guess is you are breaking the use of
->
chaining by introducing the{}
in the middle. Seems like this would throw a syntax error. I might be completely wrong thoughTake a look at this thread at how the OP used an if / else statement and completed the chain of
->
.Kevin
@kthorngren Thank you! That was it.