Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.