How Do I add PHP code to my Codeigniter view file?
I would like to add some 开发者_运维知识库PHP to my Codeigniter view, for, say, a dynamic date in the footer. What is the very best way to manage this?
There's nothing wrong with having PHP in your views. I use PHP in my views all the time for things like looping through arrays and creating ordered lists, etc. IMO, MVC is not about separating HTML from PHP, it's about separating business logic and display logic.
There are many different interpretations and implementations of MVC, so some people will disagree with me, and that's fine. Decide how you want to use MVC and be consistent throughout your project.
If you are going to be passing data to the view, then populate the $data array in the controller:
$data = array(
'date' => $myDate
);
$this->load->view('myview', $data);
Then in your view just add some PHP to write it out. For example:
<?php echo($date); ?>
For your view, you can either pass data to it via a controller or even you can put the php code directly in your view file. There is no restriction on that. However, to keep things consistent it is always good idea to put as much php code in controllers as possible and view should contain mostly the pure html/css/js code but as i said there is nothing wrong even if you put php code in your view files.
精彩评论