Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. public function index($id = null, $slug = ''){
  2. // Fetch the article
  3. //$this->db->where('pubdate <=', date('Y-m-d'));
  4. $this->data['article'] = $this->mberita->get_by_id($id,$slug);
  5. // Return 404 if not found
  6. count($this->data['article']) || show_404(uri_string());
  7.  
  8. // Redirect if slug was incorrect
  9. $requested_slug = $this->uri->segment(3);
  10. $set_slug = $this->data['article']->slug;
  11. if ($requested_slug != $set_slug) {
  12. redirect('article/' . $this->data['article']->id . '/' . url_title($set_slug), 'location', '301');
  13.  
  14.  
  15. // Load view
  16. $this->data['contents'] = 'article';
  17. $this->load->view('template/wrapper/mahasiswa/wrapper_article', $this->data);
  18.  
  19. }
  20.  
  21. <div class="post-buttons">
  22. <a href="<?php echo 'article/' . intval($dt->id) . '/' . url_title($dt->slug) ; ?>" class="ui black label">
  23. Read More
  24. </a>
  25. </div>
  26.  
  27. tkd/index.php/article/77/kejurnas-mahasiswa
  28.  
  29. RewriteCond %{REQUEST_FILENAME} !-f
  30. RewriteCond %{REQUEST_FILENAME} !-d
  31. RewriteRule ^(.*)$ index.php?/$1 [L]
  32.  
  33. $route['article/(:num)/(:any)'] = "article/show_article/$1/$2"; //where article is controller and show_article is "behind the scene" method that takes over your index(); not a single user will know about it
  34.  
  35. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  36.  
  37. class Article extends CI_Controller {
  38. //I can spot that you are extending your controller NOT by CI_Controller because of usage $this->data[]
  39.  
  40. public function __construct() {
  41. //constructor
  42. parent::__construct();
  43. $this->load->model('article_model', 'article'); //load model article_model and create alias "article"
  44.  
  45. }
  46.  
  47. public function index() {
  48. //let this be empty or redirect if no ID is sent to the controller
  49. redirect(); //redirect to default_controller
  50. }
  51.  
  52. public function show_article($id = FALSE, $slug = FALSE) {
  53. if ($id === FALSE) redirect(); // no id? redirect to default_controller
  54. if ($slug === FALSE) {
  55. //slug is not sent to the article
  56. //you should do nothing if this happens but you may require reload page with correct slug
  57. //get slug by id
  58. //redirect(/article/$id/slug_by_id)
  59. }
  60.  
  61. // Fetch the article
  62. //$this->db->where('pubdate <=', date('Y-m-d'));
  63. if ($this->data['article'] = $this->article->get($id)) {
  64. //all good we can show result
  65. $this->data['contents'] = 'article';
  66. $this->load->view('template/wrapper/mahasiswa/wrapper_article', $this->data);
  67.  
  68. } else {
  69. //get() method returns FALSE
  70. redirect(); //id is not valid or there is no such record with id = $id
  71. }
  72.  
  73. }
  74.  
  75. }
  76.  
  77. /* End of file article.php */
  78. /* Location: ./application/controllers/article.php */
  79.  
  80. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  81.  
  82. class Article extends CI_Model {
  83.  
  84. public function __construct() {
  85. parent::__construct();
  86. //Do your magic here
  87. }
  88.  
  89. public function get( $id = FALSE ) {
  90. if ($id === FALSE) return FALSE;
  91.  
  92. $q = $this->db->get_where('article', array('id' => $id)); // table = 'article'
  93.  
  94. return ($q->num_rows() > 0) ? $q->result() : FALSE;
  95.  
  96. }
  97.  
  98.  
  99. }
  100.  
  101. /* End of file article_model.php */
  102. /* Location: ./application/models/article_model.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement