1

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>
2
  • What is this returning? $this->data_model->get_thread($quest) Commented Jan 10, 2014 at 20:30
  • now that the code is working it is returning a record from the database where 'qid' matches $quest. And $quest contains uri segment(3) Commented Jan 10, 2014 at 20:38

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

6 Comments

You do not have to use Active Record functions, you can execute raw queries, which most times is slightly faster than using ActiveRecord.
You are right, but he have not used result function to fetch records
This actually seem to work, thank you. However, there is the problem with my db query... it must return no results as nothing is displayed in the view. any ideas?
@Thomas Please try this code in model $this->db->get_where('questions' , array('qid' => $quest))
Would love to vote up but not enough rep :( any ideas about my db problem mentioned above?
|
-1

$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

8 Comments

Thx for quick answer, made the code adjustment and I have a new error A PHP Error was encountered Severity: Notice Message: Undefined index: title Filename: views/view_thread_view.php
Try doing var_dump($data); in your foreach loop and post the results in your question.
Answer is wrong, you should use CodeIgniter Query Results functions to retch records. [link] (ellislab.com/codeigniter/user-guide/database/…)
Ok I see that there is another problem... the query returns this resource(30) of type (mysql link persistent) resource(37) of type (mysql result) array(0) { } array(0) { } array(0) { } int(0) int(0) NULL
So if it is an object and not an array, then use ->key. My answer was that $records is an array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.