Cairo-Coders is the one place for high quality web development, Web Design and software development tutorials and Resources programming. Learn cutting edge techniques in web development, design and software development, download source components and participate in the community.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pagenation extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries");
$this->load->library("pagination");
}
public function pagenum()
{
$config = array();
$config["base_url"] = base_url() . "pagenation/pagenum";
$config["total_rows"] = $this->Countries->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 3;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
$data["results"] = $this->Countries->fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("expagenation", $data);
}
}
Pagination Model (Countries.php) application\models\Countries.php
<?php
class Countries extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("country");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("country");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
create expagenation.php file under folder name application/views/expagenation.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class My404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this-<output-<set_status_header('404');
$this-<load-<view('err404');
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Chat extends CI_Controller {
/* The default function that gets called when visiting the page */
public function index() {
$this->load->view('chat-view');
}
public function get_chats() {
/* Connect to the mySQL database - config values can be found at:
/application/config/database.php */
$dbconnect = $this->load->database();
/* Load the database model:
/application/models/simple_model.php */
$this->load->model('Chat_model');
/* Create a table if it doesn't exist already */
$this->Chat_model->create_table();
echo json_encode($this->Chat_model->get_chat_after($_REQUEST["time"]));
}
public function insert_chat() {
/* Connect to the mySQL database - config values can be found at:
/application/config/database.php */
$dbconnect = $this->load->database();
/* Load the database model:
/application/models/simple_model.php */
$this->load->model('Chat_model');
/* Create a table if it doesn't exist already */
$this->Chat_model->create_table();
$this->Chat_model->insert_message($_REQUEST["message"]);
}
public function time() {
echo "[{\"time\":" + time() + "}]";
}
}?>
Models chat_model.php
class Chat_model extends CI_Model {
function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function get_last_item()
{
$this->db->order_by('id', 'DESC');
$query = $this->db->get('Chats', 1);
return $query->result();
}
function insert_message($message)
{
$this->message = $message;
$this-> time = time();
$this->db->insert('Chats', $this);
}
function get_chat_after($time)
{
$this->db->where('time >', $time)->order_by('time', 'DESC')->limit(10);
$query = $this->db->get('Chats');
$results = array();
foreach ($query->result() as $row)
{
$results[] = array($row->message,$row->time);
}
return array_reverse($results);
}
function create_table()
{
/* Load db_forge - used to create databases and tables */
$this->load->dbforge();
/* Specify the table schema */
$fields = array(
'id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'message' => array(
'type' => 'TEXT'
),
'time' => array(
'type' => 'INT'
)
);
/* Add the field before creating the table */
$this->dbforge->add_field($fields);
/* Specify the primary key to the 'id' field */
$this->dbforge->add_key('id', TRUE);
/* Create the table (if it doesn't already exist) */
$this->dbforge->create_table('Chats', TRUE);
}
}
Views chat-view.php
<html>
<head>
<title> Chat Exmaples! </title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
var time = 0;
var updateTime = function (cb) {
$.getJSON("index.php/chat/time", function (data) {
cb(~~data);
});
};
var sendChat = function (message, cb) {
$.getJSON("index.php/chat/insert_chat?message=" + message, function (data){
cb();
});
}
var addDataToReceived = function (arrayOfData) {
arrayOfData.forEach(function (data) {
$("#received").val($("#received").val() + "\n" + data[0]);
});
}
var getNewChats = function () {
$.getJSON("index.php/chat/get_chats?time=" + time, function (data){
addDataToReceived(data);
// reset scroll height
setTimeout(function(){
$('#received').scrollTop($('#received')[0].scrollHeight);
}, 0);
time = data[data.length-1][1];
});
}
// using JQUERY's ready method to know when all dom elements are rendered
$( document ).ready ( function () {
// set an on click on the button
$("form").submit(function (evt) {
evt.preventDefault();
var data = $("#text").val();
$("#text").val('');
// get the time if clicked via a ajax get queury
sendChat(data, function (){
alert("dane");
});
});
setInterval(function (){
getNewChats(0);
},1500);
});
</script>
</head>
<body>
<h1> Chat Example on Codeigniter </h1>
<textarea id="received" rows="10" cols="50">
</textarea>
<form>
<input id="text" type="text" name="user">
<input type="submit" value="Send">
</form>
</body>
</html>
Copy the code given below and store it at application/controllers/upload.php. Create "uploads" folder at the root of CodeIgniter i.e. at the parent directory of application folder.
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
Make the following change in the route file in application/config/routes.php and add the following line at the end of file.
$route['upload'] = 'Upload';
Now let us execute this example by visiting the following URL in the browser. Replace the yoursite.com with your URL.