Variable from controller not send to view in CodeIgniter - codeigniter

I am using HMVC with CodeIgniter.
I have this in my testmodule controller:
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
And this in my view template.php of that controller that is loaded by this controller:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>
but, when I var_dump($main_content) in the view and die() it shows null instead of frontpage
How, come? I don't get it at all.

If you want to use $this->view_data you have to declare $view_data as a property first (at the top of your controller):
class TestModule extends CI_Controller
{
public $view_data = array();
public function index()
{
// Now you can use $this->view_data in this function:
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}

Related

URL change issue after form post in Codeigniter

how can i manage url in address bar after posting a form or after loading a page after submission.
Is it possible to manage with routing ?
<?php
public function index(){
$this->load->view('login');
}
public function login_process(){
....... code......
if($login==true){
$this->load->view('dashboard'); // Url is not changing but view is loaded
}else{
$this->load->view('login');
}
}
?>
Hope this will help you :
Use redirect() method from url helper , make sure you load it in controller or in autoload.php
public function login_process()
{
....... code......
if($login === TRUE)
{
redirect('controller_name/dashboard','refresh');
/*$this->load->view('dashboard'); */
}
else
{
redirect('controller_name/index','refresh');
/*$this->load->view('login');*/
}
}
For more :https://www.codeigniter.com/user_guide/helpers/url_helper.html#redirect
You should create another method called dashboard and you should redirect to it like following
class Example_Controller extends CI_Controller {
public function index(){
$this->load->view('login');
}
public function dashboard(){
$this->load->view('dashboard');
}
public function login_process(){
// Your Code
redirect('Example_Controller/' . (($login==true) ? 'dashboard' : 'index'));
}
}
replace Example_Controller with your controller name.
and add following lines in routes.php
$route['login'] = 'Example_Controller/index';
$route['dashboard'] = 'Example_Controller/dashboard';

Understanding codeigniter base controller structure

The following is a working example of how my Codeigniter website currently functions:
Model:
<?php
class Default_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_link()
{
$query = $this->db->query('SELECT * FROM links LIMIT 5');
return $query->result();
}
Controller:
<?php
class Home extends CI_Controller {
public function index()
{
$this->load->model('segment1/Page_model');
$data['link'] = $this->Page_model->get_link();
$this->load->view('page_view', $data);
}
}
View:
<h2>Link</h2>
<ul>
<?php if (isset($link)):?>
<?php foreach ($link as $row):?>
<li><?=$row->link?></li>
<?php endforeach;?>
<?php endif;?>
</ul>
I want to begin using a base controller for the above example, and while I've followed a few online examples - I can't quite get it right, and I'd appreciate some guidance...
I autoload the Model, no problem
The View file remains
I alter the config.php file
Controller:
<?php
class Home extends Main_Controller {
public function index()
{
$this->load->model('segment1/Page_model');
$this->load->view('page_view', $data);
}
}
MY_Controller
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Now, here's where I get stuck - I can't quite figure out exactly what goes in the Main_Controller, and how it's structured...
Main_Controller:
<?php
class Main_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
//
// WHAT GOES IN HERE?
// SERIOUSLY, HELP!
//
}
}
Clearly, there's one big line of data missing from the original controller...
$data['link'] = $this->Page_model->get_link();
How does it all tie up?
Not exactly sure if I understand your question correctly, but if you want to avoid repeating this line:
$data['link'] = $this->Page_model->get_link();
What you can do is to put that in the constructor and create a public variable where you can store it.
i.e.
Main_Controller:
<?php
class Main_Controller extends MY_Controller
{
public $link;
function __construct()
{
parent::__construct();
$this->load->model('segment1/Page_model');
$this->link = $this->Page_model->get_link();
}
}
Controller:
<?php
class Home extends Main_Controller {
public function index()
{
$this->load->view('page_view', array('link' => $this->link));
}
public function another_page()
{
// you can keep using the value assigned to link in other
// methods without having to call Page_model->get_link() everytime
$this->load->view('page_view', array('link' => $this->link));
}
}

How to call custom controller from index controller in Zend Framework

I am newbie in Zend framework.And i have made sample project in netbeans.And it is working properly displaying index.phtml .But , I need to call my controller.What I have tried is below.
IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
firstExample::indexAction();
}
}
And I have deleted all the content of index.phtml(just a blank file) , coz i don't want to render this view.
My custom controller is:
firstExampleController.php
<?php
class firstExample extends Zend_Controller_Action{
public function indexAction(){
self::sum();
}
public function sum(){
$this->view->x=2;
$this->view->y=4;
$this->view->sum=x + y;
}
}
?>
firstExample.phtml
<?php
echo 'hi';
echo $this->view->sum();
?>
How to display sum method in firstExample.php.
It just shows blank page after hitting below URL.
http://localhost/zendWithNetbeans/public/
I think after hitting on above URL , execution first goes to index.php in public folder.And I didn't change the content of index.php
You are using controller (MVC) incorrectly, Controller should not do any business logic, in your case sum method. Controller only responsible controlling request and glueing model and view together. That's why you have problems now calling it.
Create Model add method sum, and use in any controller you want. From controller you may pass model to view.
Here is example: http://framework.zend.com/manual/en/learning.quickstart.create-model.html it uses database, but it's not necessary to use with database.
Basically your sum example could look like:
class Application_Sum_Model {
public function sum($x, $y) {
return ($x + $y);
}
}
class IndexContoler extends Zend_Controller_Action {
public function someAction() {
$model = new Application_Sum_Model();
//passing data to view that's okay
$this->view->y = $y;
$this->view->x = $x;
$this->view->sum = $model->sum($x, $y); //business logic on mode
}
}
Please read how controller is working, http://framework.zend.com/manual/en/zend.controller.quickstart.html

HMVC Module Error - Undefined property: model

So I'm getting the error: Undefined property: badge_progress::$bp_model.
I don't understand what's going on. Here is my code:
Controller:
<?php
// Badge Progress Module
class badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
?>
Model:
<?php
class bp_model extends CI_Model {
function dotpoints($badge_id) {
$query = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$badge_id);
if ($query->num_rows() > 0) {
return $query->result();
}
}
}
?>
Ah fixed it! Didn't realise that the main controllers (controllers outside of the module directory) also needed to be extending "MX_Controller" instead of "CI_Controller".
Class names must begin with an uppercase letter.
class Badge_progress extends...
class Bp_model extends...
http://codeigniter.com/user_guide/general/controllers.html
http://codeigniter.com/user_guide/general/models.html
update:
You shouldn't have the logic you need as a function in your constructor. Create a separate function to process the dotpoints stuff.
<?php
// Badge Progress Module
class Badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
}
function dotpoints()
{
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
Also, you are missing the constructor in your model. Check out those links I posted earlier...

Codeigniter: My controller is not able to access the model

//My controller section
<?php
class Myadmin extends CI_Controller
{
public function _construct()
{
parent::_construct();
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->model('adder','',TRUE);
}
public function index()
{
echo " i am about to call the model";
$this->adder->insert_user();
}
}
?>
**//My model section**
<?php
class Adder extends CI_Model {
function_construct() {
parent::_construct();
}
public function insert_user()
{
echo " Hi ,the model is accessed";
}
}
?>
Is it because of "function_construct()"?
It has no space and you should use two _
function _construct(){
parent::_construct();
}
Same in Controller
The problem is the way you load the model in your controller.
In the current version of the CodeIgniter you should do something like this:
//loading the model
$this->load->model('adder', 'fubar');
//accessing it's functions
$this->fubar->function();
for more info see this.
EDIT:
You have defined a _construct() function which must be __construct().
Also you should fix parent::_construct(); to parent::__construct().

Resources