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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:19:53+00:00 2026-05-26T06:19:53+00:00

I have the following code: public function ajax() { // Contains results $data =

  • 0

I have the following code:

public function ajax()
{
    // Contains results
    $data = array();
    if( isset($this->request->get['keyword']) ) {
        // Parse all keywords to lowercase
        $keywords = strtolower( $this->request->get['keyword'] );
        // Perform search only if we have some keywords
        if( strlen($keywords) >= 3 ) {
            $parts = explode( ' ', $keywords );
            $add = '';
            // Generating search
            foreach( $parts as $part ) {
                $add .= ' AND (LOWER(pd.name) LIKE "%' . $this->db->escape($part) . '%"';
                $add .= ' OR LOWER(p.model) LIKE "%' . $this->db->escape($part) . '%")';
            }
            $add = substr( $add, 4 );
            $sql  = 'SELECT pd.product_id, pd.name, p.model FROM ' . DB_PREFIX . 'product_description AS pd ';
            $sql .= 'LEFT JOIN ' . DB_PREFIX . 'product AS p ON p.product_id = pd.product_id ';
            $sql .= 'LEFT JOIN ' . DB_PREFIX . 'product_to_store AS p2s ON p2s.product_id = pd.product_id ';
            $sql .= 'WHERE ' . $add . ' AND p.status = 1 ';
            $sql .= ' AND p2s.store_id =  ' . (int)$this->config->get('config_store_id'); 
            $sql .= ' ORDER BY LOWER(pd.name) ASC, LOWER(p.model) ASC';
            $sql .= ' LIMIT 15';
            $res = $this->db->query( $sql );
            if( $res ) {
                $data = ( isset($res->rows) ) ? $res->rows : $res->row;

                // For the seo url stuff
                $basehref = 'product/product&keyword=' . $this->request->get['keyword'] . '&product_id=';
                foreach( $data as $key => $values ) {
                    $data[$key] = array(
                        'name' => htmlspecialchars_decode($values['name'] . ' (' . $values['model'] . ')', ENT_QUOTES),
                        'href' => $this->url->link($basehref . $values['product_id'])
                    );
                }
            }
        }
    }
    echo json_encode( $data );
}

So, the array generates a list of products, like for e.g.:

Apple MacBook (Product Model 10)

Apple МакБук (Product Model 10)

The problem is that those two products is actually one and the same product (same product_id) but in different languages, and both have the same URL.

So, what I want to check is, while making the array, the code to check if there is already a product with that product_id in the array, and if there is, not to add another one with the same product_id.

Practically, I don’t want the array to generate two or more products with the same product_id.

EDIT: With Marc’s code and ghbarratt suggestion work like a charm. A million thanks to you guys, and to all of you here.

P.S. How can I add ASC or DESC for ORDER BY pd.language_id:

$sql .= ' ORDER BY pd.language_id = ' . (int)$this->config->get('config_language_id');

$sql .= ' , LOWER(pd.name) ASC, LOWER(p.model) ASC';
  • 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-26T06:19:54+00:00Added an answer on May 26, 2026 at 6:19 am

    The easiest way should be to add another array to track already written ids and check with http://php.net/manual/en/function.in-array.php:

    $basehref = 'product/product&keyword=' . $this->request->get['keyword'] . '&product_id=';
    $writtenIds = array();
    foreach( $data as $key => $values ) {
        if(in_array($values['product_id'], $writtenIds)) 
        { 
            unset($data[$key]); 
            continue; 
        }
        $data[$key] = array(
            'name' => htmlspecialchars_decode($values['name'] . ' (' . $values['model'] . ')', ENT_QUOTES),
            'href' => $this->url->link($basehref . $values['product_id'])
        );
        $writtenIds[] = $values['product_id'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
All, I have the following code: public function addElements() { $newArray = array(); for
I have the following code; $.ajax({ url: /Home/jQueryAddComment, type: POST, dataType: json, data: json,
I have the following ajax call to webservice to pass json data and get
I have the following code: jQuery: $.ajax({ url: '/personcontroller/getperson', cache: false, type: POST, data:
In my javascript code I have the following: function SelectTopics() { $.ajax({ url: /Administration/Loc/SelectTopics,
I have following code: public static void ProcessStep(Action action) { //do something here if
I have following code public class TEST { public static void main(String arg[]){ try
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code: public static T ParameterFetchValue<T>(string parameterKey) { Parameter result =

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.