I want to load a view which come from logical statement (in controller) IN a view. Simply like this
Controller
public function index(){
$this->lib();
$this->view('main');
}
public function lib(){
if(TRUE){
// $this->view('something1') in $this->view('main')
}
else{
// $this->view('something2') in $this->view('main')
}
}
** View **
<html>
<body>
<!-- view from $this->view('main') -->
<!-- want to show view from lib() -->
</body>
</html>
How to show $this->view('something') get from lib() in $this->view('main') ? Any idea ?
The only way that I've found works is this:
Controller:
public function index() {
$data['internal_view'] = $this->load->view('stmt', [], true);
$this->load->view('main', $data);
}
"main" View:
<html>
<body>
<?php echo $internal_view; ?>
</body>
</html>
Note: the 3rd param in view allows for the view to be returned as a string rather than automatically outputted to the browser. Because of this functionality, you can assign a view as a return string from another function and use it to generate internal_view or whatever you decide to call it.
You could preserve the main view and also keep the lib conditional view and pass it to the main controller.
Controller :
public function index(){
$data['lib_view'] = $this->lib();
$this->view('main', $data);
}
public function lib(){
if(TRUE){
return $this->load->view('something1', true);
}
else{
return $this->load->view('something2', true);
}
}
'main' View :
<html>
<body>
<!-- view from $this->view('main') -->
<?php echo $lib_view ?>
</body>
</html>
Related
Sorry if this question is already been answered, but i couldn't find an answer.
I want to pass a variable from my model to the controller and finally display it in the view.
I execute a query on the database and after that I count my results (via num_rows). Then I return the result.
public function countHighRisk(){
$this->db->select("ares_status");
$this->db->from("tbl_alert_result");
$this->db->join('tbl_status_standards', 'ares_status = sts_status_id');
$this->db->where('sts_status_risk', 3);
$query = $this->db->get();
return $query->num_rows();
}
After that i place the result in an array and pass the array to my view.
public function index()
{
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
$this->load->view('templates/head');
$this->load->view('templates/menu');
$this->load->view('pages/Dashboard', $data);
$this->load->view('templates/footer');
}
I get an error when I try to display the variable in the view.
<div class="col-xs-8 text-right">
<span> High Risk </span>
<h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
error: http://imgur.com/iHkYHiI
Thanks in advance.
Your whole idea of calling the views is wrong here. Please use it as follows:
Controller:
public function index()
{
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
$this->load->view('pages/Dashboard', $data);
}
View:
<? $this->load->view('templates/head'); ?>
<? $this->load->view('templates/menu'); ?>
<div class="col-xs-8 text-right">
<span> High Risk </span>
<h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
<? $this->load->view('templates/footer'); ?>
Also check what $this->Alert_result_model->countHighRisk(); is actually returning by doing print_r($this->Alert_result_model->countHighRisk();)
You can load a view into controller but do this way below
class Dashboard extends CI_Controller {
public function index() {
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
// Choose only one header examples:
$data['header'] = $this->load->view('header', Null, TRUE); // If No Data
$data['header'] = $this->load->view('header', $data, TRUE); // If Data
// Choose only one Footer examples:
$data['footer'] = $this->load->view('footer', Null, TRUE); // If No Data
$data['footer'] = $this->load->view('footer', $data, TRUE); // If Data
$this->load->view('dashboard', $data);
}
}
Dashboard View
<?php echo $header;?>
<?php foreach ($high as $low) { ?>
<?php echo $low['something'];?>
<?php }?>
<?php echo $footer;?>
If you have a header separate header controller and footer controller you will need HMVC if you are confused on user guide top right corner of default user guide in CI3 is a switch to more cleaner version.
please change
$this->load->view('pages/Dashboard', $data);
into
$this->load->view('pages/Dashboard', $data, true);
now it should work.
I keep receiving this error message when I try to open a specific blog in a post I'm making. Its been about 3 days and I've gone over the code countless times. Im using CodeIgnigter and I've followed everything very carefully, the blog connects to the database and displays fine on the php_index page, but crashes when I try to open a specific post or edit, and also comes up with the error "You don't have permission to access /blog/CodeIgniter_2.2.0/< on this server." if i try to use my delete function. My code is shown below.
In my controller folder
<?php
class Posts extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('post');
}
function index(){
$data['posts']=$this->post->get_posts();
$this->load->view('post_index',$data);
}
function post($postID){
$data['post']=$this->post->get_post($postID);
$this->load->view('post',$data);
}
function new_post(){
if($_POST){
$data=array(
'title'=>$_POST['title'],
'post'=>$_POST['post'],
'active'=>1
);
$this->post->insert_post($data);
redirect(base_url().'posts/');
} else {
$this->load->view('new_post');
}
}
function editpost($postID){
$data['success']=0;
if($_POST){
$data_post=array(
'title'=>$_POST['title'],
'post'=>$_POST['post'],
'active'=>1
);
$this->post->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->post->get_post($postID);
$this->load->view('edit_post',$data);
}
function deletepost(){
$this->post->delete_post($postID);
redirect(base_url());
}}?>
and then my views folder
For singular post:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php if(!isset($post)){ ?>
<p>This Page Was Accessed Incorrectly</p>
<? } else { ?>
<h2><?=$post['title']?></h2>
<?=$post['post']?>
<? } ?>
</body>
</html>
My Model folder
<?php
class Post extends CI_Model{
function get_posts($num=20, $start=0){
$this->db->select()->from('posts')->where('active', 1)->order_by('date_added','desc')->limit($num, $start);
$query=$this->db->get();
return $query->result_array();
}
function get_post($postID){
$this->db->select()->from('posts')->where(array('active'=>1, 'postID'=>$postID))->order_by('date_added','desc');
$query=$this->db->get();
return $query->first_row('array');
}
function insert_post($data){
$this->db->insert('posts',$data);
return $this->db->insert_id();
}
function update_post($postID,$data){
$this->db->where('postID',$postID);
$this->db->update('posts',$data);
}
function delete_post($postID,$data){
$this->db->where('postID',$postID);
$this->db->delete('posts');
}
}
?>
Do you have a .htaccess file in place? If not, try to put this one in your document root:
https://gist.github.com/philipptempel/4226750
Make sure to set the rewrite base in .htaccess to your CodeIgniter install folder, I think it's /blog/CodeIgniter_2.2.0/ in your case. The .htaccess should be in the same folder as your index.php.
If you also want to remove the index.php part from your url have a look here:
CodeIgniter removing index.php from url
I am a little confused using fuelPHP 1.7.
The controller
class Controller_Website extends Controller
{
public function action_index()
{
// http://fuelphp.com/docs/general/views.html
$data = Website::get_results();
//var_dump($data) // (data is found here);
$views = array();
$views['head'] = View::forge('common/head', $data);
$views['header'] = View::forge('common/header', $data);
$views['sidebar'] = View::forge('common/sidebar', $data);
$views['content'] = View::forge('common/content', $data);
$views['footer'] = View::forge('common/footer', $data);
// return the rendered HTML to the Request
return View::forge('website', $views)->render();
}
}
The model
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->execute();
return $result;
}
}
All well sofar. Data is queried and found in the controller. What I am trying to accomplish is to use the data in my:
(nested) view
<html>
<head>
<?php echo $head; ?>
</head>
<body>
<header>
<div class="container">
<?php echo $header; ?>
</div>
</header>
<div class="row">
<div class="container">
<div class="col-md-4">
<?php echo $sidebar; ?>
</div>
<div class="col-md-8">
<?php echo $content; ?>
</div>
</div>
</div>
<footer>
<div class="container">
<?php echo $footer; ?>
</div>
</footer>
</body>
</html>
Head view (nested):
<title><?php echo $title; ?></title>
Content view (nested):
<h1><?php echo $title; ?></h1>
<div class="welcome_user"><?php echo $text; ?></div>
And so on.
The variables in the view in this example are not available because they are not explicitly set in the controller. Do they have to be set explicitly or is passing the data object also possible? If so, how do I access this objects data in the right way? FuelPHP is lacking good examples here and I am stuck now.
How do I do it?
The view data is converted from array indexed to view variable named. So:
View::forge('something', array('param' => 'value'));
Will correspond to the following view:
<h1><?=$param?></h1>
Where things are going wrong is is that you pass the plain DB result to the view. You'd need to get the first result from the database result, like this:
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->as_assoc()
->execute()
->to_array();
return reset($result);
}
}
Note that I've first used ->to_array() to convert the result object to an array, then reset() to get the first result. I've also added ->as_assoc() to make sure you get an array result, ->as_object() would give you a stdClass instance.
I don't want to add child to the viewModel in action controller:
// action controller
public function indexAction() {
$result = new ViewModel();
$result->setTemplate('application/view/another-action');
$comments = new ViewModel();
$comments->setTemplate('application/view/child-comments');
$result->addChild($comments, 'child_comments');
return $result;
}
...
// View
<div>
<?php echo $this->child_comments ?>
</div>
I want to include view in another view:
<div>
<?php
$view = new ViewModel();
$view->setVariables($this->var);
$view->setTemplate('page_nav.phtml');
// here I want to render view
?>
</div>
Is it possible?
This is what the Partial view helper does (docs seem to be outdated, but I'll link them anyway here):
<div>
<?php echo $this->partial('page_nav', array('var' => $this->var)) ?>
<div>
Obviously, your page_nav should be known by your view resolver.
I know this is old, but you can also simply call $this->render('path/to/view-script')
I have a Yii application and in the layout I have a static sidebar. I would like to change the content of the sidebar depending on the controller action, or the view. At the moment, in the views/layouts/main.php file, I have a sidebar defined as:
<div class="sidebar">hello!</div>
When the controller is image and the action is test, I would like to display this:
<div class="sidebar">Created at <? echo $image->created_at; ?></div>
The $image object is defined in the image controller and the test action.
In short, I would like to display a sidebar when the controller is image and the action is test, however, I would not like to have a different layout entirely. How can I accomplish this?
This is work for clip. In your layout:
<div class="sidebar">
<?php if(!empty($this->clips['sidebar'])) {
echo $this->clips['sidebar'];
} else { ?>
hello
<?php } ?>
</div>
Somewhere in your view:
<?php $this->beginClip('sidebar'); ?>
Created at <? echo $image->created_at; ?>
<?php $this->endClip(); ?>
You can put a simple if-else check, using getController(), getAction() like this :
if(Yii::app()->getController()->getId()=='image' && Yii::app()->getController()->getAction()->getId()=='test'){
echo '<div class="sidebar">Created at '.$this->image->created_at.'</div>';
}
else
echo '<div class="sidebar">hello!</div>';
To access $image in a layout we need to make it a public property of the image controller itself, then in the action test where we initialize the $image, we will initialize the public $image, example:
class ImageController extends Controller{
public $image;
...
public function actionTest(){
$this->image=Example::model()->findByPk(1);// example
...
}
}
This will allow us to access image in the layout albeit using $this->image->property.
Also in the layout $this refers to the current controller, hence we can also use $this->id instead of Yii::app()->getController()->getId() to get the controller id(name which is image here) and $this->action instead of Yii::app()->getController()->getAction()->getId() to get the action id.