Welcome Guest, Not a member yet? Register   Sign In
A sample application with URL(newbie)
#1

[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">
    &lt;?=$pagination?&gt;
<ul class="ul_main">
     &lt;?php foreach($search_resultscat->result() as $cat):?&gt;
    <li>in:&lt;?echo nbs(3)?&gt;<b>&lt;?php echo $cat->cat_name;?&gt;&lt;?echo nbs(3)?&gt;</b>category&lt;?echo nbs(3)?&gt;
    &lt;?php echo"Cat ID:".$cat->cat_id;?&gt;</li>
    &lt;?php endforeach;?&gt;
&lt;?php foreach($search_results->result() as $client):?&gt;
    <li>file id:<b>&lt;?php echo $client->file_id;?&gt;</b>&lt;?echo nbs(3)?&gt;
    &lt;?php echo"file name:".$client->file_name;?&gt;</li>
    &lt;?php endforeach;?&gt;
    </ul>
    <ul class="ul_main">
    </ul>
    </div>

3-create a controler : main.php in controller

Code:
&lt;?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;
        //-------- --------------------------------------------------
    }
}?&gt;
4-Create a Model: m_main.php in Model


Code:
&lt;? 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;
}
  }?&gt;
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.
#2

[eluser]Yash[/eluser]
use [ code ] (no spaces) ...

Code:
//some code



nice info for newbie as u say
#3

[eluser]Adam Griffiths[/eluser]
You should also use full php tags.

Code:
&lt;?php

// some code

?&gt;

Nonetheless, it's a nice contribution.
#4

[eluser]Grahack[/eluser]
Just a note: ?&gt; is not required, and could lead to sending unwanted whitespace, so it's recommended to just omit it.
#5

[eluser]khmer angkor[/eluser]
that good .for your comments.thanks




Theme © iAndrew 2016 - Forum software by © MyBB