Forums

WHERE (return array) with several optionnal conditions

simelas 07 Oct, 2014
Bonjour Bob,

Question about return array and optionnal multiconditions.

I try to figure out how to select within a database via retun array but with several optionnal conditions coming from drop down or tick boxes choices( in other words from several $form->data ['something_selected'] ).
schematic example :
if ($form->data['A']) ....;
if (B) .....;
if (C) ....;
return array ('A'  and or  'B'  and or  'C' , ...etc  )

There are about 10 conditions . Some of the database fields contain a unique value, and some others an array of values ( e.g.: a,b,c,d,e,..)
I went through the forum and tried few options such as $variable(s) concatenation but nothing satisfactory came out.

Any hint ?

Thanks in advance,

"Simelas"
simelas 08 Oct, 2014
Hi Bob,

after "having gone to the moon and came back while solution was across the street" i believe simple solution found :
<?php
$lang =& JFactory::getLanguage();
$tag = $lang->getTag() ;
$tag = $tag ;
$array1 = array('language' => "$tag");
$array = array_merge($array1);
if (!empty($form->data['field2_select'])) {
  $array2 = array( 'status' => $form->data['field2_select'] );
  $array = array_merge($array,$array2) ;
}
if (!empty($form->data['field3'])) {
  $array3 = array( 'field3' => $form->data['field3_select'] ) ;
  $array = array_merge($array,$array3) ;
}
if (!empty($form->data['field4_select'])) {
  $array4 = array( 'field_4' => $form->data['field4_select'] );
  $array = array_merge($array,$array4) ;
}
'etc.....'
return($array);
?>

(Two first lines are for language selection if any and $array2 ,3 , 4 , etc, could be well replaced by '$array1' only .
instead of !empty(something) could also use if (something).... )
Hope this may help other Chronoforms V5 fans !.

Happy to get your feedback.
Best Regards,

"Simelas"
GreyHead 10 Oct, 2014
Hi Simelas,

Sorry not to have replied earlier, this one got missed.

Here's a slightly simplified version of your code:
<?php
$array = array(); // empty results array
$lang = JFactory::getLanguage();
$array['language'] = $lang->getTag();
$cols_array = array(
  'status' => 'field2_select',
  'field3' => 'field3_select',
  'field4' => 'field4_select',
  // more column => inputs here
);
foreach ( $cols_array as $k => $v ) {
  if ( isset( $form->data[$v] ) &&  $form->data[$v] ) {
    $array[$k] = $form->data[$v];
  }
}
return $array;
?>
!! Not tested and may need debugging !!

Bob
simelas 11 Oct, 2014
Hi Bob,

your solution is excellent. More compact and works like a charm ! ( even not debugging needed).

Maybe one more question :

Is it possible and how would you add a "search in_array" to the whole stuff ?
In other words how to include into the return $array the " if (in_array( ..... , .... )) " functionnality. Again tried several solutions but seems to block somewhere.

If starting from your solution i am looking for something like (the if in_array is not necessarily showed at the right position) :

<?php
$array = array(); // empty results array
$lang =& JFactory::getLanguage();
$array['language'] = $lang->getTag();
$cols_array = array(
  'status' => 'field2_select',
  'field3' => 'field3_select',
  'field4' => 'field4_select',

etc....

  // more column => inputs here
);
foreach ( $cols_array as $k => $v ) {
  if ( isset( $form->data[$v] ) &&  $form->data[$v] ) {
    $array[$k] = $form->data[$v];
  }

if (in_array ('$somefield_select', $somefield )) {

}
return $array;
?>


Appreciate your input

Cheers !

Phil
GreyHead 13 Oct, 2014
Hi Phil,

Can you give me a bit more info about what you want the in_array() code to do? I can't see what you want the resulting query to be.

Bob
simelas 13 Oct, 2014
Hi Bob,
The present method with return $array works very well when there is only a single value in the column (fields) . Lets say when column field3 or field 4, etc do contain only a single value could it be numeric or alphanumeric.
But what about when one of the column contains a string of concatenated values ?

For example if fields 3 contains " ab1" and field4 contains: " bc1,bc2,bc3,bc4,bc5, ...." how would you handle field 4 ? with return $array method ?

I have tried via in_array, or building some mysql queries or FIND_ IN_SET, or LIKE %aaa%, etc, following the tuto about WHERE conditions but not success !.

<?php
$array = array(); // empty results array
$lang = JFactory::getLanguage();
$array['language'] = $lang->getTag();
$cols_array = array(
'status' => 'field2_select',
'field3' => 'field3_select',
'field4' => 'field4_select', >>>>>> If this column contains a string of values ? for example
// more column => inputs here
);
foreach ( $cols_array as $k => $v ) {
if ( isset( $form->data[$v] ) && $form->data[$v] ) {
$array[$k] = $form->data[$v];
}
}
return $array;
?>


Best,

Phil
Max_admin 16 Oct, 2014
Hi Phil,

For "LIKE", just use this in your array:
"field4 LIKE" => "%value%"


Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
simelas 18 Oct, 2014
HI Max,

i have tried the " field4 LIKE" => "%value% " but did got the expected results.
Maybe my initial question was not clear enough. (in the meantime i changed also a little bit the structure of the db).
What i am trying to do indeed is to find an alternative to an " explode " function followed by " for if " for comparison purposes of the content of a colum field " $select['column_id'] " in a database with an input " $form->data['selected_value'] " .
$select['column_id'] will contain an array of alphanueric values let say " " ab1,ab2,ab3,ab4,ab5,...., ...."
$form->data['selected_value'] could be empty or contain one of the " ab1,ab2,ab3,ab4,ab5, ... , ...." alphanumeric values let say " ab3 " for example .

Classic method would be getting the values to analyse via a dbread action ( which works well ):
foreach($form->data['SelectResults'] as $select):
$result=explode(',',$select['column_id']);
if ($form->data['selected_value']) {
for ($i = 0; $i < count($result); $i++) { if ($result[$i] == $form->data['selected_value']) {
do something ; } } } .... etc

I am trying to find out if with the "array method"; the same stuf could be done without " foreach " "explode" and " if for "
Reason is that there are several selectors and possible choices which leads to a long list of " for if do_ something ".

Best Regards,

Phil
Max_admin 18 Oct, 2014
Hi Phil,

You are trying to select records which has the $form->data['selected_value'] string, correct ?

If yes then you need a LIKE, and my code should work, please check the debug results or post it.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
simelas 25 Oct, 2014
Answer
Hi Max,

Sorry for coming late back to you.

it works well when using "%".$form->data['sv']."%" in the foreach loop as illustrated hereafter
<?php 
$array = array(); // empty results array
$cols_array = array(
    'field_1 LIKE'  =>  'value_1_select',
    'field_2 LIKE'  => 'value_2_select'

);
foreach ( $cols_array as $k => $v ) {
  if ( isset( $form->data[$v] ) &&  $form->data[$v] ) {
    $array[$k] = "%".$form->data[$v]."%";
  }
}
return $array;
?>

Probelm solved,

Best regards,

Phil
This topic is locked and no more replies can be posted.