Welcome Guest, Not a member yet? Register   Sign In
Post your controller!
#14

[eluser]brianw1975[/eluser]
Here's a controller that I wrote up when I was first learning CI... adding it because it's an example of using _remap and it has pagination.

(please don't make fun of the smarty usage -- I do it this way because I can tell users if they want to make/change templates they can read the smarty handbook instead of learning PHP)

and yes!!! no documentation! lol... I'm the sole programmer(owner actually, so consider this code now under CC license, fwiw) and this controller has very specific usage.

Code:
<?
class Users extends Controller {

    function Users()
    {
        parent::Controller();
        $this->load->model("users_model",'users');
        $this->config->set_item('per_page',36);
    }
    
    function _remap($name = '')
    {
        switch($name){
            case "index":
                global $APPCFG;
        
                $this->mysmarty->assign_by_ref("CFG",$APPCFG);
                $this->mysmarty->assign("base_url",$this->config->item('base_url'));
                $this->mysmarty->assign("page_name","Users");
                $this->mysmarty->assign("page_keywords","");
                
                $users = $this->users->getAll();
                
                $config['base_url'] = $this->config->item('base_url').'/users/page/';
                $config['total_rows'] = $users->num_rows();
                
                $this->pagination->initialize($config);
                $this->mysmarty->assign("links",$this->pagination->create_links());
                
                $this->mysmarty->assign_by_ref("users",$this->users->getAll(true,$this->config->item('per_page')));
                $content = $this->mysmarty->fetch("users_table.php");
                
                $this->mysmarty->assign("content",$content);
                $this->mysmarty->display("home.php");
            break;
            case "page":
                global $APPCFG;
                $config['per_page'] =36;
                
                $this->mysmarty->assign_by_ref("CFG",$APPCFG);
                $this->mysmarty->assign("base_url",$this->config->item('base_url'));
                $this->mysmarty->assign("page_name","Users");
                $this->mysmarty->assign("page_keywords","");
                $original_query = $this->users->getAll();
                
                $users = $this->users->getPage($this->uri->segment(3, 0),$this->config->item('per_page'));
                
                $config['base_url'] = $this->config->item('base_url').'/users/page/';
                $config['total_rows'] = $original_query->num_rows();
                
                $this->pagination->initialize($config);
                
                $this->mysmarty->assign("links",$this->pagination->create_links());
                
                $this->mysmarty->assign_by_ref("users",$users->result_array());
                $content = $this->mysmarty->fetch("users_table.php");
                
                $this->mysmarty->assign("cols",4);
                $this->mysmarty->assign("content",$content);
                $this->mysmarty->display("home.php");
            break;
            default:
                $this->_showUsers($name);
            break;
        }
    }
    function _showUsers($name)
    {
        global $APPCFG;
        
        
        $this->mysmarty->assign("page_name","User: $name");
        $this->mysmarty->assign("page_keywords","$name, stories, websites");
        
        $users= $this->users->getOne(0,$name);
        
        $this->mysmarty->assign("content",$users);
        $this->mysmarty->assign("inc_file","blocks/archive_bio.php");
        $this->mysmarty->assign("title", "User Details");
        
        $this->mysmarty->assign("content",
            $this->mysmarty->fetch("blocks/common/content.php"));
            
        $this->mysmarty->display("home.php");
    }
}
?>

I know that I used _remap for pretty link and SEO niceties, but looking back on it, I'm not exactly sure _why_ i chose to do it that way.... but like i said, it's a sample of using remap and pagination.

FYI
/site.com/users/ gives a list of users
/site.com/users/page/2 shows page two of the users
/site/com/users/(user_name) shows the information for that particular username

Ok, lemme have it.


Messages In This Thread
Post your controller! - by El Forum - 08-30-2009, 12:19 AM
Post your controller! - by El Forum - 08-30-2009, 01:36 AM
Post your controller! - by El Forum - 08-30-2009, 07:19 AM
Post your controller! - by El Forum - 08-30-2009, 12:31 PM
Post your controller! - by El Forum - 08-30-2009, 12:44 PM
Post your controller! - by El Forum - 08-30-2009, 08:59 PM
Post your controller! - by El Forum - 08-30-2009, 10:07 PM
Post your controller! - by El Forum - 08-31-2009, 02:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:19 AM
Post your controller! - by El Forum - 08-31-2009, 04:43 AM
Post your controller! - by El Forum - 08-31-2009, 04:49 AM
Post your controller! - by El Forum - 09-01-2009, 01:05 AM
Post your controller! - by El Forum - 09-01-2009, 09:30 PM
Post your controller! - by El Forum - 09-02-2009, 02:42 AM
Post your controller! - by El Forum - 09-02-2009, 05:34 AM
Post your controller! - by El Forum - 09-02-2009, 09:02 AM
Post your controller! - by El Forum - 09-02-2009, 09:43 AM
Post your controller! - by El Forum - 09-02-2009, 10:13 AM
Post your controller! - by El Forum - 09-02-2009, 01:13 PM
Post your controller! - by El Forum - 09-03-2009, 06:40 AM
Post your controller! - by El Forum - 11-03-2009, 10:20 PM
Post your controller! - by El Forum - 11-03-2009, 11:02 PM
Post your controller! - by El Forum - 11-03-2009, 11:33 PM
Post your controller! - by El Forum - 11-04-2009, 12:09 AM
Post your controller! - by El Forum - 11-04-2009, 03:36 AM
Post your controller! - by El Forum - 11-04-2009, 07:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB