Welcome Guest, Not a member yet? Register   Sign In
Problems with files in view
#1

(This post was last modified: 08-08-2017, 06:33 AM by Marcolino92.)

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);
 
   
Reply
#2

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

PHP Code:
<?php $this->load->view('view_name'); ?>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 08-08-2017, 10:47 PM by Marcolino92.)

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?
Reply
#4

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

In the method load the hot view for it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

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);
    }  
Reply
#6

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); 
Reply
#7

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

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).
Reply
#9

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB