Jump to content

Need Help With OOP Programming


gaza165

Recommended Posts

I have a function inside my Class called RetrieveBlog(), this retrieves the blog from the database what i want to do is be able to call the method without making a new instance ofthe class Blog.

 

class.php

<?php
session_start();

class Blog {		

	function __construct($blogtitle,$blogbody,$blogcreated,$createdby,$blogimg,$shortdescription) {		
		$this->blogtitle = $blogtitle;		
		$this->blogbody = $blogbody;					
		$this->blogcreated = $blogcreated;					
		$this->createdby = $createdby;
		$this->blogimg = $blogimg;
		$this->shortdescription = $shortdescription;			
	}	

	function CheckInput() {
		if(empty($this->blogtitle)) {	
		header("Location: index.php");
		$_SESSION['msg'] = "Please enter the title";
		} else if(empty($this->blogbody)) {
		header("Location: index.php");
		$_SESSION['msg'] = "Please enter the body";
		} else if(empty($this->createdby)) {
		header("Location: index.php");
		$_SESSION['msg'] = "Please enter your name";
		} else if(empty($this->blogimg)) {
		header("Location: index.php");
		$_SESSION['msg'] = "Please enter the Blog Image url";
		} else if(empty($this->shortdescription)) {
		header("Location: index.php");
		$_SESSION['msg'] = "Please enter the Short Description";
		} else { $this->InsertBlog(); }
	}

	function InsertBlog() {	  	
		$title = $this->blogtitle;
		$body = $this->blogbody;
		$created = $this->blogcreated;
		$createdby = $this->createdby;
		$blogimg = $this->blogimg;
		$shortdescription = $this->shortdescription;

		$sql = mysql_query("INSERT INTO blog(title,body,name,blog_img,short_description) VALUES 
		('$title','$body','$createdby','$blogimg','$shortdescription')");
		$_SESSION['msg'] = 'Blog Created';
		header("Location: index.php");
	}

	function retrieveBlog() {

	$sql = mysql_query("SELECT * FROM blog");

	while($row = mysql_fetch_array($sql))
	{
		echo $row['title'];
	}

	}



}


?>

 

getBlog.php

<?
include("class_lib.php");
include("dbconnect.php");

$blog = new Blog();
$blog->retrieveBlog();

?>

 

Because im making a new instance, its going to the constructr... all i want to do is call on a method inside my class from a seperate document.

Link to comment
https://forums.phpfreaks.com/topic/158119-need-help-with-oop-programming/
Share on other sites

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.