如何在codeigniter中更改数据变量值?

时间:2017-09-12 17:32:50

标签: php codeigniter search

我试图在codeigniter中创建搜索表单。这是我的控制器和模型,

Shop.php [控制器]

public function s(){
        $q = $this->input->get('q');
        if( $q == null ){
            $this->index();
        }
        $data['products'] = $this->product_model->search_products( $q );
        $this->load->view('shop', $data);
}

Product_model.php

public function search_products( $keyword ){

        $this->db->select('product.id, product.product_name, product.product_description, product.product_image1,
                           brand.brand_name, category.category_name, type.type_name, stock.selling_price,
                           (SELECT COALESCE(ROUND(SUM(rate)/( COUNT(rate)*5 )*100), 0) FROM rating WHERE rating.product_id = product.id)AS rating,
                           (SELECT COALESCE(COUNT(rating.id), 0) FROM rating WHERE rating.product_id = product.id)AS rate_count');
        $this->db->from('product');
        $this->db->join('stock','stock.product_id = product.id');
        $this->db->join('brand', 'brand.id = product.brand_id');
        $this->db->join('category', 'category.id = product.category_id');
        $this->db->join('type', 'type.id = product.type_id');
        $this->db->join('color', 'color.id = product.color_id');

        $this->db->where('MATCH(product.product_name) AGAINST("'.$keyword.'")');
        $this->db->or_where('MATCH(brand.brand_name) AGAINST("'.$keyword.'")');
        $this->db->or_where('MATCH(category.category_name) AGAINST("'.$keyword.'")');
        $this->db->or_where('MATCH(type.type_name) AGAINST("'.$keyword.'")');

        $this->db->where('stock.qty >', 0);
        $this->db->group_by('product.id');

        $query = $this->db->get();

        return $query->result_array();

    }

但返回值为null。所以我尝试在我的视图中调用search_product( $keyword )函数并且它有效。但是当我打印$data['product']变量并将其变为空时。

这是什么问题?谢谢。

修改

搜索结果显示在此处。

    <div class="row product-list-item">
    <?php

    // This is just used for check the query
    $p = $this->product_model->search_products( 'mesh' );

    echo '<br>';
    print_r($products);

    if( $products != null ){

        foreach ( $products as $product ){
            ?>
            <!-- item.2 -->
            <div class="product-item-element col-sm-6 col-md-6 col-lg-4">
                <!--Product Item-->
                <div class="product-item">
                    <div class="product-item-inner">
                        <div class="product-img-wrap">
                            <img src="<?php echo base_url($product['product_image1']);?>" alt="">
                        </div>
                        <div class="product-button">
                            <a href="<?php echo  site_url('shop/view/'.$product['id']);?>" class="js_tooltip" data-mode="top" data-tip="Quick&nbsp;View"><i class="fa fa-eye"></i></a>
                        </div>
                    </div>
                    <div class="product-detail">
                        <p class="product-title"><a href="<?php echo site_url('shop/view/'.$product['id'])?>">
                                <?php echo $product['product_name']; ?>
                            </a></p>
                        <div class="product-rating">
                            <div class="star-rating" itemprop="reviewRating" itemscope="" itemtype="" title="Rated 4 out of 5">
                                <span style="width: <?php echo $product['rating'];?>%"></span>
                            </div>
                            <a href="#" class="product-rating-count">
                                <span class="count"><?php echo $product['rate_count'];?></span> Reviews
                            </a>
                        </div>
                        <p class="product-description">
                            <?php echo $product['product_description']; ?>
                        </p>

                        <h5 class="item-price"><?php echo  $product['selling_price']; ?></h5>
                    </div>
                </div>
                <!-- End Product Item-->
            </div>

            <?php
        }

    }else{
        ?>

        <div class="text-center no-items">
            <h4>No items</h4>
        </div>

        <?php } ?>
</div>

0 个答案:

没有答案