Welcome Guest, Not a member yet? Register   Sign In
Blog Video Tutorial - Is it still possible to complete?
#1

[eluser]Unknown[/eluser]
Hi there,

I've be running through both video tutorials for CI fine. I am now on tutorial 2, making the blog.

I am up the part where the entries table is made, and we have just used the scaffolding feature to enter two blog entries.

Now this is done, and we adjust our blog class to look like this:

Code:
class Blog extends Controller {
  
  function Blog()
  {
    parent::Controller();
  }
  
  function index()
  {    
    $data['title'] = "My Blog Title";
    $data['heading'] = "My Blog Heading";
    $data['query'] = $this->db->get('entries');
    
    $this->load->view('blog_view', $data);
  }
  
}

And our blog_view.php code to look like this:

Code:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>&lt;?php echo $heading; ?&gt;</h1>

  &lt;?php print_r($query); ?&gt;
  
  &lt;?php foreach($query as $row): ?&gt;
  
   <h3>&lt;?php echo $row->title; ?&gt;</h3>
   <p>&lt;?php echo $row->body; ?&gt;</p>
  
   <hr />
  
  &lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;

I have picked this code to pieces for about an hour and can find no problem. But when you actually view the blog, All I get is 14 of these errors:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/blog_view.php
Line Number: 12

If I do a print_r on $query in the view, I get this:

CI_DB_mysql_result Object ( [conn_id] => Resource id #28 [result_id] => Resource id #32 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 2 [row_data] => )

So nothing is being returned, and to be honest, looking at that output, I can't see how the turtorial could actually work anyway.

Any ideas? Is this tute just outdated and using some redundant stuff?

Cheers
Aaron
#2

[eluser]Dan Bowling[/eluser]
The fact that it is saying that the row isn't an object probably means that you don't have a result returned.

Don't forget that you need to initialize and specify the model that your query applies to.
#3

[eluser]fatnic[/eluser]
Should it not be
Code:
&lt;?php foreach($query->result() as $row): ?&gt;
#4

[eluser]Unknown[/eluser]
[quote author="fatnic" date="1219696240"]Should it not be
Code:
&lt;?php foreach($query->result() as $row): ?&gt;
[/quote]

Well done fatnic. That's it alright.

Cheers
Aaron




Theme © iAndrew 2016 - Forum software by © MyBB