Trying to add to an array - php

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();
}

Related

Getting Error When passing value from controller to model in codeigniter

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

cant display the data from controller

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>

Undefined property: stdClass in my codeigniter controller

This is my controller function
public function check_orgn_mail()
{
$this->load->model('registrationmodel', '', TRUE);
$omail = $this->input->post('mailid');
$mailresult = $this->registrationmodel->check_org_mail();
$smil = explode("#", $omail);
$registerCompanyID = null;
if ($mailresult)
{
foreach ($mailresult as $row)
{
$registerCompanyID = $row->id;
$cmail = explode("#", $row->email);
$acmail[] = $cmail[1];
}
if (in_array($smil[1], $acmail))
{
return TRUE;
}
}
$this->form_validation->set_message('check_orgn_mail', 'Invalid Organization Mail id.');
return false;
}
This is the function in my model
function check_org_mail()
{
$this->db->select('email','id');
$this->db->from('customers');
$this->db->where('status', 1);
$query_emai = $this->db->get();
return $query_emai->result();
}
I am getting this error
Severity: Notice
Message: Undefined property: stdClass::$id Filename:
controllers/registration.php
I tried some trial and error but not working. Please help me with this. Previously it was working fine but when I checked with the different mail id, it started giving this error.
change your select to this:
$this->db->select('email, id');
You need to use
$this->db->select('email, id');
All columns are supposed be a single string argument separated by commas.
You are providing multiple arguments.

PHP CodeIgniter: Trying to get property of non-object

Everything seems to work just fine but I keep getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 24
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 31
================================================================
In the view i just echo $company_name
Controller:
$city = $this->ordermodel->get_city($order_reference);
$customerCompanyName = $this->ordermodel->get_company_name($order_reference);
$data['company_name'] = $customerCompanyName;
Model:
function get_city($ordernumber) {
$this->db->where('order_number', $ordernumber);
$city = $this->db->get('order');
return $city->row()->city;
}
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
return $companyname->row()->company_name;
}
Use therresult() method, it should elimiate all of your errors
$row = $city->result(); return $row->city;
Seem that your query has no results, try:
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
if ($companyname->num_rows() > 0) {
return $companyname->row()->company_name;
}
else {
return '';
}
}

Illegal string offset error in codeigniter view

im doing a PHP project using codeigniter 2.1.2 , using mySql database on WAMP, i have stumbled upon the following problem:
Here is my model
function loadUserData() {
$sql = "SELECT username FROM user WHERE username = ?";
$query = $this -> db -> query($sql, $this->session->userdata('username'));
if ($query -> num_rows() > 0) {
foreach ($query->result() as $row) {
$data = $row;
}
return $data;
}
}
The controller
function profile_access() {
if ($this -> session -> userdata('is_logged_in')) {
$this->load-> model('profile_model');
$uData['rows']=$this->profile_model->loadUserData();
$this->load->view('profile_view',$uData);
} else {
echo "<script type=\"text/javascript\"> alert(\"You need to provide your credentials first.\");</script>";
redirect('login_controller/index');
}
}
and the view
<?php
foreach($rows as $r){
echo print_r($r);
echo $r[username]->username; //line no. 81 in my code
}
?>
the print_r($r) from view gives me the following errors:
alphaomega1
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant username - assumed 'username'
Filename: views/profile_view.php
Line Number: 81
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'username'
Filename: views/profile_view.php
Line Number: 81
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/profile_view.php
Line Number: 81
I expect the above code to return only "alphaomega" which is the username in the database. it is however displayed, but i dont know why i'm getting those errors. would appreciate some help with this. thanks.
In your view:
foreach($rows as $r){
echo $r->username;
}
In your model:
function loadUserData() {
$sql = "SELECT username FROM user WHERE username = ?";
$query = $this->db->query($sql, $this->session->userdata('username'));
return $query->result();
}

Categories