How to use PHP in the Where clause in CCv4

construct a dynamic WHERE clause in ChronoForms CCv4 using PHP.

Overview

The issue occurred because the form data was not accessible through the `$form->data` object.
Retrieve the form values directly using `JRequest::getVar` and then echo the complete WHERE clause string.

Answered
sl sloanthrasher 20 Dec, 2014
Hi all,

I need to use php to construct the where clause in a CCv4 connection. I've added multiple search fields to the heading, and need to then test each for content, adding a `field` like '%field_val%' clause to the where used by CCv4.

Here's the code I've tried:

<?php
	$rslt = array();
	if(isset($form->data['_cc_search_string_last']) && $form->data['_cc_search_string_last'] != '') {
		$rslt[] = array("`Last_Name` like" => "'%".$form->data['_cc_search_string_last']."%'");
	}
	if(isset($form->data['_cc_search_string_first']) && $form->data['_cc_search_string_first'] != '') {
		$rslt[] = array("`First_Name` like" => "'%".$form->data['_cc_search_string_first']."%'");
	}
	if(isset($form->data['_cc_search_string_city']) && $form->data['_cc_search_string_city'] != '') {
		$rslt[] = array("`Mailing_City` like" => "'%".$form->data['_cc_search_string_city']."%'");
		$rslt[] = array("`Business_City` like" => "'%".$form->data['_cc_search_string_city']."%'");
	}
	return $rslt;
?>

What is CCv4 expecting from the php code? An array? A String?
Is there a way to "echo" in the php code so that it shows up when the connection is run on the front-end for debug purposes? Maybe $form->debug['name'] = 'debug message'?

Thanks!
sl sloanthrasher 20 Dec, 2014
Answer
1 Likes
Never mind, I figured it out.

The problem with the code above is that $form->data isn't populated with the form values. I changed the code to:

<?php
	$rslt = array();
	$src_type = JRequest::getVar('_cc_search_button');
	$fname = JRequest::getVar('_cc_search_string_first');
	$lname = JRequest::getVar('_cc_search_string_last');
	$city = JRequest::getVar('_cc_search_string_city');
	if($src_type == 'Search') {
		if(isset($lname) && $lname != '') {
			$rslt[] = "`Last_Name` like '%".$lname."%'";
		}
		if(isset($fname) && $fname != '') {
			$rslt[] = "`First_Name` like '%".$fname."%'";
		}
		if(isset($city) && $city != '') {
			$rslt[] = "`Mailing_City` like '%".$city."%'";
			$rslt[] = "`Business_City` like '%".$city."%'";
		}
	}
	$w = implode(' AND ',$rslt);
	php echo $w; 
?>

And now it works!
So to answer my own question, you simply echo the text you want placed in the where clause.
This topic is locked and no more replies can be posted.