error getting the array result in Join Query in Codeigniter - php

Hello this is my query and I have the problem getting the result_array().
This is the error:
Fatal error: Call to a member function result_array() on a non-object in C:\xampp\htdocs\plss\application\views\Admin\post_refresh.php on line 6
<?php
$this->db->select('*');
$this->db->from('forum_thread');
$this->db->join('useraccount','useraccount.user_id = forum_thread.user_id');
$post= $this->db->get();
foreach($post->result_array() as $row): ?>
<div class="panel panel-default" >
<div class="panel-body">
<p>
<small>Clinton Puds</small>
<small style="float:right;color:#808080;font-size:10px;"><b>Posted Last</b> <?php echo $row['date_last_post']?></small>
</p>
<p><?php echo $row['slug'];?></p>
</div>
<div class="panel-footer"><small>Comment</small></div>
</div>
<?php endforeach ?>

Try this:
$post = $this->db->get()->result_array();
foreach($post as $row): //etc

Try this :
$post->result() as $row

Related

Warning: Invalid argument supplied for foreach()

I have an error in the product detail, I do not know what else to change where again. when I change $detail into $categories succeed but is the same as function submenu. It was not according to my wishes
My View
<!-- navbar -->
<div class="row">
<?php foreach ($categories as $row) : ?>
<div class="col-sm-3">
<h5><?php echo $row->categoriesName; ?></h5>
<ul><li><a href="<?php echo base_url();?>member/detail">
<?php echo $row->productName; ?></a> </li></ul>
</div>
<?php endforeach; ?>
</div>
<!-- product detail -->
<?php foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; ?>
My Controller
public function home() {
$data=array('title'=>'Pasar Online | Ayo Belanja Sekarang',
'username'=> $this->session->userdata('username'),
'product' => $this->product_model->all(),
'categories' => $this->categories_model->get_main(),
'isi' =>'member/index');
$this->load->view('template_home/wrapper',$data);
}
public function detail() {
$data['username'] = $this->session->userdata('username');
$data['categories'] = $this->categories_model->get_main();
$data['detail'] = $this->categories_model->get_details();
$this->load->view('member/coba',$data);
}
My Model
public function get_main(){
$this->db->select('product.*,categories.*');
$this->db->from('product');
$this->db->join('categories','product.categoriesID=categories.categoriesID');
$this->db->limit(5);
$query = $this->db->get();
if($query->num_rows() > 0) {
$results = $query->result();
} return $results;
}
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
}
still can not display a single product
I do not know where my mistake... please help me, thanks
you are iterating details array in wrong format.
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
This will return only single row without any indexing like - 0,1,2...
and on view page you are using foreach loop and foreach loop use indexing to iterate value you can iterate like that --
<div class="box">
<h1 class="text-center"><?php echo $detail->productName; ?></h1>
<p class="price"><?php echo $detail->price; ?></p>
</div>
please use this way hope it will work..
In codeigniter row() selects only first row of your result in object format.So no need to use foreach loop.Just try like this...
<div class="box">
<h1 class="text-center"><?php echo $detail->productName;?></h1>
<p class="price"><?php echo $detail->price;?></p>
</div>
And
In model
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
it is because of $detail variable is empty, no data available in this variable, always check for data in variable before looping
<?php if(!empty($detail)){
foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; } ?>

Message: Undefined property: stdClass in codeigniter

I got this error:
A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$positionsNum
when i run.
My model:
public function load_job($id){
$Where = array('jobs.id' => $id);
$this->db->select('jobs.id, positionsNum, companyName, contract_type.titleEN as contractType');
$this->db->from('jobs');
$this->db->join('contract_type', 'jobs.contractTypeId = contract_type.id', 'left');
$this->db->where($Where);
$Result = $this->db->get();
return $Result->first_row() ;
}
my view:
<?php if(isset($job) && count($job) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($job->contractType)) echo $job->contractType; ?></div>
</div>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('num_vacancies'); ?> </div>
<div class="col-lg-6"><?php echo $job->positionsNum; ?></div>
</div>
<?php } ?>
my Controller's function:
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$job = $this->job_model->load_job($this->GetLang, $job_id);
$data['job'] = $job;
$this->load->view( 'front/singleJob', $data);
}
}
Also, i printed the returned object and it returns this:
stdClass Object(
[id] => 9
[positionsNum] => 2
[companyName] =>
[contractType] => type1
)
But, contractType won't be printed in page even if it is already has a value.
I can't figure out the problem. Any help?
The problem is here.
you have a function with one parameter.
public function load_job($id);
but in controller you are passing two values.
Try to pass correct values.
$job = $this->job_model->load_job($this->GetLang, $job_id);
it should be like this.
$job = $this->job_model->load_job( $job_id);
try like this
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$data['job'] = $this->job_model->load_job($this->GetLang, $job_id);
$this->load->view( 'front/singleJob', $data);
}
}
So, it is solved now :)
This problem happened twice in two files:
First is which stated in my question, and I had an error in another query at the same page.
Second problem was in the HTML file. My complete view was like this:
<?php if(isset($service) && count($service) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($service->contractType)) echo $service->contractType; ?></div>
</div>
<?php } ?>
<ul>
<?php foreach ($similarServices as $service) { ?> <<========= NOTICE: $service here is the same as the above condition
<li>
<div class="title"><?php echo $service->title; ?>
</div>
</li>
<?php }//end foreach similarServices ?>
</ul>
It appeared because i used the same variable name $service, so i changed it and it works now. Hope this helps you.

PHP Error: Trying to get property of non-object

I am working in CodeIgniter framework and I have tried to apply all the other solutions I found on stack but I could not make it work so here is my problem...
I am trying to retrieve a record from MySQL database table called 'questions' based on uri segment. Then I am trying to display this record in a view. As far as I can tell, the uri segment is passed along everywhere it needs to be, and record is retrieved from database.
The problem comes up when I am trying to access the data from the controller, in my view.
Error I am getting for each echo in my loop in 'view_thread_view' is
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/view_thread_view.php
Any solutions would be greatly appreciated.
Here is my code:
Controller thread
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
Model data_model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , 'qid' , $quest));
{
return $q;
}
}
View view_thread_view
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
</div>
EDIT: QUESTION ANSWERED
Thanks to Girish Jangid fixed the problem this is the working code:
Controller
function view_thread()
{
$quest = $this->uri->segment(3);
echo $quest;// for checking if uri segment is passed
//$data = array();
if($query = $this->data_model->get_thread($quest))
{
$data['records'] = $query;
}
$this->load->view('view_thread_view', $data);
}
Model
public function get_thread($quest)
{
if($q = $this->db->get_where('questions' , array('qid' => $quest)));
{
return $q;
}
}
View
<div title="question_view">
<h1>Question View<h1>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $data->uname
?>
</div>
<?php endforeach; ?>
</div>
</div>
You should user db function to fetch results, please use CodeIgniter db Query Results functions, like this
if($records->num_rows() > 0){
foreach($records->result() as $data){
if(isset($data->title)) echo $data->title;
}
}
For more detail please read CodeIgniter Query Result function
Query Results
Try this code
<div title="question_view">
<h1>Question View<h1>
<?php if($records->num_rows() > 0) { ?>
<?php foreach($records->result() as $data) : ?>
<div>
<h2>Title</h2>
<?php
echo $data->title;
?>
</div>
<div>
<h2>Question</h2>
<?php
echo $data->contents
?>
</div>
<div>
<h2>Tags</h2>
<?php
echo $data->tags
?>
</div>
<div>
Thread owner
<?php
echo $records->uname
?>
</div>
<?php endforeach; ?>
<?php } ?>
</div>
$records is an array, not an object, therefore you cannot do $records->uname. You probably meant $data there too.
Edit: On second look, it appears $data is an array as well! Arrays are accessed via ['key'] not ->key
Also, your code is way over complicated.
<?php
foreach($records as $data){
$string = <<<STR
<div>
<h2>Title</h2>
{$data['title']}
</div>
...etc
STR;
echo $string;
}
http://www.php.net/manual/en/language.types.string.php

Trying to get property of non-object in Codeigniter?

My controller code just generate ten copies of the same data. And I put the data in an array of array.Controller Code Below:
for($i=0;$i < $this->per_page;$i++)
{
$temp['title'] = "test";
$temp['link'] = "localhost/cib/index.php";
$temp['month'] = '4';
$temp['day'] = '21';
$temp['authorlink'] = "localhost/cib/index.php/author";
$temp['author'] = "Charles";
$temp['content'] = "tetestersafaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$results[] = $temp;
}
$data['main_content'] = 'report_list';
$data['posts'] = $results;
$this->load->view('include/templates', $data);
And I show the data generated by controller in the view page report_list, the code of report_list is
<div id="bg_top">
</div>
<div class = "container" id="content">
<div class="row-fluid">
<div class="span9" id="content_left">
<h2>解题报告列表</h2>
<link href="<?php echo base_url('assets/css/style.css') ?>" rel="stylesheet">
<?php if(!empty($posts) && is_array($posts)):?>
<?php foreach($posts as $post):?>
<div class="entry">
<h2 class="entrytitle"><?php echo anchor($post->link, $post->title)?><span></span></h2>
<div class="entrymeta1">
<div class="meta-date">
<span class="meta-month"><?php echo $post->month?></span>
<span class="meta-day"><?php echo $post->day?></span>
</div>
<span class="meta-author"><?php echo anchor($post->authorlink, $post->author)?></span>
<span class="meta-category"></span>
<span class="meta-comment"></span>
<div class="clear"></div>
</div><!-- [entrymeta1] -->
<div class="entrybody">
<p><?php echo $post->content;?><?php echo anchor($post->link, '阅读全文')?></p>
</div>
<div class="entrymeta3">
</div>
</div><!-- [entry] -->
<?php endforeach;?>
<?php endif;?>
</div>
<div class="span3">
<?php echo "hello world2";?>
</div>
</div>
</div>
The question is where am wrong? why is there errors "Trying to get property of non-object"?
I tried to print the data in the controller, it works quite well.
foreach($results as $post)
{
foreach($post as $key=>$value)
{
echo $key.':'.$value;
echo '<br/>';
}
}
You need to print the variables in the view file in array style like this:
<?php echo anchor($post['link'], $post['title'])?>
Still you can always print the array to view the structure:
<?php echo "<pre>";print_r($posts);echo "</pre>";
My assumption is that
You are expecting $posts as array of objects. But in $posts in some loaction there is no object with property month, day etc. So just before the for loop you should look the type of $posts by
var_dump($posts)
This will give you the clear picture about your error
Change all the variables $post->link to array $post['link']
$post->month
$post->day
every fields that are using in template.

Twitter OAuth - Get values from Array

I'm attempting to get a list of users who searched a term on Twitter and so far no luck. Here's my code:
$parameters = array('q' => '#puppy', 'count' => 2);
$results = $connection->get('search/tweets', $parameters);
//print_r($result);
foreach ($results as $data) {
?>
<div id="update">
<div id="left"><a href="https://twitter.com/#!/<?php echo $data->user->screen_name; ?>" target="_blank"/><img width="48px" height="48px" src="<?php echo $data->user->profile_image_url; ?>"/></a></div>
<div id="right">
<div class="user"><a href="https://twitter.com/#!/<?php echo $data->user->screen_name; ?>" target="_blank"/><?php echo $data->user->screen_name; ?></a> <?php echo $data->user->name; ?></div>
<div class="detail">
<?php echo $data->text; ?>
</div>
</div>
</div>
<?php
}
and this is what it outputs:
Notice: Undefined property: stdClass::$user in line 150
Trying to get property of non-object in line 150
I know I'm accessing that array in a weird way, I just can't figure out how it needs to be.
For anyone out there who may be needing this, it was just a matter of getting the right data from the array in the right way. Here's the code I used:
$parameters = array('q' => '#puppy', 'count' => 2);
$results = $connection->get('search/tweets', $parameters);
$resultsuser = $results->statuses[0]->user;
$i = 0;
foreach ($results->statuses as $data) {
$user_screen_name = $data->user->screen_name;
$user_profile_image_url = $data->user->profile_image_url;
$user_text = $data->text;
?>
<div id="update">
<div id="left"><a href="https://twitter.com/#!/<?php echo $user_screen_name; ?>" target="_blank"/><img width="48px" height="48px" src="<?php echo $user_profile_image_url; ?>"/></a></div>
<div id="right">
<div class="user"><a href="https://twitter.com/#!/<?php echo $user_screen_name; ?>" target="_blank"/><?php echo $user_screen_name; ?></a> <?php echo $user_screen_name; ?></div>
<div class="detail">
<?php echo $user_text; ?>
<hr/>
</div>
</div>
</div>
<?php
$i++;
}
?>

Categories