v.2.5 Contains function info

5 posts by 2 authors in: Forums > CMS Builder
Last Post: January 14, 2013   (RSS)

Saw that the new 2.5 release of CMSBuilder sports a "contains" function. Are there any docs or examples of this floating around that I've missed?

Cheers,
J.

Hi J,

There isn't any documentation for this function yet. Here is an example of how you could use it:


$email = 'greg@interactivetools.com';


if(contains('@',$email)){
  echo 'true';
}else{
  echo 'false';
}

The function can be used to check if a string contains another string, and it will return a boolian of true or false

The above function would echo true as the $email string does contain an @ symbol, but if you chaned the $email variable to 'greg' it would return false.

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi,

You could use this function to check if an item has been checked in in a list, something like this would work:

// load records from 'blog'
  list($blog, $blogMetaData) = getRecords(array(
  'tableName' => 'blog',
  'perPage' => '10',
  'loadUploads' => true,
  'allowSearch' => false,
));


foreach($blog as $item){
  if(contains("option one\t",$item['listField'])){
    echo 'true';
  }else{
    echo 'false';
  }
    echo '<br>';
}

You would need to change the listField variable to the name of the field that contains your list options. So this would return a list of true or false depending on if the list field has the 'option one' value selected. CMS Builder stores list fields as a string that is tab seperated.

If your using getRecord to return your records, then you might be better using the PHP in_array function instead:

foreach($blog as $item){
  if(in_array("option one",$item['listField:values'])){
    echo 'true';
  }else{
    echo 'false';
  }
  echo '<br>';
}

This will check if the meta field listField:values -which is an array of all of the selected values for a drop down- contains the value 'option one'.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Thank you Greg. That's exceptionally helpful. I really appreciate you going through the "what if" process in your response.

Be well!