error in sending multiple variable from controller to view.
Error produce in view
Message: Undefined variable: heading
Controller Code:
public function index()
{
$this->load->model('database');
$result['a'] = $this->database->factory_view();
$this->load->view('header');
$heading['heading']= '<span class="float-left" style="margin-top:10px;"><h3>Machinery and Factory</h3></span>';
$this->load->view('factory',$result,$heading);
$this->load->view('footer');
}
$data['posts'] = $posts;
$data['comments'] = $comments;
$this->load->view('your_view', $data);
in view taemplate ...
foreach($posts as $post) {
...
}
foreach($comments as $comm) {
...
}
public function index()
{
$this->load->model('database');
$result['a'] = $this->database->factory_view();
$this->load->view('header');
$result['heading']= '<span class="float-left" style="margin-top:10px;"><h3>Machinery and Factory</h3></span>';
$this->load->view('factory',$result);
$this->load->view('footer');
}
Just like this
public function index()
{
$this->load->model('database');
$result['a'] = $this->database->factory_view();
$this->load->view('header');
$result['heading']= '<span class="float-left" style="margin-top:10px;">
<h3>Machinery and Factory</h3></span>';
$result['param_1'] = "xyz";
$result['param_2'] = "abc";
$this->load->view('factory',$result);
$this->load->view('footer');
}
try this one out. this will work...
public function index()
{
$this->load->model('database');
$data['a'] = $this->database->factory_view();
$this->load->view('header');
$data['heading']= '<span class="float-left" style="margin-top:10px;"><h3>Machinery and Factory</h3></span>';
$this->load->view('factory',$data);
$this->load->view('footer');
}
then call the $a and $heading inside your page view.
Related
Can't find the error, it has to be with the query at "comunas" data base.
Here are the codes:
1) The 2 tables:
comunas:
comId comNombre comRegion
regiones:
regId regNombre
2) Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Usuarios extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->model("regiones_model");
$this->load->model("comunas_model");
}
public function index_get()
{
$data['regionDrop'] = $this->getRegiones();
//loads up the view with the query results
$this->load->view('panel/usuario_agrega_view',$data);
}
public function getRegiones()
{
$this->db->select('regId,regNombre');
$this->db->from('regiones');
$query = $this->db->get();
// the query mean select cat_id,category from category
foreach($query->result_array() as $row){
$data[$row['regId']]=$row['regNombre'];
}
// the fetching data from database is return
return $data;
}
public function getComunaByRegion($regId=string)
{
$this->db->select('comId,comNombre, comRegion');
$this->db->from('comuna');
$this->db->where('comRegion',$regId);
$query = $this->db->get();
return $query->result();
}
//call to fill the second dropdown with the comunas
public function buildDropComunas()
{
//set selected country id from POST
echo $regId = $this->input->post('id',TRUE);
//run the query for the comunas we specified earlier
$districtData['districtDrop']=$this->comunas_model->getComunaByRegion($regId);
$output = null;
foreach ($districtData['districtDrop'] as $row)
{
//here we build a dropdown item line for each query result
$output .= "<option value='".$row->comNombre."'>".$row->comNombre."</option>";
}
echo $output;
}
}
3) Models: I'm not using the querys at models, I can't "touch them" for now, so I'm putting that code direct in the controller.
4) View:
<script type="text/javascript">
$(document).ready(function() {
$("#regionesDrp").change(function(){
/*dropdown post *///
$.ajax({
url:"<?php echo base_url();?>usuarios/buildDropComunas",
data: {id: $(this).val()},
type: "POST",
success:function(data){
$("#comunaDrp").html(data);
}
});
});
});
</script>
<!--country dropdown-->
<?php
echo form_dropdown('regionesDrp', $regionDrop,'','class="required" id="regionesDrp"'); ?>
I tried to follow the example from: http://www.c-sharpcorner.com/uploadfile/225740/cascading-drop-down-in-codeigniter-using-ajax/
But I have a different database (FK comId) and as I told you, I can't use a model file for this. Anyway, I tried with a region_model and comuna_model but it's the same.
Use append() not html()
$("#comunaDrp").append(data);
First of all, the function getComunaByRegion should be:
public function getComunaByRegion($comRegion=string)
{
$this->db->select('comId,comNombre');
$this->db->from('comunas');
$this->db->where('comId',$comRegion);
$query = $this->db->get();
return $query->result();
}
Plus, in the controller, the function public function buildDropComunas_post() wasn't called with POST, that was the main reason of my 404 not found.
I have 2 functions in my controller that I'm sending a session using the first one to the second function. What I am talking about is:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result/');
}
and the second function:
function search_result()
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
Everything is fine but, the problem is that, I want to prevent direct access to search_result() function. I mean, I want to unset search_term session when the user calls search_result() directly. What should I do?!
You can use flashdata: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages
And this is the code that do what are you looking for:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_flashdata('search_term', $search_term);
redirect('search_result/');
}
function search_result()
{
$search_term = $this->session->flashdata('search_term') ? $this->session->flashdata('search_term') : '';
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
There are many ways you can do it. Here are some
append a get parameter if it exists then search else redirect back
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result?status=1');
}
public function search_result()
{
$status = $this->input->get('status');
if(status == 1){
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}else{
$this->session->unset_userdata('search_term');
redirect('search');
}
}
Protect you seach_result function and dont let user direct call it.
Using _ will do it for you.
Here is the link you can read.
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
$this->_search_result($search_term);
}
function _search_result($keyword)
{
if(strlen($keyword)>0){
$data['search_term'] = $keyword;
$this->load->view('includes/template', $data);
}else{
redirect('search');
}
}
in the search function
redirect('search_result/'.$this->session->userdata('search_term'));
in the search_result function
function search_result()
{
if($this->uri->segment(3))
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
else
{
redirect('search_result/','refresh');
}
}
hope it will help you.please let me know if you face any problem.
if my page is Company/add/4 (company is controller, add is the function and 4 is the id of that company)
is this how I would echo the id to another page:
public function add($id) {
$data['data'] = $id;
$this->load->view('templates/header');
$this->load->view('locations/add', $data);
$this->load->view('templates/footer');
then on the view:
echo $data['id'];
This is working correctly but i'm not sure its the best way to do it. because it seems like it would make more sense to have:
public function add($id) {
$this->load->view('templates/header');
$this->load->view('locations/add', $id);
$this->load->view('templates/footer');
but that doesn't appear to work.
Updated the controller
public function add($id) {
$data['id'] = $id;
$this->load->view('templates/header');
$this->load->view('locations/add', $data);
$this->load->view('templates/footer');
On VIew
just do echo $id
codeigniter passes an array to the view so that you can pass not only 1 but group of data to you're views, use the key that you assigned on the controller as the variable's name on the view
whatever you place on the key that will be the name of you're variable in the view
//controller
$data['test'] = 'test';
//view
var_dump($test);//string "test"
//controller
$data['test'] = array('1','2');
//view
var_dump($test);//array()
//controller
$data['test'] = 1;
//view
var_dump($test);//int "1"
so on and so forth
I worked on this for a day. I get this same problem, but I don't understand.
<?php
class Users extends CI_Controller
{
function index()
{
redirect('Users/login');
}
function login()
{
$data['title'] = 'Selamat datang di Sistem Informasi Koperasi';
$data['main_content'] = 'login';
$this->load->view('Users/template', $data);
}
function logout()
{
$this->session->sess_destroy();
$this->session->set_flashdata('info_login', 'Anda sudah keluar dari sistem');
redirect('Users/login');
}
function validate()
{
//Load User Model
$this->load->model('Users_Model');
//Validate User
$query = $this->Users_Model->validate();
if($query != '') {
//Mengambil Roles dari Groups
$roles = $this->Users_Model->roles($query->group_id);
//$this->login_model->last_login($query);
$data = array(
'username' => $query->username,
'roles' => $roles,
'is_logged_in' => true
);
$this->session->set_userdata($data);
if($roles == 'administrators') {
redirect('Administrators/index');
} elseif($roles == 'managers') {
redirect('Managers/index');
}
else {
$this->session->set_flashdata('info_login', 'Mohon maaf anda belum terdaftar sebagai Group! Silahkan hubungi admin!');
redirect('Users/login');
}
} else {
$this->session->set_flashdata('info_login', 'Maaf,username dan password yang anda masukkan salah,silahkan coba kembali!');
redirect('Users/login');
}
}
}
In Chrome and Firefox I get this message. What should i do?
This webpage has a redirect loop The webpage at
http://localhost/simpks/index.php/Users/login has resulted in too
many redirects. Clearing your cookies for this site or allowing
third-party cookies may fix the problem. If not, it is possibly a
server configuration issue and not a problem with your computer. Here
are some suggestions: Reload this webpage later. Learn more about this
problem. Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many
redirects.
this is my view template.php
<?php
$this->load->view('includes/header',$main_content);
$this->load->view('Users/'.$main_content);
$this->load->view('includes/footer');
?>
this is my model Users_Model.php
<?php
class Users_Model extends CI_Model{
function validate(){
$this->db->where('username',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('Users');
if($query->num_rows == 1){
$row = $query->row();
return $row;
}
}
function roles($id){
$this->db->where('id',$id);
$query = $this->db->get('Groups');
if($query->num_rows == 1){
$row = $query->row();
return $row->name;
}
}
}
?>
use include instead loader if you call it in view.
ex : include 'includes/footer';
you don't have to put redirect('Users/login'); for session checking in your view class. Just erase it.
If you need redirect, put it in another page like users/test. If session is expired in users/test call redirect method in users/test controller. For better structure, i think you should minimize php function in view.
I am also facing that problem but both page's controllers i've redirects method so I add a refresh in redirect method, try it.
read at bottom of page CI redirect with refresh
<?php
class Users extends CI_Controller
{
function index()
{
redirect('another_controller/login');
}
}
Create another controller - another_controller.php
class another_controller extends CI_Controller
{
function login()
{
$this->load->view('home');
}
}
I am new in Grocery Crud with Code Igniter and need a help. I have table vaboteni (emploees) and it work well. But I get stuck with code add more action. When I click to add action button I got error 404 Page Not Found. I want to fetch "id" from one row in table and pass to another view in order to display data for only one employee. I have site in local server, address localhost/bis_resursi/index.php/vraboteni/vraboteni_managment
Here is my Controller vraboteni.php
function vraboteni_management()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('vraboteni');
$crud->set_subject('вработен');
.....
$crud->add_action('Преглед', '', 'vraboteni/vraboten_managment/pregled','ui-icon-plus');
function pregled($id)
{
$this->load->model("vraboteni_pregled_model");
$data["result"] = $this->getVraboteniPregled($vrabotenID);
$this->load->view("pregled", $data);
}
$output = $crud->render();
$this->_example_output($output);
}
and Models: vraboteni_pregled_model.php
<?php
class Vraboteni_Pregled_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function getVraboteniPregled($id){
$query = $this->db->query("SELECT * FROM vraboteni WHERE vraboteID = '$id' ");
return $query->result();
}
and in view vraboten_view.php I put
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>
I managed to find solution. Right code is:
Controller vraboteni.php
$crud->add_action('Преглед', '', 'vraboteni/get','ui-icon-plus');
$output = $crud->render();
$this->_example_output($output);
}
function vraboteni()
{
$crud = new grocery_crud();
$crud->set_table('vraboteni');
$output = $crud->render();
print_r($output);
}
function getall()
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_getall();
$this->load->view('vraboten_view',$data);
}
function get($vrabotenID)
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_get($vrabotenID);
$this->load->view('vraboten_view',$data);
}
Models vraboten_model.php
<?php
class Vraboten_model extends CI_Model{
function vraboten_model(){
parent::__Construct();
}
function vraboten_getall(){
$this->load->database();
$query=$this->db->get(' vraboteni');
return $query->result();
}
function vraboten_get($vrabotenID){
$this->load->database();
$query=$this->db->get_where(' vraboteni',array('vrabotenID'=>$vrabotenID));
return $query->row_array();
}
}
and view vraboten_view.php
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>