My problem is that I can't get data in database but if I use var_dump() I have data. This is the error:
A PHP Error was encountered Severity: Notice Message: Undefined index: projectid Filename: views/accounting_status.php Line Number: 46
This is my View
This page was accesed incorrectly.
<div class="well">
<p><h1><?=$p['projectid']?></h1></p>
<p><?=$p['code']?></p></div>
This is my Controller
function id($projectid){
$data['p'] = $this->Accounting_Model->get_awardedid($projectid);
//echo "<pre>".print_r($data)."</pre>";
$this->load->view('tmp_header');
$this->load->view('accounting_status',$data);
}
This is my Model
function get_awardedid($projectid){
$this->db->select('*')->from('projects')
->join('customer', 'projects.customerid = customer.customerid')
->join('employees', 'projects.endorse_by = employees.employeeid')
->where(array('acctg'=>1,'projectid'=>$projectid))
->order_by('datecreated', 'desc');
$query = $this->db->get();
return $query->result_array();
}
If the result will be a single row then change on model as follows:
function get_awardedid($projectid){
$this->db->select('*')->from('projects')
->join('customer', 'projects.customerid = customer.customerid')
->join('employees', 'projects.endorse_by = employees.employeeid')
->where(array('acctg'=>1,'projectid'=>$projectid))
->order_by('datecreated', 'desc');
$query = $this->db->get();
return $query->row_array();
}
If result is multiple records, then use for loop to print the values in view.
<div class="well">foreach($p as $value){<p><h1><?=$value['projectid']?></h1></p><p><?=$value['code']?></p>}</div>
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.
Sending Value from controller to model but it shows error
"
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'email'
Filename: models/Pmodel.php
Line Number: 58
"
This is the controller which sends the value
$user_email=$_GET['email'];
$this->load->model('Pmodel');
$data['email']=$this->Pmodel->profile_model($user_email);
$this->load->view('dashboard/profile',$data);
and now the model that acquire the values
public function profile_model($arr)
{
$email=$arr->'email';
print_r($email);
$query=$this->db->where(['user_data.email'=>$email])
->from('user')
->join('user_data', 'user_data.email = user.email')
->get();
$q= $query->result_array();
return $q;
}
When i Print_r($email) it shows error
try to print_r($arr) only.
you can just directly use like this
$query=$this->db->where(['user_data.email'=>$arr])
->from('user')
->join('user_data', 'user_data.email = user.email')
->get();
Or,
$email = $arr;
$query=$this->db->where(['user_data.email'=>$email])
->from('user')
->join('user_data', 'user_data.email = user.email')
->get();
$email=$arr->'email'
Maybe replace it with this:
$email=$arr['email']
Or
$email=$arr->email
I am getting the following error.
Message: Trying to get property of non-object
Table Structure:
name ='fb' value='https://www.facebook.com/mypage'
Model:
function get_fb_url(){
$query=$this->db->select('value');
$query=$this->db->from('preferences');
$query=$this->db->where('name','fb');
return $this->db->get()->row();
}
Controller:
$data['fb_url']=$this->Home_model->get_fb_url()->value;
View:
<li><img src="css/imgs/fb.png" /></li>
When i run my page it give me error
Message: Trying to get property of non-object
only when if value in the table is link like above, if its simple text then text properly show on page.
How Can i get rid off this error?
Instead of echo use return type
Change
echo $this->db->get()->row();
to
return $this->db->get()->row();
And add $ before variable name in view
<a href="<?php echo $fb_url;?>"
UPDATED
Change your model code to
function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name', 'fb');
return $this->db->get()->row();
}
you must try like this:
function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name', 'fb');
$query = $this->db->get();
return $query->row();
}
and in Controller:
$result = $this->Home_model->get_fb_url();
//try print $result, it must be an stdObject
$data['fb_url']=$result->value;
it is because in $data['fb_url']=$this->Home_model->get_fb_url()->value; / differentiate the link to the other function, that's why u r getting this error.
get_fb_url is returning object array not object. so you have to return value from method.
function get_fb_url()
{
$query=$this->db->select('value');
$query=$this->db->from('preferences');
$query=$this->db->where('name','fb');
return $this->db->get()->row()->value;
}
and than
$data['fb_url']=$this->Home_model->get_fb_url();
In Your model do this
public function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name','fb');
$query = $this->db->get();
$row = $query->row();
return $row;
}
In Controller :
$data = array();
$result = $this->Home_model->get_fb_url();
$data['fb_url'] = $result->value;
$this->load->view('your_view',$data);
In View :
echo $fb_url;
I am facing the following error:
Trying to get property of non-object and Undefined variable php
errors in my code
Controller:
function showDoctorInformation(){
$this->load->model('PatientModel');
$data['doctorinfo'] = $this->PatientModel->getDoctorInformation();
$this->parser->parse('patient_msgview', $data);
}
Model:
function getDoctorId() {
$this->db->from('person');
$this->db->select('doctorId');
$doctorId = $this->db->get()->result();
return $doctorId;
}
function getDoctorInformation() {
$doctorId = $this->getDoctorId();
$this->db->from('DoctorInfo');
$this->db->where('doctorId', $doctorId);
$this->db->select('name', 'surname', 'Bio', 'Address', 'img');
$doctorinfo = $this->db->get()->result();
return $doctorinfo;
}
View:
<?= $doctorinfo->name ?>
I have displayed information from the database before with this method and I can't see the error now.
result() return
This method returns the query result as an array of objects, or an
empty array on failure
So you need to fetch single data form your database using ->row()
function getDoctorId() {
$this->db->select('doctorId');
$this->db->from('person');
$this->db->select('doctorId');
$query = $this->db->get();
if ($query->num_rows == 1) {
$row=$query->row();// fetch single row
return $row->doctorId;// get doctor id
} else {
return FALSE;
}
}
And in viwe you have to get your data using foreach loop
For exm
foreach ($doctorinfo as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
I'm trying to pass along the question AND the id of the question in the first query and then with that id get that poll's options and add those to the array. Not seeing what I'm doing wronge here.
Here's the error I am getting:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysql_result::$row
Filename: models/sitemodel.php
Line Number: 161
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/sitemodel.php
Line Number: 161
Code:
function getPoll() {
$this->db->select('site_polls.id, site_polls_questions.poll_question');
$this->db->from('site_polls');
$this->db->join('site_polls_questions', 'site_polls_questions.id = site_polls.site_polls_questions_id');
$this->db->where('site_polls.status_id', 1);
$this->db->order_by('site_polls.date_posted', 'desc');
$this->db->limit(1);
$query = $this->db->get();
$id = $query->row->id;
$this->db->select('site_polls_questions_options.poll_option');
$this->db->from('site_polls_questions_options');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result_array();
}
I'm trying to figure out how I can add the question of the poll into the array.
row() is a function and not a property.
$id = $query->row()->id;
or
$id = $query->row(0)->id;
$id = $query->row->id;
Should be:
$id = $query->row()->id;
$query->row() is a function, not a property.
Try this:
$query = $this->db->get()->first_row();
if ( !empty($query->id))
{
$id = $query->id;
}
else
{
return array();
}