0

I think I have my dumber-than-usual head on today.

I am trying to parse text from a textbox in a form to generate an array that I can pass to the find method in cakePHP.

I have used php regular expressions to generate an array of the terms entered (including "phrases included in speech marks" as single item)

This may look like this:

array(
(int) 0 => 'test',
(int) 1 => 'this out',
(int) 2 => 'now')

In the cakePHP cookbook it says that I have to get an array that looks like this (using above example):

array(
array('ProjectDocumentSectionVariable.variable_description  LIKE' => '%test%'),
array('ProjectDocumentSectionVariable.variable_description  LIKE' => '%this out%'),
array('ProjectDocumentSectionVariable.variable_description  LIKE' => '%now%'))

Try as I might, I end up with arrays that do NOT look like an array of arrays.

Typically I am ending up with this horrible looking thing:

array(
(int) 0 => array(
    'ProjectDocumentSectionVariable.variable_description LIKE' => '%test%'
),
(int) 1 => array(
    'ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%'
),
(int) 2 => array(
    'ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'
))

The code for the above is:

$i = 0;
            foreach($matches[1] as $match) :
                $searcharray['ProjectDocumentSectionVariable.variable_description LIKE'] = "%" . $match . "%";                  
                array_push($array_full,$searcharray);
                $i++;
            endforeach;

THis must be a common requirement, so I am hoping one of you guys could put me on the straight an narrow...

Thanks

2
  • Please edit to use actual model and field names - it helps to understand the question. Your bottom section and your 'hoping for this' section don't line up at all as far as names, which makes it confusing. Also - what is your "matches" array? And your $searcharray? Commented Nov 15, 2013 at 14:24
  • Yes, sorry Dave - less haste more speed. The matches array is the one that is shown at the top, coming out of the reg expression processing of textbox content.. $searcharray is set on the line it is first mentioned... I think. (It does seem to behave "properly" - but may be where I am going wrong!) Commented Nov 15, 2013 at 14:27

2 Answers 2

1

If you're trying to build a search query (not clear what your goal is), your conditions should all be in the same array within an OR or AND - something like this:

$searchResults = $this->find('all', array(
    'conditions' => array(
        'OR' => array(
            'ProjectDocumentSectionVariable.variable_description  LIKE' => '%test%',
            'ProjectDocumentSectionVariable.variable_description  LIKE' => '%this out%',
            'ProjectDocumentSectionVariable.variable_description  LIKE' => '%now%'
        )
    )
));

If you want to build it out in a foreach() loop, you can do it something like this:

$conditions = array('OR' => array());
$matches = array('test', 'this out', 'now');
foreach($matches as $match) {
    array_push($conditions['OR'], array('variable_description LIKE' => '%' . %match . '%'));
}

$searchResults = $this->find('all', array(
    'conditions' => $conditions
));
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I am trying to do that..Thanks for your time. the foreach loop above leaves a condition array with only one item - the last item in the $matches array... any other ideas?
updated - forgot you'll need it to be within an AND or an OR array.
Thanks David, this did it. It was the setting up of the initial array with the or array and then pushing to that that flumoxed me! Brill. Thanks again.
0

its as simple as this:

 $array= array();
    foreach ( $colors as $c):
        $uniques[] = $c;   
    endforeach;

not having the [] will treat it as a regular variable. This will append it to the array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.