I'm new to PHP and CodeIgniter, and saw that there is many questions mentioning this and been trying them all but nothing seems to work. Everything is auto-loaded in configuration, the database is running and function for posting to database are working but writing to view page doesn't work at all. Except for displaying username, but for that I create a new variable on view page.
Controller
public function ShowNews()
{
$data = array();
$this->load->model('user');
$data['news'] = $this->user->getNews();
$this->load->vars($data);
}
Model
function getNews(){
$q = $this->db->get('News');
if($q->num_rows() > 0){
return $q->result();
}
return FALSE;
}
View
<?php foreach($news as $row) : ?>
<li><?php echo $row->Title; ?> </li>
<li><?php echo $row->Date; ?></li>
<?php endforeach; ?>
This is the error EDIT ves to news
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: admin/Pocetna.php
Line Number: 64
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/Pocetna.php
Line Number: 64
Using WAMP, NetBeans with CodeIgniter. I saw it has to be something with passing data from controller to view but I can't figure it out and been trying few days already, but always having problems.
You're not passing $data to your view. Your controller should be like this:
public function show_news()
{
$this->load->model('user');
$data = [];
$data['news'] = $this->user->get_news();
$this->load->view('news', $data);
}
Your view should also be checking if $news is FALSE, because you'll have some issues with foreach if you loop over the value FALSE. Your model should also be returning result_array not result, foreach cam't loop over objects..
public function get_news()
{
$q = $this->db->get('News');
return($q->num_rows() > 0) ? $q->result_array() : FALSE;
}
Your view should look something like this:
<?php
if($news !== FALSE)
{
foreach($news as $row)
{
echo "<li>{$row['title']}</li>";
echo "<li>{$row['date']}</li>";
}
}
else
{
echo "No news to see here!";
}
?>
Your title also doesn't link up with the error in the post, so that's the solution to the one in the title.
Related
Fetching page title from database getting error as
A PHP Error was encountered Severity: Notice Message: Trying to get
property of non-object Filename: controllers/digital_marketing.php
Line Number: 20
A PHP Error was encountered Severity: Notice Message: Trying to get
property of non-object Filename: controllers/digital_marketing.php
Line Number: 21
I am having two tables like
1.digital_marketing
2.pagetitle
In the first table i am inserting the data related to digital marketing along with digitalmarketing_name(The table will be in the following format)
digital_id description digitalmarketing_name
1 dfhbsdjbfd digital_marketing
Second Table:(pagetitle)
pagetitle_id page_title title
1 digital_marketing Digital Marketing
In this i am comparing page_title if both the page_titles match then i need to display title name but while comparing that getting an error which i have posted above.
If i am using underscore(_) in the page title it is getting that error if not it is working fine.
Controller:
class Digital_marketing extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('index_model');
$this->load->model('digitalmarketing_model');
}
public function index()
{
$data['records2']=$this->digitalmarketing_model->get_digitalmarketing();
$pageReult = $this->digitalmarketing_model->getpagetitle($this->uri->segment(1));
$data['page_title']=$pageReult->title;
$data['meta_tags']=$pageReult->meta_tags;
$data['mainpage'] = "digital-marketing";
$this->load->view('templates/template',$data);
}
Model:
function getpagetitle($id)
{
$this->db->select('P.*,D.digitalmarketing_name');
$this->db->from('pagetitle AS P');
$this->db->join('digital_marketing AS D','D.digitalmarketing_name=P.page_title','INNER');
$this->db->where(array('P.page_title'=>$id));
$q=$this->db->get();
//var_dump($this->db->last_query());
//print_r($q->num_rows());
if($q->num_rows()>0)
{
$output = $q->result();
return $output[0];
}
else
{
return false;
}
}
The pagetitle which i have inserted in digital_marketing table it is my controller name.
You can change your modal function
Controller.php
public function index()
{
$data['records2']=$this->digitalmarketing_model->get_digitalmarketing();
#echo $this->uri->segment(1); exit;
$pageReult = $this->digitalmarketing_model->getpagetitle($this->uri->segment(1));
$data['page_title']=$pageReult->title;
$data['meta_tags']=$pageReult->meta_tags;
$data['mainpage'] = "digital-marketing";
$this->load->view('templates/template',$data);
}
Modal : digitalmarketing_model.php
function getpagetitle($id) {
$this->db->select('p.*,d.digitalmarketing_name');
$this->db->from('digital_marketing AS d');
$this->db->join('pagetitle as p', 'p.page_title = d.digitalmarketing_name', 'left');
$this->db->where('p.page_title',$id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$row = $query->row();
return $row;
} else {
return false;
}
}
I hope this will helps you.
I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}
I am fetching some information from mysql database and I am displaying it using the following script(I am using Codeigniter).
Models
public function sum($field, $table, $userfield, $username)
{
return $this->db->query("SELECT SUM($field) FROM $table WHERE $userfield = '$username'");
}
Controller
public function save(){
$data = array(
'sum_simpan' => $this->user_m->sum('besar_simpanan', 'simpanan', 'id', $this->session->userdata('username'))->result()
); }
View
<div class="info-a">
<p>Total Simpanan Anda</p>
<h3><?php
foreach ($sum_simpan as $row) {
echo $row->besar_simpanan;
}
?>
</h3>
</div>
I want to do is sum the contents of a field, and display it in the view.
However I get an error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$besar_simpanan
Filename: anggota/simpanan.php
Line Number: 14
Would you please kindly show me how to do it.
Thanks in Advance :)
Change your model like this
public function sum($field, $table, $userfield, $username)
{
return $this->db->query("SELECT SUM($field) as total FROM $table WHERE $userfield = '$username'");
}
also the view
<h3>
<?php
foreach ($sum_simpan as $row) {
echo $row->total;
}
?>
</h3>
I can't seem to figure about what is wrong with this code. thank yu for your help.
I am getting error messages:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/members.php
Line Number: 24
My model:
public function show_user()
{
$this->db->select('');
$this->db->from('users');
$this->db->where('email',$this->input->post('email'));
$q=$this->db->get('');
if($q->num_rows() > 0 )
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
My controller:
public function members()
{
if ($this->session->userdata('is_logged_in'))
{
$this->load->model('model_users');
$data['member'] = $this->model_users->show_user();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
My view:
echo "<p> Congratulations you are logged in!!</p>";
foreach($member as $row)
{
echo $row->Name. "<br/>";
echo $row->email. "<br/>";
}
So this my code. I want to display user information from database but I am getting errors. please help me..
You only need to return $q-result();
if($q->num_rows() > 0 )
{
return $q-result();
}
I'm not sure what your database looks like but I suspect this is because either the Name or email properties don't exist on the $row object. Are you sure your field in your users table is called Name or is it name (lowercase)? If its lowercase your code should be.
foreach($member as $row)
{
echo $row->name. "<br/>"; //Changed this line
echo $row->email. "<br/>";
}
Also your foreach loop on your model is a bit pointless. You might as well remove it and just do
return $data->row;
my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)