简体   繁体   中英

Codeigniter single data extraction error with result

I've done a live search section with ajax, but when I combine the pictures and products table with join, the number of pictures shows as many products as I want to show this one. Codeigniter just started to confuse me a bit thanks for your help

Model

public function search_products($search)
{
    $search = remove_special_characters($search);
    $this->db->join('products', 'images.product_id = products.id');
    $this->db->like('products.title', $search);
    $this->db->order_by('products.is_promoted', 'DESC');
    $this->db->limit(8);
    $query = $this->db->get('images');
    return $query->result();
}

Controller

public function ajax_search()
    {
        $lang_base_url = $this->input->post('lang_base_url', true);
        $search_type = $this->input->post('search_type', true);
        $input_value = $this->input->post('input_value', true);
        $input_value = remove_special_characters($input_value);

        $data = array(
            'result' => 0,
            'response' => ''
        );

        if (!empty($search_type) && !empty($input_value)) {
            if ($search_type == 'member') {
                $data['result'] = 1;
                $response = '<ul>';
                $members = $this->profile_model->search_members_limited($input_value);
                if (!empty($members)) {
                    foreach ($members as $member) {
                        $response .= '<li><a href="' . $lang_base_url . 'profile' . '/' . $member->slug . '">' . $member->username . '</a></li>';
                    }
                } else {
                    $response .= '<li><a href="' . $lang_base_url . 'members?search=' . $input_value . '">' . $input_value . '</a></li>';
                }
                $response .= '</ul>';
                $data['response'] = $response;
            } else {
                $data['result'] = 1;
                $response = '<div class="suggestions__group-content">';
                $products = $this->product_model->search_products($input_value);
                if (!empty($products)) {
                    $response .= '<div style="margin-top: 5px;" class="suggestions__group-title">Arama Sonucu</div>';
                    foreach ($products as $product) {
                        $response .='

                                <a class="suggestions__item suggestions__product" href="' . $lang_base_url . $product->slug . '">
                                    <div class="suggestions__product-image"><img width="40"
                                       src="' .base_url("uploads/images/".$product->image_small) . '" alt="' . $product->title . '"></div>
                                    <div class="suggestions__product-info">
                                       <div class="suggestions__product-name">' . $product->title . '</div>
                                       <div class="suggestions__product-rating">
                                          <div class="suggestions__product-rating-label">Uygun Fiyatları Görmek İçin İnceleyin </div>
                                       </div>
                                    </div>
                                    <div style="margin-right: 10px;" class="suggestions__product-price">'.fiyatsay($product->id).' Fiyat Bulundu</div>
                                 </a>




                        ';
                    }
                } else {
                    $response .= '<div style="margin-top: 5px;" class="suggestions__group-title">Arama Sonucu</div>';
                    $response .= '<div class="suggestions__group-content"><a class="suggestions__item suggestions__category" href="' . $lang_base_url . 'products?search=' . $input_value . '">' . $input_value . '</a></div>';
                }
                $response .= '</div>';
                $data['response'] = $response;
            }
        }
        echo json_encode($data);
    }


Result

在此处输入图像描述

How can I do? Thanks in advance!

I found the solution:)

$this->db->group_by('images.product_id'); 

    public function search_products($search)
    {
        $search = remove_special_characters($search);
        $this->db->join('products', 'products.id = images.product_id');
        $this->db->like('products.title', $search);
        $this->db->group_by('images.product_id'); 
        $this->db->order_by('products.is_promoted', 'DESC');
        $this->db->limit(8);
        $query = $this->db->get('images');
        return $query->result();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM