[eluser]khmer angkor[/eluser]
First of all i am no expert on neither of them, but just know enough to get what i need from them. If you dont agree with the practices here, please feel free to provide constructive feedback.
Now i will assume you have knowledge and experience with CodeIgniter since i will only be posting the functions that are used on this example. I wont be defining what a controller, model or view is since you should know if you have worked with CI.
1-Creat the sample database with this infomation:
Code:
DROP TABLE IF EXISTS mxt_download_cat;
CREATE TABLE mxt_download_cat (
cat_id int(11) unsigned NOT NULL auto_increment,
cat_name varchar(120) DEFAULT '',
PRIMARY KEY (cat_id)
) TYPE=MyISAM;
DROP TABLE IF EXISTS mxt_download_file;
CREATE TABLE mxt_download_file (
file_id int(11) unsigned NOT NULL auto_increment,
file_catid int(11) DEFAULT '0',
file_name varchar(120) DEFAULT '',
PRIMARY KEY (file_id)
) TYPE=MyISAM;
=>now i did not guide you how to confin url and the Database. i think u knew it
2-Creat the file in view :/download/
v_main_vcat.php
Code:
<div id="center_content">
<?=$pagination?>
<ul class="ul_main">
<?php foreach($search_resultscat->result() as $cat):?>
<li>in:<?echo nbs(3)?><b><?php echo $cat->cat_name;?><?echo nbs(3)?></b>category<?echo nbs(3)?>
<?php echo"Cat ID:".$cat->cat_id;?></li>
<?php endforeach;?>
<?php foreach($search_results->result() as $client):?>
<li>file id:<b><?php echo $client->file_id;?></b><?echo nbs(3)?>
<?php echo"file name:".$client->file_name;?></li>
<?php endforeach;?>
</ul>
<ul class="ul_main">
</ul>
</div>
3-create a controler : main.php in controller
Code:
<?class Main extends Controller {
function Main()
{
parent::Controller();
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
}
function viewcat()
{
//-------- LOAD TASKBAR MENU --------------------------------
$data['title_site'] = 'Home';
//-------- LOAD TASKBAR MENU --------------------------------
$this->load->library('pagination');
$url = $this->uri->segment(3); // get var from url
//$this->pagination->initialize($config);
$base_url = $this->config->item('base_url');
$config['base_url'] = ''.$base_url.'main/viewcat/'.$url.'';
$query = $this->db->where('file_catid', $url)->orderby('file_id', 'desc')->get('mxt_download_file');
$querycat = $this->db->where('cat_id', $url)->orderby('cat_id', 'desc')->get('mxt_download_cat');
$num = $query->num_rows();
$config['total_rows'] = $num;
$config['per_page'] = '10';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$config['uri_segment'] = 4;
//$data['search_results'] = $query;
$this->pagination->initialize($config);
echo $this->uri->segment(4);
//load the model and get results
$this->load->model('m_main');
$data['search_results'] =$this->m_main->get_files($config['per_page'],$this->uri->segment(4));
$data['search_resultscat'] = $this->m_main->get_cat($config['per_page'],$this->uri->segment(4));
$data['pagination'] = $this->pagination->create_links();
//-------- views --------------------------------------------
$this->load->view('download/v_main_vcat', $data);
//return $query;
//-------- --------------------------------------------------
}
}?>
4-Create a Model: m_main.php in Model
Code:
<? class M_Main extends Model {
function M_Main(){
parent::Model();
}
function get_files($num, $offset) {
$url = $this->uri->segment(3);
$query = $this->db->where('file_catid', $url)->orderby('file_id', 'desc')->get('mxt_download_file', $num, $offset);
return $query;
}// end function
function get_cat($num, $offset) {
$url = $this->uri->segment(3);
$querycat = $this->db->where('cat_id', $url)->orderby('cat_id', 'desc')->get('mxt_download_cat', $num, $offset);
return $querycat;
}
}?>
Check the result:
http://yourdomain/index.php/main/viewcat/n
With
n is your
cat_id.
in my sample database: n=1 or n=2 and run on localhost
http://localhost/index.php/main/viewcat/1
or
http://localhost/index.php/main/viewcat/2
this is my sample Database.