如何从表CodeIgniter和MySql中加总数量

时间:2018-01-06 20:45:37

标签: php mysql codeigniter

我是CodeIgniter的新手并且仍在探索它。 我只想询问是否,如何根据产品ID添加所有库存数量。 我有五张表相互关系,产品品牌类别尺寸即可。

类别品牌尺寸产品表上的外键,而产品< / strong> table是股票表中的外键。

每次有库存时,特定产品的数量和日期都会保存到数据库中。我想知道如何汇总特定产品的所有库存并显示所有总量。

数据库:产品

CREATE TABLE `products` (
  `prod_id` int(11) NOT NULL AUTO_INCREMENT,
  `prod_code` varchar(45) NOT NULL,
  `prod_desc` varchar(55) NOT NULL,
  `brand_id` int(11) DEFAULT NULL,
  `category_id` int(11) NOT NULL,
  `size_id` int(11) DEFAULT NULL,
  `original_price` decimal(10,2) NOT NULL,
  `date_registered` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `date_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`prod_id`),
  KEY `category_fk_idx` (`category_id`),
  KEY `size_fk_idx` (`size_id`),
  KEY `brand_id_idx` (`brand_id`),
  CONSTRAINT `brand_id` FOREIGN KEY (`brand_id`) REFERENCES `brand` (`brand_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `category_fk` FOREIGN KEY (`category_id`) REFERENCES `category` (`category_id`),
  CONSTRAINT `size_fk` FOREIGN KEY (`size_id`) REFERENCES `sizes` (`size_id`)
) 

股票

CREATE TABLE `stocks` (
  `stock_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) DEFAULT NULL,
  `quantity` double DEFAULT NULL,
  PRIMARY KEY (`stock_id`),
  KEY `prod_fk_idx` (`product_id`),
  CONSTRAINT `prod_fk` FOREIGN KEY (`product_id`) REFERENCES `products` (`prod_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) 

我的观点(product_view.php)

  <div class="col-sm-10" id="main">

    <div class="table-wrapper">
       <div id="content">
           <?php echo $this->session->flashdata('successMessage');
                echo $this->session->flashdata('errorMessage'); ?>
<legend class="text-danger"><h1>Product List</h1></legend>
            <?php   $tableAttr = array(
                    'table_open' => '<table class="table table-responsive table-striped table-hover" id="item_tbl">',
                    );
                                    $item_table = $this->table->set_heading('No.','PRODUCT CODE','PRODUCT DESCRIPTION','BRAND', 'CATEGORY', 'SIZE','QUANTITY','ACTION');
                $item_table = $this->table->set_template($tableAttr);
                $num = 0;
                foreach ($items as $item) {
                    $itemName = urlencode($item->prod_desc);
                    $num++;
                    $item_table = $this->table->add_row($num, $item->prod_code, $item->prod_desc, $item->brand_name,$item->category_name,$item->size,$item->quantity,"

                        <a href='".base_url("item/update/$itemName")."'><button class='btn btn-info btn-sm'>EDIT</button></a> 
                                                    <a href='".base_url("item/stock_in/$itemName")."'><button class='btn btn-primary btn-sm'>STOCK IN</button></a> 
                        ");
                }
                echo $this->table->generate($item_table);  ?>
                </div>

</div>                            
</div>  

控制器

    public function home () {
    $this->load->model('item_model');

    $data['items'] = $this->item_model->itemList();
    $data['page'] = 'home';

    $this->load->view('header',$data);
    $this->load->view('side_menu');
    $this->load->view('product_view',$data);
    $this->load->view('footer');
}

model(item_model.php)

    public function itemList () {
    $this->load->database();

$this->db->select('p.*,c.category_name,s.size,b.brand_name,t.quantity')

            ->from('stocks t');
              $this->db->join('products p','p.prod_id = t.product_id','full')
                    ->join('category c','p.category_id = c.category_id','full');
               $this->db->join('sizes s','p.size_id = s.size_id','full');
                $this->db->join('brand b','p.brand_id = b.brand_id','full');

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

    return $result;
}

我想/我想在我的网站上显示的是

+-----+--------------+--------------+-------------------+
| ID  | Product Code | Description  | Stocks Quantity   |
+-----+--------------+--------------+-------------------+
| 001 |   12345      | sample       |  12               |
| 002 |   23456      |  try         |  15               |
+-----+--------------+--------------+-------------------+

在我的数据库表中

+-----+--------------+--------------+
| ID  | Product_Id   | Quantity     |
+-----+--------------+--------------+
| 1   |   1          |     10       | 
| 2   |   3          |     7        |
+-----+--------------+--------------+
| 3   |   1          |     2        | 
| 4   |   3          |     8        |
+-----+--------------+--------------+

我希望你能帮助别人。

2 个答案:

答案 0 :(得分:0)

首先在你的控制器中你需要像你一样加载模型

$this->load->model('database');

所以将模型名称更改为其他名称以避免歧义 第二:我不知道为什么你在模型类中加载相同的模型 如果您打算加载数据库库。像这样加载

$this->load->library('database'); 

答案 1 :(得分:0)

使用SUM()和分组:

$this->db->select('p.*,c.category_name,s.size,b.brand_name,SUM(t.quantity) as total_quantity',false)
               ->from('stocks t')
               ->join('products p','p.prod_id = t.product_id','full')
               ->join('category c','p.category_id = c.category_id','full')
               ->join('sizes s','p.size_id = s.size_id','full')
               ->join('brand b','p.brand_id = b.brand_id','full')
               ->group_by('t.product_id');
          $result=$this->db->get()->result();