【问题标题】:PHP class inheriting Codeigniter继承 Codeigniter 的 PHP 类
【发布时间】:2016-05-14 13:56:49
【问题描述】:

我是 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”。

注意:我做了所有配置以连接到数据库

【问题讨论】:

    标签: php database codeigniter


    【解决方案1】:

    你错过了你的魔法__construct 函数

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

    【讨论】:

      【解决方案2】:

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

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

      示例控制器:

      <?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();
              }
      }
      

      【讨论】:

        【解决方案3】:
        1. 删除此&lt;&lt;!DOCTYPE html&gt;
        2. 在您的 getter(例如 getProduactName)中使用 return not echo
        3. 而不是setAttribute set __construct
        4. 扩展 codeIgniter 模型

        代码最类似于:

        <?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);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          • 2011-07-10
          • 1970-01-01
          • 1970-01-01
          • 2015-08-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多