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 4255038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:09:54+00:00 2026-05-21T05:09:54+00:00

I’m building a search page that will have many different search parameters that the

  • 0

I’m building a search page that will have many different search parameters that the user can click on, which will pass the variables in the URL.

$this->Restaurant->recursive = 1;
$this->paginate = array(
    'conditions' => array(
        'City.name' => $this->params['url']['city'],
        'Cuisine.name' => $this->params['url']['cuisine'],
    ),
);
$data = $this->paginate('Restaurant');
$this->set('data', $data);

This works just fine IF there is ?city=newyork&cuisine=pizza in the URL – but if not, it errors out* If it were normal PHP, I’d build out the query as a string and append conditions only if the variables existed…etc. But with Cake I’m not sure where to begin or what would be the best way to manage this.

EDIT:

*If I do not have ‘city’ or ‘cuisine’ being passed as URL variables, I get this:

<b>Notice</b> (8)</a>: Undefined index:  city [<b>APP/controllers
/restaurants_controller.php</b>, line <b>18</b>

...

City.name'&nbsp;=&gt;&nbsp;$this-&gt;params['url']['city'],
</span></code></span></pre></div><pre class="stack-trace">
RestaurantsController::search() - APP/controllers/restaurants_controller.php
, line 18
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83

It wouldn’t be a problem if I just had “city” and “cuisine” – but if I plan on having 20 more search options to be passed in the same way, I’d like to be able to pass them or not at my discretion instead of being forced to have them ALL in the URL every time.

  • 1 1 Answer
  • 1 View
  • 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-21T05:09:54+00:00Added an answer on May 21, 2026 at 5:09 am

    What do you mean errors out?

    Usually adding $this->Paginator->options(array('url' => $this->passedArgs)); before your pagination call in the view will make cake add all the passed URL params to the url that it generates

    EDIT

    What you’re looking for are named parameters : http://book.cakephp.org/view/947/Named-parameters

    If you look at the last example, you’ll see that the route maps to ContentsController->view(); with all the parameters you (optionally) chose to send available in the passedArgs array.

    URL: /contents/view/chapter:models/section:associations
    Mapping: ContentsController->view();
    $this->passedArgs['chapter'] = 'models';
    $this->passedArgs['section'] = 'associations';
    $this->params['named']['chapter'] = 'models';
    $this->params['named']['section'] = 'associations';
    

    EDIT 2

    Your problem here is that you have a variable set of search criteria. Your system should basically go through the search criteria and build your search query depending what filters have been chosen. There are several ways you can solve this. The easiest would be to loop through them.

    $allowsFilters = array('city' => 'Model.city', 'name' => 'Model.name', 'price' => 'Model.price'); //list of allowed keys to search on. 
    $opts  = array();
    foreach ($this->params['named'] as $filter => $value)  {
        if (isset($allowsFilters[$filter])) {
             $opts[$allowsFilters[$filter]] = $value; 
        }
    }
    $findOpts = array('conditions' => $opts)
    $this->Model->find('all', $findOpts);
    

    This way, you’re whitelisting a set of filters that can be applied. And you loop through the params and build your conditions array. You’ll have to modify this a bit if you need OR conditions or different MYSQL. But this will allow you to have a variable number of filters.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
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
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.