codeigniter视图,添加,更新和删除

时间:2022-10-07 00:09:39

I'm newbie in codeigniter and still learning. Anyone can help for sample in basic view, add, update, delete operation and queries in codeigniter will gladly appreciated.

我是codeigniter的新手,还在学习。任何人都可以帮助您在基本视图中进行示例,添加,更新,删除操作以及在codeigniter中查询将很高兴。

Just a simple one like creating addressbook for newbie.

只是一个简单的例如为新手创建地址簿。

thanks,

谢谢,

best regards

最好的祝福

2 个解决方案

#1


5  

Some sample queries in Codeigniter

Codeigniter中的一些示例查询

class Names extends Model {
  function addRecord($yourname) {
    $this->db->set("name", $yourname);
    $this->db->insert("names");
    return $this->db->_error_number(); // return the error occurred in last query
  }
  function updateRecord($yourname) {
    $this->db->set("name", $yourname);
    $this->db->update("names");
  }
  function deleteRecord($yourname) {
    $this->db->where("name", $yourname);
    $this->db->delete("names");
  }
  function selectRecord($yourname) {
    $this->db->select("name, name_id");
    $this->db->from("names");
    $this->db->where("name", $yourname);
    $query = $this->db->get();
    return $this->db->result();
  }
  function selectAll() {
     $this->db->select("name");
     $this->db->from("names");
     return $this->db->get();
  }
}

More information and more ways for CRUD in codeigniter active record documentation More about error number over here

有关codeigniter活动记录文档中CRUD的更多信息和更多方法有关此处错误号的更多信息

A sample controller

样本控制器

class names_controller extends Controller {
   function addPerson() {
      $this->load->Model("Names");
      $name = $this->input->post("name"); // get the data from a form submit
      $name = $this->xss->clean();
      $error = $this->Names->addRecord($name);
      if(!$error) {
         $results = $this->Names->selectAll();
         $data['names'] = $results->result();
         $this->load->view("show_names", $data);
      } else {
         $this->load->view("error");
      }
   }
}

More about controllers over here

更多关于这里的控制器

A sample view - show_names.php

示例视图 - show_names.php

<table>
  <tr>
    <td>Name</td>
  </tr>
  <?php foreach($names as $row): ?>
  <tr><td><?ph echo $row->name; ?></td></tr>
  <?php endforeach; ?>
</table>

More about codeigniter views over here

有关此处的codeigniter视图的更多信息

#2


3  

You can use this as an example

您可以将此作为示例

class Crud extends Model {
    // selecting records by specifying the column field
    function select()
    {
        // use $this->db->select('*') if you want to select all the records
        $this->db->select('title, content, date');
        // use $this->db->where('id', 1) if you want to specify what row to be fetched
        $q = $this->db->get('mytable');

        // to get the result
        $data = array();
        // for me its better to check if there are records that are fetched
        if($q->num_rows() > 0) { 
            // by doing this it means you are returning array of records
            foreach($q->result_array() as $row) {
                $data[] = $row;
            }
            // if your expecting only one record will be fetched from the table
            // use $row = $q->row();
            // then return $row;
        }
        return $data;
    }

    // to add record
    function add()
    {
        $data = array(
           'title' => 'My title' ,
           'name' => 'My Name' ,
           'date' => 'My date'
        );

        $this->db->insert('mytable', $data); 
    }

    // to update record
    function update()
    {
        $data = array(
           'title' => $title,
           'name' => $name,
           'date' => $date
        );

        $this->db->where('id', 1);
        $this->db->update('mytable', $data); 
    }

    // to delete a record
    function delete()
    {
        $this->db->where('id', 1);
        $this->db->delete('mytable');
    }
}

Some of this are from codeigniter userguide.

其中一些来自codeigniter用户指南。

To view the records,

要查看记录,

If return data is array of records,

如果返回数据是记录数组,

   foreach($data as $row)
   {
       echo $row['title'] . "<br />";
   }

If the return data is an object (by using $q->row),

如果返回数据是一个对象(通过使用$ q-> row),

   echo $data->title;

This is just a few examples or CRUD in Codeigniter. Visit the codeigniter userguide.

这只是Codeigniter中的一些示例或CRUD。访问codeigniter用户指南。

#1


5  

Some sample queries in Codeigniter

Codeigniter中的一些示例查询

class Names extends Model {
  function addRecord($yourname) {
    $this->db->set("name", $yourname);
    $this->db->insert("names");
    return $this->db->_error_number(); // return the error occurred in last query
  }
  function updateRecord($yourname) {
    $this->db->set("name", $yourname);
    $this->db->update("names");
  }
  function deleteRecord($yourname) {
    $this->db->where("name", $yourname);
    $this->db->delete("names");
  }
  function selectRecord($yourname) {
    $this->db->select("name, name_id");
    $this->db->from("names");
    $this->db->where("name", $yourname);
    $query = $this->db->get();
    return $this->db->result();
  }
  function selectAll() {
     $this->db->select("name");
     $this->db->from("names");
     return $this->db->get();
  }
}

More information and more ways for CRUD in codeigniter active record documentation More about error number over here

有关codeigniter活动记录文档中CRUD的更多信息和更多方法有关此处错误号的更多信息

A sample controller

样本控制器

class names_controller extends Controller {
   function addPerson() {
      $this->load->Model("Names");
      $name = $this->input->post("name"); // get the data from a form submit
      $name = $this->xss->clean();
      $error = $this->Names->addRecord($name);
      if(!$error) {
         $results = $this->Names->selectAll();
         $data['names'] = $results->result();
         $this->load->view("show_names", $data);
      } else {
         $this->load->view("error");
      }
   }
}

More about controllers over here

更多关于这里的控制器

A sample view - show_names.php

示例视图 - show_names.php

<table>
  <tr>
    <td>Name</td>
  </tr>
  <?php foreach($names as $row): ?>
  <tr><td><?ph echo $row->name; ?></td></tr>
  <?php endforeach; ?>
</table>

More about codeigniter views over here

有关此处的codeigniter视图的更多信息

#2


3  

You can use this as an example

您可以将此作为示例

class Crud extends Model {
    // selecting records by specifying the column field
    function select()
    {
        // use $this->db->select('*') if you want to select all the records
        $this->db->select('title, content, date');
        // use $this->db->where('id', 1) if you want to specify what row to be fetched
        $q = $this->db->get('mytable');

        // to get the result
        $data = array();
        // for me its better to check if there are records that are fetched
        if($q->num_rows() > 0) { 
            // by doing this it means you are returning array of records
            foreach($q->result_array() as $row) {
                $data[] = $row;
            }
            // if your expecting only one record will be fetched from the table
            // use $row = $q->row();
            // then return $row;
        }
        return $data;
    }

    // to add record
    function add()
    {
        $data = array(
           'title' => 'My title' ,
           'name' => 'My Name' ,
           'date' => 'My date'
        );

        $this->db->insert('mytable', $data); 
    }

    // to update record
    function update()
    {
        $data = array(
           'title' => $title,
           'name' => $name,
           'date' => $date
        );

        $this->db->where('id', 1);
        $this->db->update('mytable', $data); 
    }

    // to delete a record
    function delete()
    {
        $this->db->where('id', 1);
        $this->db->delete('mytable');
    }
}

Some of this are from codeigniter userguide.

其中一些来自codeigniter用户指南。

To view the records,

要查看记录,

If return data is array of records,

如果返回数据是记录数组,

   foreach($data as $row)
   {
       echo $row['title'] . "<br />";
   }

If the return data is an object (by using $q->row),

如果返回数据是一个对象(通过使用$ q-> row),

   echo $data->title;

This is just a few examples or CRUD in Codeigniter. Visit the codeigniter userguide.

这只是Codeigniter中的一些示例或CRUD。访问codeigniter用户指南。