Why won't my codeigniter model load? - codeigniter

Here's my model:
<?php
class Dbtest extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
parent::Model();
$this->db->select('title, content');
$this->db->from('posts');
$this->db->where('post_id', 1);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
Here's my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dbtest extends CI_Controller {
function index() {
$this->load->model('dbtest');
$data['rows'] = $this->dbtest->getAll();
$this->load->view('dbtest', $data);
}
}
And here's my view
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="container">
<p>My view has been loaded</p>
<?php foreach($rows as $r) : ?>
<h1><?php echo $r->title; ?></h1>
<div><?php echo $r->content; ?></div>
<?php endforeach; ?>
</div>
</body>
</html>
I keep getting a 500 server error. What could I possibly be doing wrong. I have autoloaded the database library. Please help a codeigniter newbie trying to learn this great framework.

function getAll() {
parent::Model(); //<-------------------Remove this line
$this->db->select('title, content');
$this->db->from('posts');
After taking a quick look, why is this line there?
It is a php4 constructor call to the old version of the Model base class that no longer exists..
remove and try again.
EDIT
Also, you cannot name a model AND Controller the same name, they have namespace conflicts.
call the model Dbtest_model or something and use it that way.
Also, this is unnecessary
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
$q->result() returns an array, no need to loop through and rebuild... Just do this...
if($q->num_rows() > 0) {
return $q->result();
}

Well after some pretty crappy stuff... I finally found that the problem was in my desition to put some Vars after the
class CI_model{}
in the Core Model (application/system/core/Model) of my CI application, just for the goods of autocompletion for Eclipse/aptana/netbeans. So if you can revert back to normal from a clean CI installation, i think it should get back to normal, this was my final solution. Happy Coding!

What happens if you call parent::CI_Model(); instead of parent::__construct();?
Ci is designed to work on php4, and __construct() wasn't added until php5.

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

How do you set a variable global in codeigniter

Note: Ive already set mine to extend to a base controller extending the ci controller for all my controller, the problem is how do i pass and declare a variable and pass it on the template?
You can pass the data to view file from controller as below :
Home.php Controller file
<?php
class Home extends CI_Controller {
public function index()
{
$data['title'] = "Title";
$data['heading'] = "Heading";
$this->load->view('home', $data);
}
}
home.php view file
<html>
<head>
<title> <?php echo $title; ?> </title>
</head>
<body>
<h1><?php echo $heading; ?></h1>
</body>
</html>
You can use 2 difftent methods view data from the controller.
Method 1 :
Controller file : Home.php
<?php
class Home extends CI_Controller {
public function index()
{
$this->data['title'] = "Title";
$this->data['heading'] = "Heading";
$this->load->view('home', $this->data);
}
}
Method 2 :
Controller file : Home.php
<?php
class Home extends CI_Controller {
public function index()
{
$data['title'] = "Title";
$data['heading'] = "Heading";
$this->load->view('home', $data);
}
}
Note : View file same here. no changes
view file : home.php
enter code here
<html>
<head>
<title> <?php echo $title; ?> </title>
</head>
<body>
<h1><?php echo $heading; ?></h1>
</body>
</html>

autocomplete textbox are not working in ci

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 index()
{
$searchTerm = $_GET['term'];
$data['search'] = $this->Fetch_data->autocomplete($searchTerm);
$this->load->view('index',$data);
}
}
view: index.php
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input type="text" name="colleges" id="colleges" class="form-control" />
<script>
$(function() {
$( "#colleges" ).autocomplete({
source: '<?php echo base_url("index.php"); ?>/test/index';
});
});
</script>
model: Fetch_data.php
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function autocomplete($searchTerm)
{
$this->db->select('college_name,field');
$this->db->from('all_colleges');
$where = "short_name like '%".$searchTerm."%' or college_name like '%".$searchTerm."%'";
$this->db->where($where);
$this->db->order_by('college_name');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
I am new in ci. Using this code I want to create a autocomplete suggestion box in codeigniter But this code is not work for me. So, how can I create autocomplete suggestion box in ci and please tell me what wrong in my code can I use this code for autocompletion.
Thank You
UPDATED You can try this:
in you Model Fetch_data.php
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function autocomplete()
{
$this->db->select('college_name,field');
$this->db->from('all_colleges');
$this->db->order_by('college_name');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
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 index()
{
$all_data = array();
$result = $this->Fetch_data->autocomplete(); //collect all college name
foreach($result as $key=>$value){
array_push($all_data, $value["college_name"]);
}
$data['search'] = $all_data;
$this->load->view('index',$data);
}
}
view: index.php
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input type="text" name="colleges" id="colleges" class="form-control" />
<input type='hidden' id='mydata' value='<?php echo json_encode($search);?>' />
<script>
$( function() {
var oData = $("#mydata").val();
oData = JSON.parse(oData);
$( "#colleges" ).autocomplete({
source: oData;
});
});
</script>
You can get all the college_names and store it with a variable then when you enter a letter you can no longer use ajax because it is already stored with a variable.
Hope it will work.

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.

Resources