View code
<?php foreach($payment as $test){?>
<tr>
<td><?php echo $test->customerName;?></td>
<td><?php echo $test->dateTime;?></td>
<td><?php echo $test->amount;?></td>
</tr>
<?php }?>
Controller code
function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('Income_sales_model');
}
public function index()
{
$this->data['pja_test'] = $this->Income_sales_model->get_all();
$this->load->view('income_sales_view', $data);
}
**AND HERE IS MY ERROR **
I'm stuck here I don't know what to do..
the variable you are trying to access must be a key of $data...
So,print_r($pja_test);die; in your view you will get the appropriate output
If still any issue let me know
Controller code
function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('Income_sales_model');
}
public function index()
{
$data['payment'] = $this->Income_sales_model->get_all(); // $query->result();
$this->load->view('income_sales_view', $data);
}
Related
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);
}
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.
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.
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.
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;
?>