控制器代码我有:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="vertical"
android:weightSum="4">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:background="@android:drawable/screen_background_dark_transparent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="@+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:paddingBottom="@dimen/songs_count_padding_bottom"
android:visibility="invisible"
android:paddingLeft="@dimen/album_title_padding"
android:paddingRight="@dimen/album_title_padding"
android:textSize="@dimen/songs_count" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
模型代码我有:
<?php
class Latest_ctrl extends Ci_controller{
public function insert(){
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data=array(
'name'=>$name,
'pass'=>$pass,
'email'=>$email,
'mobile'=>$mobile,
'address'=>$address
);
$this->load->model('latest_model');
$query= $this->db->insert('form',$data);
if($query){
redirect('latest_ctrl/view');
}
}
public function view(){
$this->load->model('latest_model');
$val=$this->latest_model->get_data();
$data['value']=$val;
$this->load->view('latest',$data);
}
public function index(){
$this->load->view('new_login');
}
public function delete($id){
$id=$this->uri->segment(3);
$this->load->model('latest_model');
$this->latest_model->delete_id($id);
redirect(base_url('latest_ctrl/view'));
}
Public function update($id){
$upd=$this->uri->segment(3);
$data = array(
'name' => $this->input->post('name'),
'pass' => $this->input->post('pass'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'address' => $this->input->post('address')
);
$this->load->model('latest_model');
}
}
?>
查看我:
<?php
class Latest_model extends CI_model{
public function insert($tableName,$data){
return $this->db->insert($tableName, $data);
}
public function get_data(){
$query = $this->db->get('form');
if ( $query->num_rows() > 0 )
{
$row=$query->result();
return $row;
}
}
public function delete_id($id){
$this->db->where('id', $id);
$this->db->delete('form');
}
public function update_data(){
}
}
?>
我想为此目的编辑特定记录,我需要模型和控制器代码来更新codeigniter中的记录。
此外,我有一个页面将页面重定向到更新控制器,但我只想更新模型和控制器的代码以进行更新。
答案 0 :(得分:0)
**Controller**
- &GT;在此处添加构造和加载模型,以便您无法在每个函数中加载模型
<?php
class Latest_ctrl extends CI_controller{
function __construct()
{
parent::__construct();
$this->load->model('latest_model');
}
public function insert()
{
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data=array(
'name'=>$name,
'pass'=>$pass,
'email'=>$email,
'mobile'=>$mobile,
'address'=>$address
);
$id = $this->latest_model->insert($tableName="form",$data);
if($id)
{
redirect('latest_ctrl/view');
}
}
public function view()
{
$val=$this->latest_model->get_data();
$data['value']=$val;
$this->load->view('latest',$data);
}
public function index(){
$this->load->view('new_login');
}
public function delete($id)
{
$id=$this->uri->segment(3);
$this->latest_model->delete_id($id);
redirect(base_url('latest_ctrl/view'));
}
Public function update($id)
{
$id=$this->uri->segment(3);
$data = array(
'name' => $this->input->post('name'),
'pass' => $this->input->post('pass'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'address' => $this->input->post('address')
);
$this->latest_model->update_data($tableName="form",$data,$id);
}
}
?>
型号代码
<?php
class Latest_model extends CI_model{
public function insert($tableName,$data)
{
$this->db->insert($tableName, $data);
return $this->db->insert('form',$data);
}
public function get_data()
{
$query = $this->db->get('form');
if ( $query->num_rows() > 0 )
{
$row=$query->result();
return $row;
}
}
public function delete_id($id)
{
$this->db->where('id', $id);
$this->db->delete('form');
}
public function update_data($tableName,$data,$id)
{
$this->db->where('id'=>$id)
$this->db->update($tableName, $data);
return true;
}
}
?>