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>
Related
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
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 } ?>
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.
this is my controller
<?php
class Site extends CI_Controller{
public function index(){
$this->load->view('home');
}
}
?>
This is my model
<?php
class AdminModel extends CI_Model{
//function to get all the questions
function getAllQA(){
$result=$this->db->get('question');
if($result->num_rows()>0){ //this checks if the result has something if blank means query didn't get anything from the database
return $result;
}
else{
return false;
}
}
}
?>
and this is my view PAge
<form method="get" action="">
<div id="container">
<?php
$this->load->model('adminmodel');
if($result=$this->adminmodel->getAllQA()){
foreach($result->result() as $rows){
echo $rows->question;
echo $rows->option1;
}
}
else{
echo "No question found";
}
?>
</div>
</form>
So am calling the model in view called home.php page but its showing an error Call to a member function getAllQA() on a non-object in So but when am calling the model in controller its working fine but why is it showing error when am loading and calling the method in view page
Load your models inside the constructor of your controller
your controller should be like this
<?php
class Site extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('adminmodel');
}
//if you want to pass data or records from database to view do as blow code
public function index()
{
//define an empty array
$data = array();
$records = $this->adminmodel-> getAllQA();
$data['records'] = $records;
//pass the data to view you can access your query data inside your view as $records
$this->load->view('home',$data);
}
}
?>
You should not be loading Models from view files. Codeigniter is a MVC framework which means that all communication with the model should be handled by the controller.
The technical reason that this isn't working is likely that the view file is not a php class and therefore $this does not exist. Thats regardless, if you want to do something like this, don't use codeigniter!
not sure if you've done it.
Usually you call your model from within the controller.
In your case it would be:
class Site extends CI_Controller{
public function index(){
// load your model
$this->load->model('adminmodel');
// do your fancy stuff
$data['allQA'] = $this->adminmodel->getAllQA();
// here you can pass additional data e.g.
$data['userdata'] = $this->adminmodel>getuserinfo();
// pass data from controller --> view
$this->load->view('home',$data);
}
}
You can access the data in your view file by acessing $allQA or $userdata respectively. E.g.
foreach ($allQA as $qa){
echo $qa->question . "<br>" . $qa->option . "<br>";
}
or somewhere within a div
<div class="userbadge">
<?php echo $userdata; ?>
</div>
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.