tags:

views:

71

answers:

3

Im working on making a blog in oop php. Now Im trying to display the post entries in my db. I manage to output data from the created field but nothing else. Basically I have four fields in the post table in my db. I have created, author, title and body. With this code below I only manage to display created. Anyone got any clued to how I should proceed?! Do I need to create a new blog object for every field?! That seems so weird if so. Thanks! BTW the code is from my index.php...

<?php
require_once('_settings.config.php');
            global $db;
            $blog = new Blog("My Blog");
            $posts = $blog->getPosts(); ?>

               <?php foreach ($posts as $post): ?>
               <li>
               <?php echo "<div>"; ?>
                   <?php echo "<span class='footer'>Posted by: " . $post->author . "Created: " . $post->created . "</span>"; ?>
                  <?php echo "</div>"; ?>
               <?php endforeach; ?>  

Here as requested is my getPosts function. It returns the $posts array.

public function getPosts() {
        $result = $this->db->query("SELECT * FROM posts");
        $posts = array();
        while($post = $result->fetch_assoc()) {
            array_push($posts, new BlogPost($post['id'], $post['created'], $post['author'], $post['title'], $post['body']));      
        }    
        return $posts;        
    }                                                                                                                                       
A: 

There are some steps: 1. connect to the database 2. SELECT the data LIMIT number of posts on page 3. assign data to the view 4. foreach or so through them in the manner you want, including them in layout

You don't need to create new object for each post - just create one that will handle them all.

Tomasz Kowalczyk
@Tomasz, yes thats basically what I have done. I have not pasted the rest of the code. I create a db object. I assign data to the view and their I create a new blog object and try to display the data trough a foreach loop.
Tim
A: 

Does your post class have any additional functions? IMO, if it does, it makes sense to construct a new post object, so that way you could call $post->do_something() later on in the code. On the other hand, if your post class is just a container for the information that you pulled from the database, I would just create an instance variable $posts in your blog class that contains an array of hashes of all of the fields. Only real difference there is that you would have $post['author'] instead of $post->author You could also define a function display_all_posts() or display_posts(some_range) for your blog object to make your code look nicer.

dave
@Dave: In my post BlogPost class I have the constructor and getters and setters for the fields in the database. Then in my blogclass I have the getPosts function that does the query and creates the BlogPost object which contains the fields in the database. Thats as far as I have come until now.
Tim
A: 

is not clear what do you want to do, but you can try somenthing like this:

<?php
  class blog
  {
      public $title;
      public $body;
      public $author;
      public $date;
      public $bid;
      public function __construct($bid)
      {
          $this->bid = $bid;
          $sql = new db //assuming you have like this...
          $query = " SELECT * FROM blog WHERE blog_id = '{$this->bid}' LIMIT 1";
          $sql->db_select($query);
          while ($row = $sql->db_fetch()) {
              extract($row);
              $this->title = $blog_title;
              $this->body = $blog_body;
              $this->date = $blog_created;
              $this->author = $blog_author;
          }

          }

          public function title()
          {
              return $this->title;
          }
          public function author()
          {
              return $this->author;
          }
          public function body()
          {
              return $this->body;
          }
          public function date()
          {
              return date('Y/m/d', $this->date);
          }
      }
                      // yout code.......
      $posts = $blog->getPostsIds();   // get all posts ids

               foreach ($posts as $id) {
               $b = new blog($id);
                    echo $b->name() . "<br />";
                    echo $b->author() .  "<br />";
                    echo $b->date() .  "<br />";
                    echo $b->body();
               }
?>

not tested but should work as expected!

aSeptik
missing the closing brace for the constructor method. and I'm just curious, why use the php5 constructor method (__construct) and use the php4 way for declaring properties (var instead of defining scope)?
Jonathan Kuhn
tnx for the advice! ;)
aSeptik