Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6322627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:22:07+00:00 2026-05-24T16:22:07+00:00

Ok so I’m I’m fairly new to cakephp, and I have a search form

  • 0

Ok so I’m I’m fairly new to cakephp, and I have a search form with multiple fields and I have some fields that are optional in the search form. What I am trying to do is when the form is submitted, is check to see if those fields are not NULL and apply those to my find(‘all’) query.

Here is my ctp view

<h2>IDX Listings</h2>
<?php echo $this->Form->create('Listing', array('action'=>'results')); ?>
<fieldset><legend>Location Criteria</legend>
    <?php echo $this->Form->input('listing_countyid', array('type'=>'select', 'options'=>$counties, 'default'=>'Richmond')); ?>
    <?php echo $this->Form->input('listing_area', array('type'=>'select', 'options'=>$areas)); ?>
    <?php echo $this->Form->input('listing_neighborhood', array('type'=>'select', 'options'=>$hoods)); ?>
    <?php echo $this->Form->input('listing_subdivision', array('type'=>'select')); ?>
</fieldset>
<fieldset><legend>Market</legend>
    <?php echo $this->Form->input('min_price', array('type'=>'select', 'options'=>$minPrices)); ?>
    <?php echo $this->Form->input('max_price', array('type'=>'select', 'options'=>$maxPrices)); ?>
    <?php echo $this->Form->input('listing_yearbuilt', array('type'=>'select', 'options'=>$years, 'label'=>'Built After')); ?>  
</fieldset>
<fieldset><legend>Size</legend>
    <?php echo $this->Form->input('min_rooms', array('type'=>'select', 'options'=>$minRooms)); ?>
    <?php echo $this->Form->input('max_rooms', array('type'=>'select', 'options'=>$maxRooms)); ?>
    <?php echo $this->Form->input('min_stories', array('type'=>'select', 'options'=>$stories, 'label'=>'Min Stories')); ?>
    <?php echo $this->Form->input('min_listing_sqfttotal', array('type'=>'select', 'options'=>$minSqft, 'label'=> 'Min Sq Ft'));
    <?php echo $this->Form->input('max_listing_sqfttotal', array('type'=>'select', 'options'=>$maxSqft, 'label'=> 'Max Sq Ft'));
?>
    <?php echo $this->Form->input('min_bathrooms', array('type'=>'select', 'options'=>$minBath, 'label'=>'Min Bathrooms')); ?>
    <?php echo $this->Form->input('max_bathrooms', array('type'=>'select', 'options'=>$maxBath, 'label'=>'Max Bathrooms')); ?>
    <?php echo $this->Form->input('min_bedrooms', array('type'=>'select', 'options'=>$minBed, 'label'=>'Min Bedrooms')); ?>
    <?php echo $this->Form->input('max_bedrooms', array('type'=>'select', 'options'=>$maxBed, 'label'=>'Max Bedrooms')); ?>
</fieldset>
<fieldset><legend>Options</legend>
    <?php echo $this->Form->input('heat_type', array('type'=>'select', 'options'=>$heat_type, 'label'=>'Heating System')); ?>
    <?php echo $this->Form->input('cool_type', array('type'=>'select', 'options'=>$cool_type, 'label'=>'Cooling System')); ?>
    <?php echo $this->Form->input('irrigation_system', array('type'=>'select', 'options'=>$irrigation_system, 'label'=>'Irrigation System')); ?>
    <?php echo $this->Form->input('handicap', array('type'=>'select', 'options'=>$handicap, 'label'=>'Handicap Equipped')); ?>
    <?php echo $this->Form->input('fireplace', array('type'=>'select', 'options'=>$fireplace, 'label'=>'Fireplace')); ?>
    <?php echo $this->Form->input('fence', array('type'=>'select', 'options'=>$fence, 'label'=>'Fenced')); ?>
    <?php echo $this->Form->input('level1_mstr', array('type'=>'select', 'options'=>$level1_mstr, 'default'=>'No', 'label'=>'1st Floor Master Bedroom')); ?>
    <?php echo $this->Form->input('level1_laundry', array('type'=>'select', 'options'=>$level1_laundry, 'default'=>'No', 'label'=>'1st Floor Laundry Room')); ?>
</fieldset>
    <?php echo $this->Form->submit('Search', array('target'=>"SearchResults", 'id'=>'submit')); ?>

The fieldset that is titled Options is the optional field
The Optional field values set to an array whose initial value is NULL

Here is my search function inside my controller

function results() {
    if (!empty($this->data)) {

        $listings = $this->Idx->find('all', array(
            'conditions'=>array(
                'listing_countyid' => $this->data['Listing']['listing_countyid'],
                'listing_area' => $this->data['Listing']['listing_area'],
                'listing_neighborhood' => $this->data['Listing']['listing_neighborhood'],
                'listing_listprice >=' => $this->data['Listing']['min_price'],
                'listing_listprice <=' => $this->data['Listing']['max_price'],
                'listing_rooms >=' => $this->data['Listing']['min_rooms'],
                'listing_rooms <=' => $this->data['Listing']['max_rooms'],
                'listing_sqfttotal >=' => $this->data['Listing']['min_listing_sqfttotal'],
                'listing_sqfttotal <=' => $this->data['Listing']['max_listing_sqfttotal'],
                'listing_stories >=' => $this->data['Listing']['min_stories'],
                'listing_yearbuilt >=' => $this->data['Listing']['listing_yearbuilt'],
                'listing_bathstotal >=' => $this->data['Listing']['min_bathrooms'],
                'listing_bathstotal <=' => $this->data['Listing']['max_bathrooms'],
                'listing_bedrooms >=' => $this->data['Listing']['min_bedrooms'],
                'listing_bedrooms <=' => $this->data['Listing']['max_bedrooms'],
                'listing_roommasterbrlevel' => $this->data['Listing']['level1_mstr'],
                'listing_roomlaundrylevel' => $this->data['Listing']['level1_laundry'],
                'listing_heatsystem' => $this->data['Listing']['heat_type'],
                'listing_coolsystem' => $this->data['Listing']['cool_type'],
                'listing_irrigationsrc' => $this->data['Listing']['irrigation_system'],
                'listing_fireplaces' => $this->data['Listing']['fireplace'],
                'listing_handicap' => $this->data['Listing']['handicap'],
                'listing_fence' => $this->data['Listing']['fence']
            )
        ));
        $this->set('listings', $listings);
    }
}

My returned results are correct if I remove the options from my find() query, but once I set a value in the view I don’t return the correct results. I know the SQL syntax behind the find() prepared statement is SELECT * FROM table WHERE column_name AND column_name etc. But passing a NULL value to the Database will either (1) not return the results or (2) return the wrong results. I have tried many different methods in the controller, with an OR statement, but OR is to generalized and returns to many results.

Is there a way to modify the SQL statement either in the Controller or in the my Model via the beforeFind() function (beforeFind is beyond my knowledge and its hard to find a solid example of it in use), or would I be better off checking the values and modifying the SQL statement in the Model __construct() function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:22:08+00:00Added an answer on May 24, 2026 at 4:22 pm
    $conditions = array();
    // it's a postback, so the key should exist
    if($this->data['Listing']['listing_countyid'] !== '')
    {
        $conditions['listing_countryid'] = $this->data['Listing']['listing_countyid'];
    }
    if($this->data['Listing']['listing_area'] !== '')
    {
        $conditions['listing_area'] = $this->data['Listing']['listing_area'];
    }
    if($this->data['Listing']['listing_neighborhood'] !== '')
    {
        $conditions['listing_neighborhood'] = $this->data['Listing']['listing_neighborhood'];
    }
    if($this->data['Listing']['min_price'] !== '')
    {
        $conditions['listing_listprice >='] = $this->data['Listing']['min_price'];
    }
    if($this->data['Listing']['max_price'] !== '')
    {
        $conditions['listing_listprice <='] = $this->data['Listing']['max_price'];
    }
    

    I’m sure you can see what I’m doing and you should be able to complete this for the other params. Then you can assign $conditions to the conditions key in your find function call.

    $listings = $this->Idx->find('all', array(
        'conditions' => $conditions
    ));
    

    I considered assigning the $condtions in a loop, but that could be a bit complicated with min and max values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.