Jump to content

Working with OOP outputting dynamic content?


Mr Chris

Recommended Posts

Hello,

 

I've kind of mastered the basics of working with OOP with regards to 'static' content thats not database driven, but now I want to make it database driven.  So take this example below where I output a headline, picture and Opening Paragraph within a CSS box.

 

Click here to view the example

 

...and here's the code it's made up of

 

?php 

Class NewsBox { 

private $cssbox;
private $headline;
private $image;
private $opening_paragraph;

function __construct($cssbox) { 
  $this->cssbox = $cssbox;
} 

function setData($headline,$image,$opening_paragraph) { 
  $this->headline = $headline;
  $this->image = $image;
  $this->opening_paragraph = $opening_paragraph;
} 

function getData() {
  $output = '<div class="'.$this->cssbox.'">
               <h3>'.$this->headline.'</h3>
               <img style="float:right" src="'.$this->image.'">
               <p>'.$this->opening_paragraph.'</p>
   </div>';
   return $output;
}

} 

$box = new NewsBox("wrapper"); 
$box->setData("Headline","image.jpg","This is the Opening Paragraph");  //The Headline, the image and the opening paragraph
echo $box->getData(); 
?>

 

...and that's fine, however, what i'm really struggling to comprehend is using OOP to grab content dynamically?

 

Say for example I wanted another three css boxes on that page with the latest headline, picture and Opening Paragraph in them called from a database which is in exactly the same format as my static data like so.

 

database.jpg

 

How would I go about doing that dynamically with the example above?

 

Thanks

 

 

class ParagraphDAO{
    private $database=null;
    public function __construct($database){ $this->database=$database; }
    
    public function findById($id){
        $id=$this->database->real_escape_string($id);
        $query="SELECT * FROM table WHERE id=$id";
        $result=$this->database->query($query);
        return 0!==$result->num_rows()?$result->fetch_object('Paragraph'):null;
    }
};

class NewsBox extends HtmlModule{
    private $paragraph=null;
    public function __construct(Paragraph $p){
        $this->paragraph=$p;
    }
    
    public function render(){
        return '<div class="">'.
               '<h3>'.$this->paragraph->getHeadline().'</h3>'.
               '<img src="http://domain/images/'.$this->paragraph->getImage()->getFilename().'"'.
               '     alt="" style="float:right">'.
               '<p>'.$this->paragraph->getOpening().'</p>'.
               '</div>';
    }
};

class Paragraph{
    private $id=0;
    private $headline='';
    private $image=null;
    private $opening='';
    
    public function __construct($id,$headline,$image,$opening){
        $this->setId($id);
        $this->setHeadline($headline);
        $this->setImage($image);
        $this->setOpening($opening);
    }
    
    public function getId(){ return $this->id; }
    public function setId($id){ $this->id=$id; }
    
    public function getHeadline(){ return $this->headline; }
    public function setHeadline($headline){ $this->headline=$headline; }
    
    public function getImage(){ return $this->image; }
    public function setImage($image){
        if(file_exists($image))
            $this->image=new SplFileInfo($image);
    }
    
    public function getOpening(){ return $this->opening; }
    public function setOpening($opening){ $this->opening=$opening; }
};

 

Use as:

 

$mysqli=new mysqli(..);
$dao=new ParagraphDao($mysqli);
$paragraph=$dao->findById(1);

$nb=new NewsBox($paragraph);
echo $nb->render();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.