PHP类继承Codeigniter

时间:2016-05-14 13:56:49

标签: php database codeigniter

我是PHP的新手,我正在使用Codeigniter。我有一个PHP文件,我做了以下OOP:

<<!DOCTYPE html>
<?php
 class Producat 
 {
     var $price;
     var $title;
     var $name;

   public function setProducatName ($nam)
   {
    $this->name=$nam;
   } 

   public function getProduactName()
  {
    echo $this->name;
  }
  public function setAttribute ($price1,$title1) 
  {
    $this->price=$price1;
    $this->title=$title1;

  }

class  Fruniture extends Producat 
 {
    var $size;
    var $material;
 } 
  class  Cddvd extends Producat 
 {
    var $size;
    var $manufacuter;
 }

我的phpmyadmin中有一个名为saied的数据库。

我正在尝试将产品的标题插入数据库,然后从类中的数据库中获取名称。

  1. like:如何连接到类中的数据库。如何在产品类中与数据库进行通信,但最好是将所有数据库功能分开
  2. 我需要材料属性才有2个选择(塑料或木材)?
  3. 获取所有产品属性的功能CD / DVD的大小属性应始终附加“MB”。
  4. 注意:我将所有配置连接到数据库

3 个答案:

答案 0 :(得分:0)

你错过了神奇的__construct功能

http://php.net/manual/en/language.oop5.decon.php

答案 1 :(得分:0)

这不是使用CI php框架的确切方法。 首先,您需要了解基础知识。

  1. Controller - 这是一个处理uri的类。
  2. 模型 - 与数据库通信的类。
  3. 视图 - 显示HTML。
  4. 示例控制器:

    <?php
    class Product extends CI_Controller {
    
        public function index()
        {
            echo 'Hello World!';
            $this->load->view("products");
        }
    }
    ?>
    

    示例模型:

       class Product extends CI_Model {
    
            var $title   = '';
            var $content = '';
            var $date    = '';
    
            function __construct()
            {
                // Call the Model constructor
                parent::__construct();
            }
    
            function get_all()
            {
                $query = $this->db->get('entries');
                return $query->result();
            }
    }
    

答案 2 :(得分:0)

  1. 删除此<<!DOCTYPE html>
  2. 你的getter中的
  3. (例如getProduactName)使用return not echo
  4. 而不是setAttribute set __construct
  5. 扩展codeIgniter模型
  6. 代码最像是:

    <?php
     class Producat extends CI_Model
     {
         var $price;
         var $title;
         var $name;
    
    function __construct($price1,$title1) {
    parent::__construct();
     $this->price=$price1;
        $this->title=$title1;
    }
       public function setProducatName ($nam)
       {
        $this->name=$nam;
       } 
    
       public function getProduactName()
      {
        return $this->name;
      }
      public function setAttribute ($price1,$title1) 
      {
        $this->price=$price1;
        $this->title=$title1;
    
      }
    function insert_indb(){
    $data[] = ["title"=>$this->title];
    $data[] = ["name"=>$this->name];
    $data[] = ["price"=>$this->price];
    $this->db->insert('students', $data);
    }