I have created two views in CodeIgniter and I have created controller named HelloWorld.php
It contains two views.. but my problem is that the second view never gets called.
http://localhost/CodeIgniter/HelloWorld/Hello
Works fine for me, but second view
http://localhost/CodeIgniter/HelloWorld/Buzz
Doesn't call the second view
Here is my code
<?php
class HelloWorld extends CI_Controller
{
var $name;
var $color;
function __construct()
{
parent:: __construct();
$this->name= 'Suzzu';
$this->color = 'aqua';
}
public function Hello()
{
$this->load->view("hello");
}
public function Buzz()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->view("welcome",$data);
}
}
what's the problem ??
Your code works as expected for me provided that:
there is a view file "views/welcome.php"
there is a view file "views/hello.php"
Can you verify that HelloWorld::Buzz() is being called?
public function Buzz()
{
die('yes, it works');
}
If this function doesn't execute when you go to /localhost/CodeIgniter/HelloWorld/Buzz, can you provide the following:
$config['base_url'] (config/config.php)
$config['index_page']
config/routes.php contents
change $config['base_url']="localhost/codeigniter/contollername"
Related
I have these sample codes:
Controller
public function one()
{
$this->two();
// url equals localhost/project/index.php?/one
}
public function two()
{
// some code..
// url still equals localhost/project/index.php?/one
}
How will I change the URL to localhost/project/index.php?/two whenever I call two() under one()?
Hope this example will help you out to understanding the routes concept:
Controller
class Abc extends CI_Model {
public function one()
{
}
public function two()
{
}
}
config => Routes
$route['new-first-url'] = 'abc/one';
$route['new-second-url'] = 'abc/two';
in browser url -
for method one => https://localhost:80/project_name/new-first-url
for method two => https://localhost:80/project_name/new-second-url
contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.
Here is my controller code
public function index()
{
$this->load->model("mod_home");
$data['avoinics'] = $this->mod_home->getAvoinics();
$data['dir']="home";
$data['page']="index";
$this->load->view('main',$data);
}
for another page
public function about()
{
$this->load->model("mod_home");
$data['avoinics'] = $this->mod_home->getAvoinics();
$data['dir']="home";
$data['page']="about";
$this->load->view('main',$data);
}
But i don't want to send $data['avoinics'] again again. Is there any way to access a data from anypage.
How to use same data in a single view more than time.
foreach($avoinics as $avoinics):
$name=$avoinics->sc_name;
echo '<li>'.$name.'</li>';
endforeach;
if i use it again on same view page it's sowing error...
Yes, you can:
Create a global array
private $data = array();
In constructor
$this->load->model("mod_home");
$this->data['avoinics'] = $this->mod_home->getAvoinics();
Now your function will look like this
public function index() {
$this->data['dir']="home";
$this->data['page']="index";
$this->load->view('main',$this->data);
}
For second part, do not change the variable value
foreach($avoinics as $record){
echo '<li>'.$record['name'].'</li>';
}
$avoinics is intact now. You can use it again until you do not modify it.
A good example you might easily understand is when you need to call certain scripts or css files for a specific controller. You won't call it in every single page but yes in the constructor.
class yourController extends CI_Controller
{
private $data;
public function __construct()
{
$this->data['css'] = array('file1.css', 'file2.css');
$this->data['js'] = array('jquery.min.js', 'jquery-ui.min.js');
}
public function index()
{
$this->load->view('yourView', $this->data);
}
public function about()
{
$this->load->view('yourView', $this->data);
}
}
I recommend you to extend core ci_controller with my_controller and declare there your variable in constructor. Then in your new controllers extend your my_controller where you will have variable declaration.
I know this is MVC structure incompatible but i need to use this technique:
I have a controller and a model.
I'm calling a function in model from controller.
Model called function calles controllers another function. (This is what generates error).
Example below:
Controller:
public function B($ret=false) {
if(!$ret)$this->Model_model->M($ret);
else echo 'ok';
}
Model:
public function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this->Controller->B(!$ret); //i can't find how can i call this
}
My first trigger function is:
$this->Controller->B(false);
I've moved code in function B of controller to the Model completely and now everything is going in model itself. B function calling another B function in model and at last step model isn't need to call controller; it is calling B function in Model:
Controller:
public function B($ret=false) {
$this->Model_model->B($ret);
}
Model:
public function B($ret){
if(!$ret)$this->M($ret);
else echo 'ok';
}
private function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this-B(!$ret);
}
My first trigger function is:
$this->B(false);
And my controller is still has short code.
Using CodeIgniter 2.0.3 on XAMPP 1.7.7 I have code where I get the error as: Unable to load the requested file: home.php
The home.php code as follows stored in ./ci2/application/controllers:
class Home extends CI_Controller {
function Home()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
While using it just notice:
controller's name is home.php
File named home.php must be present in ci/views
Incase file extention in view folder is not php eg like its html.
then load it using : $this->load->view('home.html');
the function u need to call from outside must be public So make it:
public function index() {... }
as u are calling the constructor for the class Home. Call it like:
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
Now it will work !
Your call to $this->load->view('home'); will be looking for home.php in /ci2/application/views/. That's the file it can't find.
I'm assuming you're calling http://myapp/index.php/home/ - that means it'll automatically call the index() method.
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
}
Please check the name of the file you have created in application/views/, because otherwise the code is correct.