i'm try to get news_id from database but when go to view say this error :
Trying to get property of non-object / Message: Message: Undefined variable: xls
model:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mdl_tagihan extends CI_Model { public function employeeList() {
$this->db->select(array('e.id', 'e.IDTAGIHAN', 'e.BANDWIDTH_BILLING', 'e.SITE_NAME', 'e.PERIODE_TAGIHAN',
'e.REGION', 'e.NOMINAL', 'e.USED_FOR','e.RECON_PERIOD','e.OA_DATE','e.REQUEST_ID','e.TRANSMISSION_ID','e.PROVIDER','e.PRODUCT',
'e.SOW', 'e.NE_ID', 'e.NE_NAME', 'e.FE_ID', 'e.FE_NAME', 'e.BANDWIDTH', 'e.SERVICE_2G', 'e.SERVICE_3G', 'e.SERVICE_4G', 'e.TOTAL_SERVICE'));
$this->db->from('import as e');
$query = $this->db->get();
return $query->result_array();
}
controller:
class Ctrl_tagihan extends CI_Controller { public function __construct()
{
parent::__construct();
if($this->session->userdata('group') != '1'){
$this->session->set_flashdata('error','Maaf, login first!');
redirect('CTRL_Login');
}
$this->load->model('Mdl_tagihan');
$this->load->helper(array('form', 'url'));
$this->load->library('upload');
}
public function index()
{
$data['tagihan'] = $this->Mdl_tagihan->get_tagihan();
$this->load->view('admin/dbtagihan/index_tagihan', $data);
}
public function export_excel(){
$data = array( 'IDTAGIHAN' => 'Laporan Excel',
'dbtagihan' => $this->Mdl_tagihan->listing());
$this->load->view('admin/dbtagihan/laporanexcel_tagihan',$data);
}
view:
<?php ("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=$xls.xls.xls"); ("Pragma: no-cache"); ("Expires: 0") ?> <table border="1" width="100%"> <thead> <tr>
In order to use the $xls on your view, you have to supply a $xls variable on the controller :
public function export_excel(){
$data = array( 'IDTAGIHAN' => 'Laporan Excel',
'dbtagihan' => $this->Mdl_tagihan->listing());
$data['xls'] = 'Title';
$this->load->view('admin/dbtagihan/laporanexcel_tagihan',$data);
}
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
This is the complete code (I want to select the view through a variable set on the MY_Controller):
I would like to pass that variable but it doesn't "reach" the view it gives me
$ses_group = "not_logged_in" with a test echo and I'm not setting anything on the MY_Controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
public function __construct()
{
parent::__construct();
}
function index() {
if (logged_in() == TRUE)
{
if (in_group('users'))
{
$this->special_data['ses_group'] = 'users';
}elseif (in_group('empresas'))
{
$this->special_data['ses_group'] = 'empresas';
}elseif (in_group('admin'))
{
$this->special_data['ses_group'] = 'admin';
}else{
// $this->special_data['ses_group'] = 'not_logged_in';
}
}
return $this->special_data;
}
The Main_Controller:
function index(
$data = array(
'ses_group' => $this->special_data
);
$this->load->view('auth/descricao_anuncio', $data);
)
The view:
<?php if($ses_group="not_logged_in"){ ?>
<li>Login</li>
<?php }elseif($ses_group="users"){ ?>
<li>Your Area</li>
<li>Logout</li>
<?php }elseif($ses_group="empresas"){ ?>
<li>Empresa</li>
<li>Logout</li>
<?php }?>
echo $ses_group;
Thanks againg!
Controller
<?php
class MY_Controller extends CI_Controller {
protected $special_data = array();
function MY_Controller () {
parent::Controller();
}
function special_data($val)
{
if(a){
$this->special_data['ses_group'] = 'users';
}elseif(b){
$this->special_data['ses_group'] = 'companies';
}else{
$this->special_data['ses_group'] = 'admin';
}
return $this->special_data;// return value of the function
}
}
You can send your value from controller to views by using $this-> in your MY_Controller
Controller
class Main_controller extends MY_Controller {
function __construct() {
parent::MY_Controller();
}
$this->data['group']= $this->special_data['ses_group'];// call function and pass parameter
$this->load->view('view_x', $this->data);
}
Views
<?php echo $group; ?>
Is this what you need
controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
function special_data($val)
{
if($val=="a"){
$this->special_data = 'users';
}elseif($val=="b"){
$this->special_data = 'companies';
}else{
$this->special_data = 'admin';
}
return $this->special_data;// return value of the function
}
}
class Main_controller extends MY_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$data = array(
'group' => $this->special_data('a')
);
$this->load->view('view_x', $data);
}
}
view
<?php echo $group; ?>
I have a model called register_model.php, which I have loaded in a function in my controller (register_controller.php). Model file is placed into model folder itself. Still, I get this error.
An Error Was Encountered
Unable to locate the model you have specified: register_model
register_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
// function index()
// {
//
// }
public function register() {
if ($this->session->userdata('logged_in'))
{
//user is already logged in
redirect('index.php');
}
else {
//init
//$data['country_list']=$this->config->item('um_country_list');
$data['username'] = '';
$data['firstname'] = '';
$data['lastname'] = '';
$data['email'] = '';
// $data['password'] = '';
//$data['userlevel'] = '';
//load rules
$rules = $this->config->item('um_register_rules');
//default msg
$data['msg'] = $this->lang->line('um_form_msg');
$this->load->model('register_model');
if (isset($_POST['submit'])) {
//the user has submitted the form
//get the user input
$data['username'] = $this->input->post('username');
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$data['email'] = $this->input->post('email');
$data['password'] = $this->input->post('password');
//$data['userlevel'] = $this->input->post('userlevel');
$this->form_validation->set_rules($rules); //check with the rules
if ($this->form_validation->run() === FALSE) {
//validation failed
$data['msg'] = $this->lang->line('um_form_error');
$this->load->view('user_register_form', $data);
} else {
//validation passed
$dbdata = array(
'username' => $this->input->post('username'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
//'userlevel' => $this->input->post('userlevel')
);
$this->register_model->register_user($dbdata);
$data['msg']=$this->lang->line('um_form_activate');
//render the view
$this->load->view('um_msg', $data);
}
} else {
//render the view
$this->load->view('user_register_form', $data);
}
}
}
}
?>
register_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Register_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function register_user($dbdata) {
$this->db->insert('users', $dbdata);
}
}
?>
Probably you should include your model inside your controller, try to include after parent::__construct
This should look like so:
class Registration extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
or if you using this model in most controllers you can include it using autoload, go to /config/autoload.php search for $autoload['model'] and add your model inside the suitable array
First of all load your model in your controller ..
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
This will work for you...
I am trying to work through the CodeIgniter tutorial and my news pages won't output data from the foreach loop. I get the following messages:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: pages/index.php
Line Number: 1
and
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/index.php
Line Number: 1
This is my model class:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
}
And my controller:
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
And the first view:
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
and the second:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
I know there are other questions about the tutorial but none seemed to help me.
Thanks.
in model you have closed your class after constructor. Should be closed after all function.
Also view() is initialized twice.
I have changed my application core/MY_Controller.php extendable. Before I could get data from mysql but Now I get Message: Undefined variable: records. How can i get data again ?
MY_Controller.php :
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
My home controller :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$this->data);
}
Services_model :
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
This is the where i get error in home.php view :
<ul class="blog-medium">
<?php foreach($records->result() as $row): ?>
<li><h1><?php echo $row->title; ?></h1></li>
<?php endforeach ?>
The error message shows : Message: Undefined variable: records
In your controller it looks like you are setting $data['records'] when you should be setting $this->data['records']
I found a solution. In my Home controller, I was using the view as :
$this->render_page('pages/'.$page,$this->data);
But I have to use it as here :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
I dont know why but I tried this before. It did not make but it is working now.