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

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

Related

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.

Hi, I am new to Codeigniter. A PHP Error was encountered

A PHP Error was encountered
Severity: Notice
Message: Undefined property: News::$News_model
Filename: controllers/News.php
Line Number: 14
Backtrace:
File: /Applications/MAMP/htdocs/workshop10/application/controllers/News.php
Line: 14
Function: _error_handler
File: /Applications/MAMP/htdocs/workshop10/index.php
Line: 315
Function: require_once
my controller:
defined('BASEPATH') OR exit('No direct script access allowed');
class News extends CI_Controller {
public function index()
{
$data['theNews'] = array("All your base are belong to us!",
"Autotune this news",
"Numa Numa!!",
$this->News_model->get_new_news()
);
$this->load->view('templates/news_header');
$this->load->view('new_items',$data);
$this->load->view('templates/news_footer');
}
}
my model:
class News_model extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
public function __construct()
{
//call the Model constuctor
parent::__construct();
}
public function get_new_news()
{
return "Something Kool";
}
}
my view:
<?php
foreach ($theNews as $news_item) { ?>
<div><?php echo $news_item; ?></div>
<?php } ?>
In your controller.Use the following code.
public function index()
{
$this->load->model('News_model');//loads your model
$return = $this->News_model->get_new_news();
$data['theNews'] = array("All your base are belong to us!",
"Autotune this news",
"Numa Numa!!",
$return
);
$this->load->view('templates/news_header');
$this->load->view('new_items',$data);
$this->load->view('templates/news_footer');
}
u didnt load your model file on your controller
use this$this->load->model('News_model');

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