Codeigniter - cannot load any view at all - codeigniter

I have built a website with the CodeIgniter framework.
Unfortunately, I am having a problem with CodeIgniter's load->view() function.
Every time I try to load another view file with the code $this->load->view('any_view_file'); I get the error:
unable to load the requested file 'any_view_file'.php
For example i am having problems with the $this->load->view('available_lessons.php', $data); command. So let's try to recreate the problem and think this through...
I am calling the load_available_lessons() function of the Lessons.php controller
The load_available_lessons() function uses the get_lessons() function of the db_model.php model and saves the result on the $data['result'].
Then, the load_available_lessons() function calls the available_lessons view file while parsing the $data to it.
Here is where the problem rises. CodeIgniter is unable to load the view. As i explained earlier, CodeIgniter fails to load any view...!!!
Any suggestions please?
My .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\\.php|images|css|lessons|robotos\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
From my config.php file:
$config['index_page'] = '';
From my routes.php file:
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['translate_uri_dashes'] = TRUE;
My Pages.php Controller:
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
}
?>
My Lessons.php Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Lessons extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('db_model');
/* $this->load->library('acl'); */
/* $this->load->helper(array('form', 'url')); */
}
public function index()
{
$this->load->view('available_lessons', array('error' => ' ' ));
}
public function load_available_lessons()
{
$data['result'] = $this->db_model->get_lessons();
$this->load->view('available_lessons.php', $data); /* available_lessons */
}
}
?>
My db_model.php Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class db_model extends CI_Model{
public function __construct()
{
parent::__construct();
}
public function new_lesson($data)
{
/* Inserting in Table "lessons" of CodeIgniter's Database */
$this->db->insert('available_lessons', $data);
}
public function get_lessons() /* $data */
{
/* Get lessons from Table "lessons" of CodeIgniter's Database */
$this->db->select('Name, Description, DateAdded, LastEdit, UserID, FileName');
$this->db->from('lessons');
$query = $this->db->get();
return $result = $query->result();
}
}
?>
My available_lessons.php View file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="container">
<h6 class="description">Browse Lessons</h6>
<div class="row">
<section id="lessons-panel" class="col-xs-12 col-md-8 col-md-offset-2">
<table>
<tr>
<th>Content</th>
</tr>
<?php foreach($this->result as $r): ?>
<tr><?php echo $r->content; ?>
</tr>
<?php endforeach; ?>
</table>
</section>
</div>
</div>

As #ourmandave and a coworker pointed out to me, i should add the "pages/" before every view name that i want to call. For example, in the Lessons.php controller, i should have $this->load->view('pages/available_lessons.php', $data); As it turned out he is right.
The thing is that i cannot understand the reason why i should include the "pages/" as I have already included this in the Pages.php controller. It seems that the Pages Controller is not being used when i call a view. I will have to look into it later on.
Thank you all for your valuable input!!!!! Just another rookie mistake...

Related

Writing to Database when executing controller's index method - error

I call the index() method of the collaborating controller, it automatically inserts 6 records into the database, and the action of logging into the database is not in the cadastrarquestao() method of the class, and not in the index method . I'm using a template library.
Controller Colaborador:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Colaborador extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->helper('cookie');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados['teste'] = 1;
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function logout() {
session_unset();
redirect ('/entrar');
}
public function cadastrarquestao() {
$this->simulado->envioquestao(1);
$dados['mensagem'] = "dados cadastrados com sucesso!";
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function materia() {
}
}
part of the code
public function envioquestao($dados) {
$data = [
'Id_usuario' => $this->session->userdata('id_usuario'),
];
$this->db->insert('teste',$data);
}
librarie Template:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
var $template_data = array();
function set($name, $value)
{
$this->template_data[$name] = $value;
}
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{
$this->CI =& get_instance();
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data, $return);
}
}
View template:
<html lang="pt-br">
<?php
$this->load->view('app/header');
?>
<body>
<div class="wrapper">
<?php
$this->load->view('app/topo');
?>
<?php
$this->load->view('app/menu');
?>
<?php echo $contents ?>
<footer class="footer-container"><span>© </span></footer>
</div>
<?php
$this->load->view('app/footer');
?>
</body>
</html>
the correct one would be to insert it in the database after clicking on 'register' which is in the cadastrarquestao() method

Message: Undefine variable: exam

controller: Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url'));
$this->load->model('Fetch_data');
}
public function agriculture_exam()
{
$stream = 'agriculture';
$data['exam'] = $this->Fetch_data->top_agriculture_exams($stream);
$this->load->view('header',$data);
}
}
view: header.php
<ul class="list">
<?php
foreach($exam as $row)
{
echo "<li><a href='#'>".$row['exam_name']."</a></li>";
}
?>
</ul>
model: Fetch_data.php
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function top_agriculture_exams($stream)
{
$this->db->select('exam_name');
$this->db->from('all_exams_details');
$this->db->where('field',$stream);
$this->db->order_by('exam_name');
$this->db->limit('10');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
I am new in ci. In controller i.e. Test.php I am defining $stream='agriculture' and pass $stream variable to top_agriculture_exams model. Now, when I am fetching data on header.php file it show me a Message: Undefine variable: $exam I don't know why. So, how can I fix it ?please help me.
Thank You
Please check my below code. This will help you.
<ul class="list">
<?php
if ($exam !== NULL):
foreach ($exam as $row):
echo "<li><a href='#'>" . $row['exam_name'] . "</a></li>";
endforeach;
endif;
?>
</ul>
Also cna you please put this code in View and share result.
<?php
var_dump($exam);
?>
Also check your model code below.
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function top_agriculture_exams($stream)
{
$this->db->select('exam_name');
$this->db->from('all_exams_details');
$this->db->where('exam_name',$stream);
$this->db->order_by('exam_name');
$this->db->limit('10');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
instead of accessing it using the element array try accessing it using object.
change below code in your Fetch_data.php file.
//$result = $query->result_array();
$result = $query->result();
and access it by object in your header.php. here's an example below.
<ul class="list">
<?php foreach($exam as $row): ?>
<?= "<li><a href='#'>" . $row->exam_name . "</a></li>" ?>
<?php endforeach;
</ul>
where exam_name is equal to the column in your database.

Error displayed when trying to insert data from db into dropdown list within CodeIgniter

The following error is displayed:.............................................
Severity: Notice
Message: Undefined variable: groups
Filename: views/content_view.php
Line Number: 10
please can you assist me...below is the code that i have tried
below is my controller
**MY CONTROLLER**
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('model_get');
}
public function delivery()
{
$data['groups'] = $this->model_get->getAllGroups();
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view('login');
$this->load->view('content_view');
$this->load->view("content_video");
$this->load->view("site_footer");
}
}
?>
below is my view
**MY VIEW**
<div id="content" class="col-md-6">
<h1> Comparison</h1>
<select class="form-control">
<?php
if (is_array($groups))
{
foreach($groups as $row)
{
echo '<option value="'.$row->page.'">'.$row->page.'</option>';
}
}
?>
</select>
</div>
below is the relevant information from the model
MY MODEL
class Model_get extends CI_Model{
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT page FROM pageData');
return $query->result();
}
}
Try:
$this->load->view('content_view', $data);

Call to undefined function result()

I am getting this error in my view file. Here is my code please do help me out and tell what to do?
<?php
//foreach($records->result() as $row):
foreach(result() as $row):
echo $row->title;
endforeach;
?>
Here is my controller file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller
{
public function index()
{
$this->load->model('hello_model');
$data['records']=$this->hello_model->getAll();
$this->load->view('you_view',$data);
//$this->load->view('you_view');
}
}
?>
I am posting here my model file also. Ihv tried some by me, but still getiing this error. Dnt knw wt to do.
<?php
class Hello_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll()
{
$q=$this->db->get('test'); // query where 'test' is table name.
if($q->num_rows()>0)
{
foreach ($q->result() as $row)
{
$data[]=$row;
}
return $data;
}
}
}
?>
$records already holds your data so this should work:
foreach($records as $row){
echo $row->title;
}
You do not need to use result() in your view,because in your model you already did.
Just try this:
<?php
foreach($records as $row):
echo $row->title;
endforeach;
?>

why is this error showing

I am using CI for my web-site. While programming CMS for the same I faced no problem but when I am programming the same for user-end I am getting error as:
"404 Page Not Found
The page you requested was not found."
What am i doing wrong?? Any help/suggestions is warmly welcome.Thank you.
In Controller(model.phpl):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user_model');
$this->load->model('common');
$this->load->model('home_model');
$this->load->model('page_model');
}
function _remap($method , $params = array())
{
$this->menu = $this->common->createMenuArr();
$this->index();
}
function index()
{
$data['sliderArr'] = $this->user_model->sliders();
$this->load->view('index', $data);
}
}
In Model(user_model.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model{
function __construct()
{
parent::__construct();
//$this->load->database();
}
function sliders()
{
$query = $this->db->query("SELECT slider FROM tbl_sliders ORDER BY slider_id DESC")->result_array();
return $query;
}
}
and finally in view(index.php):
<div id="slideContainer">
<div id="slideShim">
<?php
if(!empty($sliderArr))
{
foreach($sliderArr as $slider)
{ ?>
<img src="<?php echo base_url();?>images/sliders/<?php echo $slider['slider'];?>"
<?php }
}
?>
</div>
</div>
Go to Application -> Config -> Routes.
And Set $route['default_controller'] = "index"; //whatever your controller name is on which you want your application to route by default
Hope this helps.

Resources