Message: Undefine variable: exam - codeigniter

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.

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

How to show validation errors in view using redirect in codeigniter?

I try different methods but it's not working.
My question is how can achieve both these requirements: passing validation data and pass my data which I get from the database
*
<?php
class Jobpost extends my_controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('html','url'));
}
function index()
{
$this->load->model('new_post','model');
$ans= $this->model->get_user_data();
$this->load->view('website/job_post',['ans'=>$ans]);
}
public function new_post()
{
if ($this->form_validation->run('post') == FALSE)
{
$this->index(); /* index function above. */
}
else
{
echo "succes";
}
}}?
<div class=" text-center">
<?php echo validation_errors(); ?>
</div>
You can pass the error messages as flashdata. This works with the session library.
It can be implemented as follows
In your controller:
$this->session->set_flashdata('Failure', 'failure message.');
redirect("To your view");
In the View
<?php if($this->session->flashdata('Failure')){ ?>
$('.confirm-div').html('<?php echo $this->session->flashdata('Failure'); ?>').show();
<?php } ?>

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);

How to retrieve data from the database and display it in a html table using Codeigniter

anyone please help me to retrieve the db data and how to view it in html table.is the coding i given is correct or not if not can you please say how i have to give. in order to view it in html table.
Controller
class edit_content extends CI_Controller {
function edit_content()
{
parent::__construct();
$this->load->model('editcontent_model');
$this->load->helper('url');
$this->load->library('acl');
$this->data = $this->editcontent_model->get_contents();
}
}
View
<table>
<tr>
<th>Content</th>
</tr>
<?php foreach($this->data as $r): ?>
<tr>
<tr><?php echo $r['content']; ?>
</tr>
<?php endforeach; ?>
<table>
Model
class editcontent_model extends CI_Model {
var $CI;
function editcontent_model(){
parent::__construct();
}
function get_contents() {
$this->db->select('content');
$this->db->from('contents');
$query = $this->db->get();
return $result = $query->result();
$this->load->view('edit_content/edit_content', $result);
}
}
Try this:
<?php
#mycontroller
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class edit_content extends CI_Controller {
public function __construct(){
parent::__construct();
}
function edit_content()
{
$data = array();
$this->load->model('editcontent_model');
$this->load->helper('url');
$this->load->library('acl');
$data['result'] = $this->editcontent_model->get_contents();
$this->load->view('edit_content/edit_content', $data);
}
}
?>
<!-- myview -->
<table>
<tr>
<th>Content</th>
</tr>
<?php foreach($result as $r): ?>
<tr><?php echo $r->content; ?>
</tr>
<?php endforeach; ?>
</table>
<?php
#mymodel
class editcontent_model extends CI_Model{
function get_contents() {
$this->db->select('content');
$this->db->from('contents');
$query = $this->db->get();
return $result = $query->result();
}
}
Put this in the end of your edit_content function in your controller
$this->load->view('edit_content/edit_content', $result);
Loading of the view should be done in the controller. From what I see, your model have returned the result so the last line is not executed.
Do this in your edit_content function
$result=$this->editcontent_model->get_contents();
$this->load->view('edit_content/edit_content', $result);
and in your view, access the content like:
<?php foreach($result as $result): ?>
<tr>
<tr><?php echo $result[0]->content; ?>
</tr>
<?php endforeach; ?>
You have to do this beacuse the $result is an associative array not a simple one.
Hope this helped you.

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;
?>

Resources