Forums

Passing a value from a url to a custom where clause

jinx52 05 Jun, 2012
Hi,
I am using a custom "where" clause to load a db record based on user_id. I would like to include a value passed from the url in the "where" clause as well. At the moment my "where" clause looks like this:
<?php
$user =& JFactory::getUser();
echo "`cf_user_id` = '{$user->id}'";
?>

If the URL includes "...token=1" I would like to include that in the "where" clause as well to check against another field in the database (say one called record_type). In other words I want my "where" clause to finish up reading;
`cf_user_id` = '{$user->id}' and 'record_type' = 'token'.

Token is obviously replaced with the value passed in the URL when the form is run.

Is this possible?

Thanks in advance.

Nick
GreyHead 05 Jun, 2012
Hi Nick,

Please try this
<?php
$token = $form->data['token'];
$user =& JFactory::getUser();
$where = array();
$where[] = "`cf_user_id` = '{$user->id}'";
if ( $token ) {
  $where[] = "`record_type` = '{$token}'";
}
$where = implode(' AND ', $where);
echo $where;
?>
You can continue to add more 'AND' clauses if you need them by adding extra entries in the $where array.

Bob
jinx52 05 Jun, 2012
Thanks Bob, that's brilliant! It worked like a charm.
Is there somewhere I can read about the structure of $form->data to understand it better?
Thanks again,
Nick
GreyHead 05 Jun, 2012
Hi Nick,

There's not a lot to know but here goes:

ChronoForms stores all the information about the current form in the $form object (an object is - more or less - a fancy variant of an array). *Everything* about the form is there somewhere. For example, you'll find the form name in $form->form_details->name

A part of the $form object is a 'sub-object' where the current form data is saved. It's in $form->data.

At the beginning of the form On Load event the $form->data object is empty but very early on ChronoForms adds any variables that are in the $_GET array (that is data from the URL that called the form) and the $_POST array (that is usually data from a previous form step).

You can add more data to the $form->data object in the On Load event by using actions like the 'DB Record Loader' action, or the 'Session to Data' action. You can manipulate data using PHP in 'Custom Code' actions.

When the Show HTML action runs it will use the $form->data object contents (a) to 'Re-publish' values in form inputs and (b) if the Curly Replacer is turned, on it will use the contents of the object to replace {input_name} in the Form HTHL.

When the On Submit action starts exactly the same thing happens. The contents of the $_POST array - the form results - are added to the $form->data array and are available and/or used in all of the succeeding actions. Again you can use Custom Code actions and some others to manipulate or add values to the object.

One important difference from ChronoForms v3 is that the $form->data object is loaded once from the $_POST array, any changes to the $_POST array after that will have no effect on the data that ChronoForms uses. This is why the JRequest::setVar() methods used in many of the custom code snippets for CFv3 don't work in CFv4.

Bob
jinx52 08 Jun, 2012
Thanks again Bob,
It's a lot to get my head around, but at least I can make it work!
Cheers,
Nick
This topic is locked and no more replies can be posted.