Codeigniter过滤产品表的类别ID

时间:2017-04-29 16:55:32

标签: php filter categories codeigniter-3

我有这个代码从数据库中的表中提取所有值,但它工作正常,但我不想要显示所有产品。我希望按category_id进行过滤。表的名称称为products。以下是显示所有产品的视图文件:



<?php foreach($products as $product) : ?>
	<div class="col-md-4 game">
		<div class="game-price"><?php echo $product->price; ?></div>
			<a href="<?php echo base_url(); ?>products/details/<?php echo $product->id; ?>">
				<img src="<?php echo base_url(); ?>assets/images/products/<?php echo $product->image; ?>" />
			</a>
			<div class="game-title">
				<?php echo $product->title; ?>
			</div>
			<div class="game-add">
				<form method="post" action="<?php echo base_url(); ?>cart/add">
							QTY: <input class="qty" type="text" name="qty" value="1" /><br>
							<input type="hidden" name="item_number" value="<?php echo $product->id; ?>" />
							<input type="hidden" name="price" value="<?php echo $product->price; ?>" />
							<input type="hidden" name="title" value="<?php echo $product->title; ?>" />
							<button class="btn btn-primary" type="submit">Add To Cart</button>
						</form>
			</div>
	</div>
<?php endforeach; ?>
&#13;
&#13;
&#13;

这是我桌子的截图: Database Screenshot

如何编辑上面的代码,以便显示特定类别的结果?

这是我的控制器:

&#13;
&#13;
<?php
class Products extends CI_Controller{
	public function index(){
		//Get All Products
		$data['products'] = $this->Product_model->get_products();

		//Load View
		$data['main_content'] = 'products';
		$this->load->view('layouts/main', $data);
	}

	public function details($id){
		//Get Product Details
		$data['product'] = $this->Product_model->get_product_details($id);

		//Load View
		$data['main_content'] = 'details';
		$this->load->view('layouts/main', $data);
	}

	public function __construct()
    {
        parent::__construct();

        $this->load->helper('form');

        $this->load->model('Search_model');
    }

    public function execute_search()
    {
        // Retrieve the posted search term.
        $search_term = $this->input->post('search');

        // Use a model to retrieve the results.
        $data['results'] = $this->Search_model->get_results($search_term);

        // Pass the results to the view.
        $this->load->view('products',$data);
    }
}
&#13;
&#13;
&#13;

这是我的模特:

&#13;
&#13;
<?php
class Product_model extends CI_Model{
	/*
	 *	Get All Products
	 */
	 public function get_products(){
		$this->db->select('*');
		$this->db->from('products');
		$query = $this->db->get();
		return $query->result();
	 }
	 
	 /*
	  *	Get Single Product
	  */
	  public function get_product_details($id){
		$this->db->select('*');
		$this->db->from('products');
		$this->db->where('id', $id);
		
		$query = $this->db->get();
		return $query->row();
	  }
	  
	  /*
	   *	Get Categories
	   */
	   public function get_categories(){
		$this->db->select('*');
		$this->db->from('categories');
		$query = $this->db->get();
		return $query->result();
	   }
	   
	   /*
	 * Get Most Popular Products
	*/
	public function get_popular(){
		$this->db->select('P.*, COUNT(O.product_id) as total');
		$this->db->from('orders AS O');
		$this->db->join('products AS P', 'O.product_id = P.id', 'INNER');
		$this->db->group_by('O.product_id');
		$this->db->order_by('total', 'desc');
		$query = $this->db->get();
		return $query->result();
	}
	
	/*
	 *	Add Order To Database
	 */
	 public function add_order($order_data){
		$insert = $this->db->insert('orders', $order_data);
        return $insert;
	}
}
&#13;
&#13;
&#13;

这是我的模型中实际获得产品的部分以及我认为必须编辑的部分:

&#13;
&#13;
public function get_products(){
		$this->db->select('*');
		$this->db->from('products');
		$query = $this->db->get();
		return $query->result();
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

将一个可选参数传递给

public function index($category_id = false){

然后将相同的参数传递给模型的方法

$data['products'] = $this->Product_model->get_products($category_id);

在模型中,你会检查id是否是有效值(即肯定的int然后用该子句查询DB,否则查询全部)

public function get_products($category_id){
    $this->db->select('*');
    $this->db->from('products');

    if ((int)$category_id > 0) {
        $this->db->where('category_id', $category_id);
    }

    $query = $this->db->get();
    return $query->result();
 }

这样的东西,但你应该检查一下传递整数但是cat id不存在的情况。