简体   繁体   English

如何在codeigniter框架中实现过滤器

[英]How to implement filter in codeigniter framework

I have a Webpage here I am Displaying My Product details, in this page and I want the following requirements我在这里有一个网页,我正在显示我的产品详细信息,在此页面中,我需要以下要求

Display products should have filtering options:
         a. From lower to higher price
         b. From Higher to lower price
         c. Newly created 
         d. Alphabetical order

I would like to know how to implement the above requirements.我想知道如何实现上述要求。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CodeIgniter Shopping Cart</title>

<link href="<?php echo base_url(); ?>assets/css/core.css" media="screen" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/core1.js"></script>
</head>

<body>
    <div id="cart_data">
           <a href="<?php echo base_url(); ?>cart1/show_cart">View Cart </a>
    <div id="cart_message"> </div>
<div id="wrap">

    <?php $this->view('products1'); ?>
    <!--
    <div class="cart_list">
        <h3>Your shopping cart</h3>
        <div id="cart_content">
            <?php //echo $this->view('cart1.php'); ?>
        </div>
    </div>-->

</div>
   <!-- <p style="text-align: justify;"><div id="pagination"> <?php //echo $links; ?> </div></p>-->
    </div>
</body>
</html>

you don't say that the products are stored in a database or not.您不会说产品是否存储在数据库中。 If they are not, they probably should be.如果他们不是,他们可能应该是。

then if your products are stored in a database, it is simple to order them using the $this->db->order_by('field_name desc|asc');那么如果您的产品存储在数据库中,使用 $this->db->order_by('field_name desc|asc'); 订购它们很简单。 note you have not asked to 'filter' you have asked to 'order'请注意,您没有要求“过滤”您已经要求“订购”

so, read about the documentation about the MVC approach that codeigniter uses.因此,请阅读有关 codeigniter 使用的 MVC 方法的文档。 make a products model that will handle creating the products table, adding, deleting, and fetching the products, along with the ordering you need.制作一个产品模型,该模型将处理创建产品表、添加、删除和获取产品以及您需要的排序。 make a controller to coordinate these tasks, and create views to display the data.创建一个控制器来协调这些任务,并创建视图来显示数据。

think of the things you want the product table to do, and work out what you need to store to make that happen -- so from what you describe, the products table needs something like:想想您希望产品表做的事情,并计算出您需要存储什么来实现这一目标——因此根据您的描述,产品表需要如下内容:

    create table products (
      id int unsigned auto_increment,
      name varchar(255) not null,
      price float unsigned not null,
    primary key (id))
    ENGINE=InnoDB default CHARSET=utf8 COLLATE=utf8_unicode_ci;

then you can get the results as an array of objects with something like:然后您可以将结果作为对象数组获取,例如:

function get_records($order_by)
{
  return $this->db->from('products')
     ->order_by($order_by)
     ->get()
     ->result();
}

where order by is order by 在哪里

a: price asc a:价格升序

b: price desc b: 价格说明

c: id desc c: id 描述

d: name asc d:名称 asc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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