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

  • Home
  • SEARCH
  • 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 8886185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:29:51+00:00 2026-06-14T21:29:51+00:00

What I am trying to do is write a search class that can search

  • 0

What I am trying to do is write a “search” class that can search for a list of products and store them in an array.

I already have a “product” class that can be used to get the details of a specific product.

Here is my code:

class Product {

    public $name;
    public $price;
    public $description;

    public function getProductById ($id) {

        $sql = 'SELECT name, price, description FROM product WHERE id = ' . $id;

        $row = /* MySQL functions here to execute SQL statement and get a matching row */

        $this->name = $row['name'];
        $this->price = $row['price'];
        $this->description = $row['description'];
        return TRUE;
    }
}

class Search {

    public $results;
    public $totalResults;

    function __construct() {
        $this->results = array ();
        $this->totalResults = 0;
    }

    public function doSearch ($name) {

        $sql = 'SELECT id FROM product WHERE name LIKE "%' . $name . '%"';

        $rows = /* MySQL functions here to execute SQL statement and get a list of matching product ID's */

        foreach ($rows as $row) {
            $product = new Product;
            $product->getProductById ($row['productid']);
            $this->results[] = $product;
        }
        return TRUE;
    }
}

$search = new Search;
$search->doSearch ('Fresh Flowers');

The problem with the above is that every matching record in the doSearch method will execute a query in the getProductById method. If there are 100 matching products, there will be 100 individual queries carried out in the Product class.

However, if I get the products directly in the doSearch method using a single query, this will then bypass the Product class altogether.

When a “product” is an object, what’s the most appropriate way to write a search class that can return a list of “product” objects without the overhead of what I’m doing above?

  • 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-14T21:29:52+00:00Added an answer on June 14, 2026 at 9:29 pm

    Add a constructor to the Product class which takes name, price and description as parameters (or an assoziative array), to populate the object with the necessary values, decoupled of the database query.

    Within doSearch, you can then create a SELECT which not only gets the ID but all relevant fields from the products table, and create the populated product objects immediately with new Product($name, $price, $description) or new Product($row), without calling getProductById for each product.

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

Sidebar

Related Questions

I am trying to write a method that reads a list from file1, search
I am trying to write a query to search a subclass attribute that is
I'm trying to write an SQL query that would search within a CSV (or
Possible Duplicate: byte[] array pattern search I am trying to write a simple compression
I am trying to write code to search all children for a div that
I am trying to write simple Binary Search tree class (BST). When I add
I am trying to write a sample program that can call use the main
I am trying to write a hash table for class and I can't seem
I have been trying to write a recursive function that searches a stack, but
I am trying to write a custom comparer to sort a list of search

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.