Welcome Guest, Not a member yet? Register   Sign In
Messing Arround With CI
#1

[eluser]Rost[/eluser]
I am messing around with CI, but stood on a problem.

When I am trying to echo my sitetitle I get a error:
Quote:<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: views/index.php</p>
<p>Line Number: 4</p>

This line is:
Code:
&lt;title&gt;&lt;?=$s->f_sitetitle; ?&gt;&lt;/title&gt;

The Model's code is:
Code:
function getSettings(){
     $q = $this->db->get('tbl_settings');
     if($q->num_rows() > 0){
     foreach($q->result() as $row)
        {
         $data[] = $row;
        }
        return $data;
     }
    
    }

The Controllers code is:
Code:
function index()
     {
      $this->load->model('data_news');
      $this->load->model('data_overall');

          $data['news'] = $this->data_news->getNews();
          $data['s'] = $this->data_overall->getSettings();
        
      $this->load->view('index', $data);
     }

I can't solve it, trying the whole morning. I hope you can help me
#2

[eluser]garymardell[/eluser]
You are returning the results as an array but using it like a object.

Use this:

Code:
function getSettings(){
     $q = $this->db->get('tbl_settings');
     return $q->result();
    
    }

That should make your code work as it is.
You should also note that i believe it good practice to return the whole get() object back if you are getting results.

Code:
function getSettings(){
     return $this->db->get('tbl_settings');    
    }

Code:
function index()
     {
      $this->load->model('data_news');
      $this->load->model('data_overall');

          $data['news'] = $this->data_news->getNews();
          $data['s'] = $this->data_overall->getSettings();
        
      $this->load->view('index', $data);
     }

then in the view you can do:

Code:
&lt;? if($s->num_rows() > 0): ?&gt;
    &lt;? foreach($s->result() as $row): ?&gt;
        &lt;title&gt;&lt;?=$row->f_sitetitle;?&gt;&lt;/title&gt;
    &lt;? endforeach; ?&gt;
&lt;? endif; ?&gt;

This way your view has access to the num rows function which allows you to display items such as "you do not have any posts" or else display the posts.
#3

[eluser]Dam1an[/eluser]
I'm guessing $s is never set to anything as the qiery doesn't return anything? (aka doesn;t make it in the foreach loop)
Try doing var_dump on the variable to see what you get
#4

[eluser]Rost[/eluser]
When using var_dump($s) I am getting:
Code:
array(1) {
  [0]=>
  object(stdClass)#17 (4) {
    ["id"]=>
    string(1) "1"
    ["f_sitetitle"]=>
    string(10) "RobbertCMS"
    ["f_description"]=>
    string(80) "RobbertCMS:  This CMS is made by Robbert Stevens, with the CodeIgniter Framework"
    ["f_keywords"]=>
    string(74) "Robbert, Stevens, Robbert Stevens, RobbertCMS, Code Igniter, Code, Igniter"
  }
}

And @Gary, the code you provide me doesnt work. I am getting the error:
Quote:Fatal error: Call to a member function num_rows() on a non-object in /var/www/vhosts/rstevens.eu/httpdocs/system/application/views/index.php on line 5

EDIT: @Gary it works, but cant it be shorter?
#5

[eluser]Dam1an[/eluser]
the problem is you're returning an array of results, instead of a single row
If you only ever expect a single result, use ->row() instead
Otherwise, call it as follows
Code:
&lt;title&gt;&lt;?=$s[0]->f_sitetitle; ?&gt;&lt;/title&gt;

Notice you're calling the f_sitetitle variable on the first object in the array
#6

[eluser]Rost[/eluser]
It works thank you very much Smile I would never come with that idea.
#7

[eluser]Dam1an[/eluser]
You're welcome Smile
And if something ever isn;t what it seems, var_dump is your friend Smile




Theme © iAndrew 2016 - Forum software by © MyBB