CodeIgniter Forums
Problems with files in view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Problems with files in view (/showthread.php?tid=68651)



Problems with files in view - Marcolino92 - 08-08-2017

Hi guys, I ask you a question to which I found a somewhat spartan solution and I do not like it.

Then, in my Post.php file in controller, I have this code:

PHP Code:
class Post extends CI_Controller {
 
   
    public 
function __construct() {
 
       parent::__construct();
 
       $this->load->model('post_model');
 
       $this->load->helper('url_helper');
 
   }
 
   
    public 
function index() {
 
       $data['title'] = 'Title Page';
 
       $data['post'] = $this->post_model->get_post();
 
       $this->load->view('templates/header'$data);
 
       $this->load->view('index'$data);
 
   

Perfect, so I in the view folder, I have a file called index.php which is exactly the index of my site.

My question is the following, I would like to create a page called "hot.php" where the same thing as index.php is displayed but it shows the results according to the views.

I can not do this, using the Post.php file in controller, and the Post_model.php file in Models.

Would an idea be this?

PHP Code:
class Post extends CI_Controller {
 
   
    public 
function __construct() {
 
       parent::__construct();
 
       $this->load->model('post_model');
 
       $this->load->helper('url_helper');
 
   }
 
   
    public 
function index() {
 
       $data['title'] = 'Title Page';
 
       $data['post'] = $this->post_model->get_post();
 
       $this->load->view('templates/header'$data);
 
       $this->load->view('index'$data);
 
   }

    public function 
hot() {
      
$data['title'] = 'Title Page';
      
$data['post'] = $this->post_model->get_hot_post();
      
$this->load->view('templates/header'$data);
      
$this->load->view('hot'$data);
 
   



RE: Problems with files in view - InsiteFX - 08-08-2017

You can also load views using php right in your html files like so

PHP Code:
<?php $this->load->view('view_name'); ?>



RE: Problems with files in view - Marcolino92 - 08-08-2017

In my "view" folder I have the index.php. Well, I would like to create virtually the same model but with a file called hot.php

So that I can reach my site in these ways:

miosito.com
Miosito.com/hot.php (or miosito.com/hot)

Do I need to create another file in the controller? Can not I use the same by creating another public function?


RE: Problems with files in view - InsiteFX - 08-09-2017

Create a method in your controller called hot, create a route for it.

In the method load the hot view for it.


RE: Problems with files in view - Marcolino92 - 08-09-2017

So do I need to create a new file in the specific controller for this new page?

I can not use the same Post.php file like this:

PHP Code:
class Post extends CI_Controller {
    
    public function __construct
() {
        parent::__construct();
        $this->load->model('post_model');
        $this->load->helper('url_helper');
    }
    
    public function index
() {
        $data['title'] = 'Title Page';
        $data['post'] = $this->post_model->get_post();
        $this->load->view('templates/header', $data);
        $this->load->view('index', $data);
    }

    public function hot() {
      $data['title'] = 'Title Page';
      $data['post'] = $this->post_model->get_hot_post();
      $this->load->view('templates/header', $data);
      $this->load->view('hot', $data);
    }  



RE: Problems with files in view - Wouter60 - 08-09-2017

There is nothing wrong with your current controller Post.php.
If your view ' hot' is no different from the view 'index', you might just as well load the 'index' view via the hot method.
From the hot method you are passing a different set of data to the view, so the outcome is that only the selected 'hot' posts are shown.

The only thing you have to change is this line in the hot method:
PHP Code:
$this->load->view('hot',$data);
//change it into:
$this->load->view('index',$data); 



RE: Problems with files in view - Marcolino92 - 08-09-2017

So if I go for example on: miosito.com I will not have a conflict of two indexes?


RE: Problems with files in view - Wouter60 - 08-09-2017

I don't mean to be rude, but your question tells me that you haven't got a clue about the MVC concept which CodeIgniter is based on.
Please, read about it in the documentation: http://www.codeigniter.com/userguide3/overview/mvc.html
For further reading, start here: http://www.codeigniter.com/userguide3/general/urls.html
And keep reading until you've read the pages about Controllers, Models and Views. (For a start, that is).


RE: Problems with files in view - ciadvantage - 08-16-2017

(08-09-2017, 06:31 AM)Marcolino92 Wrote: So if I go for example on: miosito.com I will not have a conflict of two indexes?

 Based on your code, I think it looks ok.  It just matters of how you route it, by default if you type in
http://miosito.com/index.php/post/hot  then it show show your 'hot'  view

OR http://misito.xom/post/hot  (you can omit the index.php depends on how you set it up)

OR I can make it whatever i want to call in the router like 

$route['oh-my-hot']='post/hot';  

Then it shows up on http://misito.com/oh-my-hot

Just take sometime to read the doc,  you will find it easy

Regards