MySQL DDL Quer
Create a blogs table using sql below:
CREATE TABLE `db_name`.`blog` (
`blog_id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 200 ) NOT NULL ,
`desc` TEXT NOT NULL ,
`created` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL
) ENGINE = MYISAM
CodeIgniter Model
<?php
class
blog
extends
Model
{
function
blog()
{
parent::Model();
$this
->load->database();
}
function
count_blogs()
{
// Count total numbers of blog entries
$query
=
$this
->db->query(
'SELECT count(*) as count_blog FROM blogs'
);
$result
=
$query
->results();
return
$result
[0]->count_blog;
}
function
select_blogs(
$num
,
$offset
)
{
// Fetch data from blogs table
$query
=
$this
->db->get(
'blogs'
,
$num
,
$offset
);
$result
=
$query
->results();
return
$result
;
}
}
?>
CodeIgniter Controller
<?php
class
blog
extends
Controller
{
function
blog() {
parent::Controller();
$this
->load->library(‘pagination’);
$this
->load->model(‘blog’);
$this
->load->helper(‘url’);
}
function
bloglist() {
$config
[
'base_url'
] = ‘http:
//your-site.com/index.php/test/page/’;
$config
[
'per_page'
]=10;
$config
[
'total_rows'
] =
$data
[
'count_blog'
];
$this
->pagination->initialize(
$config
);
$data
[
'title'
]=’CodeIgniter Pagination Example’;
$data
[
'bloglist'
] =
$this
->blog->select_blogs(
$config
[
'per_page'
],
$this
->uri->segment(3));
$data
[
'count_blog'
]=
$this
->blog->count_blogs();
// This will generate your pagination links
$data
[
'page_links'
]=
$this
->pagination->create_links();
$this
->load->view(‘blog/index’,
$data
);
}
}
?>
CodeIgniter View
<table>
<?php
for
(
$i
=0;
$i
<
count
(
$bloglist
);
$i
++) : ?>
<tr>
<td><?php
echo
$bloglist
[
$i
]->title; ?></td>
</tr>
<tr>
<td><?php
echo
$bloglist
[
$i
]->desc; ?></td>
</tr>
<?php
echo
endfor
; ?>
<tr>
<td><?php
echo
$page_links
; ?></td>
</tr>
</table>
replace echo endfor; with endfor;
ReplyDelete