Where Clause with conditions in an array
Where Clause with conditions in an array
HI
I have an array of Stand data ['All','Local','Auction','Wholesale']
My table data is access with ajax.
How do i set the where clause for unknown array elements.
<?php
session_start();
@$CompanyId = $_SESSION['ADASOFTCLIENT_CompanyId'];
@$userCompanyName = $_SESSION['ADASOFTCLIENT_userCompanyName'];
@$username = $_SESSION['ADASOFTCLIENT_userName'];
@$usersurname = $_SESSION['ADASOFTCLIENT_userSurname'];
@$loggedin = $_SESSION['ADASOFTCLIENT_loggedin'];
@$UserLevel = $_SESSION['ADASOFTCLIENT_level'];
@$UserStands = $_SESSION['ADASOFTCLIENT_stands'];
if(!isset($username) && !isset($loggedin) && (!$loggedin == true) && isset($CompanyId)){
header('Location:login.php');
}
@$CompanyId = $CompanyId."_stock";
@$CompanyId = strtolower($CompanyId);
include( "./Editor/php/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;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, $CompanyId)
->fields(
Field::inst( 'id' ),
Field::inst( 'StockNr' ),
Field::inst( 'DOS' ),
Field::inst( 'DYOS' ),
Field::inst( 'REGNR' ),
Field::inst( 'OLDREG' ),
Field::inst( 'NatisNr' ),
Field::inst( 'TYPE' ),
Field::inst( 'MAKE' ),
Field::inst( 'MODEL' ),
Field::inst( 'MMCODE' ),
Field::inst( 'VEHICLE_CONDITION' ),
Field::inst( 'COLOUR' ),
Field::inst( 'YEAR' ),
Field::inst( 'REG_MONTH' ),
Field::inst( 'KILOS' ),
Field::inst( 'CLASSIFICATION' ),
Field::inst( 'VIN_NO' ),
Field::inst( 'ENGINENR' ),
Field::inst( 'Status' ) ,
Field::inst( 'Stand' ) ,
Field::inst( 'OwnerSuppAccCode' ),
Field::inst( 'keynumber' )
)
->where( function ( $q ) {
?????????? Array [ 'Local' OR 'wholesal' OR .......] = to Stand field
} )
->process( $_POST )
->json();
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
This is an example from my own coding - more complex than what you need but you'll recognize what you can use of this stuff: I have a string called $_SESSION['govSearchString']. I split it into an array and then use the array elements in my WHERE clause. All you need to know is the maximum number of potential array elements. Let's assume the array has maximum 4 elements but only two of them are actually provided in reality. In this case you fill your first two where clause fields with those values and the other two "or_where" fields are blank (or whatever is appropriate for you). That works.
The WHERE clause:
the function splitQueryString called in the where clause:
Looks good.. But the problem is that i will never know the maximum amount of elements in the array
WOuld it be possible to add my own method to Query.php..... -> whereString()
Is there a way to input sql string....... ->where(' Stand = "All" || Stand = "Wholesale"') ?
Options
I found this in the documentation where( array('fieldName', ...), array('value', ...) );
Something like this could work. But it might be harmful to SQL injections. Please check whether it is being bound by Data Tables. You'll find something here:
https://editor.datatables.net/manual/php/conditions
Another idea: Don't know where you get your array from. If it is being selected from a database as well you can pass it in via a subselect like this:
Here is what i had to do
and the plug in Query.php file