CodeIgniter

Link zur Dokumentation

Model: Datenbank CRUD

View: Was man sieht

Controller: Zwischenstück

class Welcome extends CI_Controller {
		  
  public function index() {
    $this->load->view('welcome_message');
  }
}

  public function test() {
    echo "hello";
  }

// Model: User_model.php
		  
		  class User_model extends CI_Model {
	
	public function get_users() {
		
		$query = $this->db->get('users');
		
		return $query->result();
		
	}
} 
// Controller: Users.php
class Users extends CI_Controller {
	
	function show() {
		//$this->load->model('user_model');
		
		$data['results'] = $this->user_model->get_users();
		
		
		$this->load->view('user_view',$data);

	}
	
}
  
// View: user_view.php
		
		foreach($results as $object) {
				echo $object->username;
				echo "
"; }
class User_model extends CI_Model {
	
	public function get_users() {
		
		$query = $this->db->query("SELECT * FROM users");
		
		return $query->num_rows(); // row count
		return $query->num_fields(); // column count
	}
}
// Model: User_model.php
		  class User_model extends CI_Model {
	
	
	public function get_users($user_id) {
		
		$this->db->where('id',$user_id);
		$query = $this->db->get('users');
		
		return $query->result();
		
	}
}
// Controller: Users.php
		class Users extends CI_Controller {
	
	function show($user_id) {
		//$this->load->model('user_model');
		
		$data['results'] = $this->user_model->get_users($user_id);
		
		
		$this->load->view('user_view',$data);
	}
	
}  
		  ////// https://codeguru.ch/ci/index.php/users/show/1
		  
class User_model extends CI_Model {
	
	
	public function get_users($user_id) {
		
		$this->db->where(['id' => $user_id, 'username' => $username]);
		$query = $this->db->get('users');
		
		return $query->result();
		
	}
class User_model extends CI_Model {
	
	
	public function get_users($user_id) {
		
		$this->db->where('id', $user_id);
		$query = $this->db->get('users');
		
		return $query->result();
		
	}
	
	public function create_users($data) {
		$this->db->insert('users', $data);
	}
}
class Users extends CI_Controller {
	
	function show($user_id) {
		//$this->load->model('user_model');
		
		$data['results'] = $this->user_model->get_users($user_id);
		
		
		$this->load->view('user_view',$data);
	}
	
	function insert() {
		$username = "peter";
		$password = "secret";
		
		$this->user_model->create_users([
			'username' => $username,
			'password' => $password
		]);
	}
	
}
class User_model extends CI_Model {
	
	
	public function get_users($user_id) {
		
		$this->db->where('id', $user_id);
		$query = $this->db->get('users');
		
		return $query->result();
		
	}
	
	public function create_users($data) {
		$this->db->insert('users', $data);
	}
	
	public function update_users($data, $id) {
		$this->db->where(['id' => $id]);
		$this->db->update('users', $data);
	}
	
	public function delete_users($id) {
		$this->db->where(['id' => $id]);
		$this->db->delete('users');
	}
}

class Users extends CI_Controller {
	
	function show($user_id) {
		//$this->load->model('user_model');
		
		$data['results'] = $this->user_model->get_users($user_id);
		
		
		$this->load->view('user_view',$data);
	}
	
	function insert() {
		$username = "peter";
		$password = "secret";
		
		$this->user_model->create_users([
			'username' => $username,
			'password' => $password
		]);
	}
	
	function update() {
		$id = 3;
		$username = "william";
		$password = "not so secret";
		
		$this->user_model->update_users([
			'username' => $username,
			'password' => $password
		], $id);
	}
	
	function delete() {
		$id = 3;
		$this->user_model->delete_users($id);
	}
	
	
}