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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:45:34+00:00 2026-06-17T05:45:34+00:00

I’m working on a search engine using CakePHP 2.0 and am having difficulty finding

  • 0

I’m working on a search engine using CakePHP 2.0 and am having difficulty finding the most efficient way to get the result I’m wanting.

Say I’m querying people and I get a set of 20 results with 5 age 20, 10 age 30 and 5 age 40. In addition, 15 of these people have brown eyes, 3 have blue and 2 have green. I want to find the most efficient way to get those specific counts. I’ll then display these results on the page so that users can see what’s in the results with those parameters. They will then be able to click on one of them to add that search parameter to the current query.

This isn’t something that I can store in a database or cache at all because each search could be different and could/will return different results.

If I’m not explaining what I’m trying to do (this may be likely) there are several websites that do this. Cars.com uses this method when searching for cars. You search a generic search and then links on the side allow you to narrow your results. These links include counts of the current result set that fall within the specific parameter.

An idea has been to get the full result set and then parse through it generating the counts and this would work, but in my specific project I’m dealing with thousands of records and it seems like this could add additional load time to the page and/or strain on the server.

Here’s a visual example:
visual example of the question/result I'm trying to achieve

  • 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-06-17T05:45:35+00:00Added an answer on June 17, 2026 at 5:45 am

    Cars.com is likely using associated tags for each feature that is counted. With associated tags a table record has many and belongs to many feature tags.

    So that they don’t have to create a tag for each car price. They create price range tags.

    For every car record there are associated tag records that hold all features of that car. You can then cache a count in the tag record of how many cars have that feature.

    The SQL table structure might be something like this.

     CREATE TABLE `cars` (
       `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
       `make` varchar(45) DEFAULT NULL,
       `model` varchar(45) DEFAULT NULL,
       PRIMARY KEY (`id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
     CREATE TABLE `features` (
       `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
       `name` varchar(45) DEFAULT NULL,
       `count` int(11) NOT NULL DEFAULT '0',
       PRIMARY KEY (`id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
     CREATE TABLE `cars_features` (
       `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
       `car_id` int(11) NOT NULL,
       `feature_id` int(11) NOT NULL,
       PRIMARY KEY (`id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    For every Car record there can be multiple Feature records. These are associated to each Car via the cars_features table. When someone searches and finds Car XXXX you can then look up the Features of that car, and also display a cached count of how many cars have that feature.

    EDIT:

    To narrow the counts so that they are limited to only the cars that were discovered in the search. You’ll need to first get a list of all the Car IDs and then perform a COUNT using a JOIN between the cars_features table and features.

    Here is some sample data.

      INSERT INTO `cars` (`make`, `model`) VALUES ('Ford', 'Explorer');
      INSERT INTO `cars` (`make`, `model`) VALUES ('Hond', 'Civic');
      INSERT INTO `cars` (`make`, `model`) VALUES ('Hond', 'Civic');
    
      INSERT INTO `features` (`name`, `count`) VALUES ('Red', 2);
      INSERT INTO `features` (`name`, `count`) VALUES ('Green', 1);
    
      INSERT INTO `cars_features` (`car_id`, `feature_id`) VALUES (1, 1);
      INSERT INTO `cars_features` (`car_id`, `feature_id`) VALUES (2, 1);
      INSERT INTO `cars_features` (`car_id`, `feature_id`) VALUES (3, 2);
    

    Assuming we searched for that returned two items so that our Car IDs were (1,2). We could find a feature count using the following SQL query.

     SELECT `features`.`id`,COUNT(`features`.`id`)
         FROM `cars_features` 
         JOIN `features` ON (`cars_features`.`feature_id`=`features`.`id`)
         WHERE `cars_features`.`car_id` IN (1,2)
         GROUP BY `features`.`id`
    

    This will report that count for each Feature limited to just the Car records found.

    I’ll try to write the above in CakePHP model format.

     $this->CarFeature->find('all',array(
        'conditions'=>array('CarFeature.car_id'=>$ids),
        'fields'=>array('Feature.id','COUNT(Feature.id)'),
        'group'=>array('Feature.id'),
        'contain'=>'Feature'
     ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms

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.