PHP code example of faqzul / codeigniter-crud-model
1. Go to this page and download the library: Download faqzul/codeigniter-crud-model library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class Welcome extends CI_Controller {
public function __construct() { parent::__construct(); }
public function list($page = 0) {
$a = $this->crud->readData('*', 'users')->result();
// This method returns the query result as an array of objects, or an empty array on failure. Typically you’ll use this in a foreach loop.
// Executes: SELECT * FROM users
$where = array('username' => 'FaqZul');
$b = $this->crud->readData('*', 'users', $where)->row();
// This method returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object.
// Executes: SELECT * FROM users WHERE username = 'FaqZul'
$join = array('user_profiles' => 'users.id = user_profiles.user_id');
$where = array('username !=' => 'FaqZul');
$c = $this->crud->readData('*', 'users', $where, $join, '', 'users.id DESC', array(10, $page * 10))->result_array();
// This method returns the query result as a pure array, or an empty array when no result is produced. Typically you’ll use this in a foreach loop.
// Executes: SELECT * FROM users JOIN user_profiles ON users.id = user_profiles.user_id WHERE username != 'FaqZul' ORDER BY users.id DESC LIMIT 10
}
public function search($q = '') {
/* You can use more specific LIKE queries. */
$like = array(array('like' => array('user_name' => $q)), 'or_like' => array('user_email' => $q));
/* Or You can use it. */
$like = array('or_like' => array('user_name' => $q, 'user_email' => $q));
// Executes: WHERE user_name LIKE '%$q%' OR user_email LIKE '%$q%'
$a = $this->crud->readData('*', 'users', $like)->result_array();
echo json_encode($a);
}
public function user() {
/* Only show users who are guests or owners. */
$where = ['or_where' => [['role' => 'guest'], ['role' => 'owner']]];
$a = $this->crud->readData('*', 'users', $where)->result_array();
echo json_encode($a);
}
// See: https://codeigniter.com/user_guide/database/query_builder.html#query-grouping
public function search_group() {
$where = ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd'];
$this->crud->group_set([0 => 'group_start', 1 => 'or_group_start']);
$this->crud->group_end([2 => 2]);
echo $this->crud->readDataQuery('*', 'mytable', $where);
// Print: SELECT * FROM "mytable" WHERE ( "a" = 'a' OR ( "b" = 'b' AND "c" = 'c' ) ) AND "d" = 'd'
// Search username administrator or superadmin in role admin
$where = ['role' => 'admin', 'or_where' => [['username' => 'administrator'], ['username' => 'superadmin']]];
echo $this->crud->group_set([1 => 'group_start'])->group_end([1 => 1])->readDataQuery('*', 'user', $where);
// Print: SELECT * FROM "user" WHERE "role" = 'admin' AND ( "username" = 'administrator' OR "username" = 'superadmin' )
}
}
class Welcome extends CI_Controller {
public function __construct() { parent::__construct(); }
public function edit($id = 0) {
$data = array('link' => 'https://github.com/FaqZul/CodeIgniter-CRUD-Model');
echo ($this->crud->updateData('user_profiles', $data, array('user_id' => $id))) ? 'Success updating data.': $this->crud->error_message();
}
}
class Welcome extends CI_Controller {
public function __construct() { parent::__construct(); }
public function delete($id = 0) {
if ($this->crud->deleteData('users', array('id' => $id))) {
// Success deleting data.
$this->crud->deleteData('user_profiles', array('user_id' => $id));
}
else {
// Fail deleting data.
echo var_dump($this->crud->error());
}
}
}
class Blog_model extends Crud {
public function __construct() {
parent::__construct();
// You can initialize crud in here.
// $this->initialize($config);
}
public function get_entries() {
$query = $this->readData('*', 'entries');
return $query->result_array();
}
public function insert_entry() {
$insert = array(
'title' => $_POST['title'], // please read the below note.
'content' => $_POST['content'],
'date' => time()
);
$this->createData('entries', $insert);
}
public function update_entry() {
$update = array(
'title' => $_POST['title'], // please read the below note.
'content' => $_POST['content'],
'date' => time()
);
$this->updateData('entries', $update, array('id' => $_POST['id']));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.