Jump to content

OO PHP Trying to print/dump a database table.


Xdega

Recommended Posts

OK. So I have been working with some basic OO PHP. I am on a crusade to create a very basic blog script as I work on getting comfortable with Object Oriented PHP. I got some basics down, built some database config structure etc. Now I am working on working with database data.

 

I have a table with one record, and I want to print the contents of that row as I work on learning how to create an array to pull my "blog posts". At this point I am simply looking to grab something from the db and print it, but I have hit a little bit of a blank at this point.

 

Could anyone point me in the right direction on how to use an Object Oriented Approach to pull data from a database and then return the values (I know how to print the values once returned).

 

I have my Database Config all working and connecting. Here is my table structure :

table:kdb_posts

postid: int, auto inc

title: varchar

content: varchar

created: datetime

user: varchar

categories: int

 

 

Here is my Class:

The part I need help with is the "DUMP DATABASE" section at the top.

<?php
//connect to mySQL and select database
include "includes/_dbcon.inc.php";

//begin class
class kdblogpost{

//define properties and set some arbitrary default values
PUBLIC $titledata= "POST TITLE";
PUBLIC $bodydata= "POST BODY <br /> XXXXXXXXX";
PUBLIC $p_by="POSTED BY: XXX"; 
PUBLIC $p_realname=" (XXX XXX)";
PUBLIC $p_date=" ON: XXX"; 

///////////////////////////////////////////
//DUMP DATABASE///////////////////////////
PUBLIC $postArray = array();
PUBLIC $query("SELECT * FROM kdb_posts");

function getpostdb()  
{
  	
return "{$postArray}";  
}
////////////////////////////////////////////

// write the blog post title
function writetitle()
	{
	return "{$this->titledata}";
	}

// write the blog post body.
function writebody()
	{
	return "{$this->bodydata}";
	}

// write the blog post info
function writeinfo()
	{
	return  "{$this->p_by}".
		"{$this->p_realname}".
		"{$this->p_date}";
	}
}
//end class

 

 

Here is my html markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>KenDega Blog. Beta</title>
<?php include "KD_LIB/_classloader.php"; ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<!-- print/dump blog post database-->
<div class="dbdump">
	<p><?php $dbdump= new kdblogpost(); print "{???????????????????????}"; ?></p>
</div>

<div class="blogpost">
	<h1><?php $ptitle= new kdblogpost(); print "{$ptitle->writetitle()}"; ?></h1>
	<div class="post"><p>><?php $pbody= new kdblogpost(); print "{$pbody->writebody()}"; ?></p></div>
	<div class="postinfo"><p><?php $pinfo= new kdblogpost(); print "{$pinfo->writeinfo()}"; ?></p></div>
</div>
</body>
</html>

 

 

I don't want to get too involved with intricacies (such as my poor approach of making all of my properties PUBLIC). I would just simply like a finger in the right direction with how to get basic information from my database fields using an object oriented PHP approach, with as little code as possible.

 

Thank you much ,

Xdega.

Link to comment
Share on other sites

Not tested, and take note this is a simple version of the class, i havn't implemented a while loop or anything, so it only grabs 1 row at a time. I also did this so you can put your skills to the test and figure it out.

 

<?php
class testClass{

  public $sql;
  public $row;

  function setQuery($query){

 	 $this->sql = mysql_query($query);

		if($this->sql){

			 return true;

		}else{

		     return false;

		}

  }
  
  function fetchQuery(){
  
  	$this->row = mysql_fetch_array($this->sql);

  }


}

$db = new testClass();
$query = "SELECT * FROM `table`";

     if($db->setQuery($query)){
         $db->fetchQuery();
         print $db->row['someColumn'];
     }else{
        // not a valid sql
     }

?>

Link to comment
Share on other sites

well thanks guys. That should give me something to chew on. I am still working on my book "PHP Objects, patterns, and practice". So between the book, and your code snippets I should be able to nail it soon.

 

Honestly, I have an understanding of the basics of OO Php. But yet to see how it gets much easier (so I've heard). Seems to just get more and more complex. Although I admire the approach.

Link to comment
Share on other sites

Well I did recently play around with some basics in C++, but didn't delve in to Classes or Structs yet, now started back on some OO PHP because I want to tackle my burning desire to build a Blog Script from the ground up.

 

....But luck has it that I am heading back to college in about 2 weeks to begin my Computer Science degree. Although we start off with C++ in "Programming 1" but it is all procedural. I wont get in to any OO programming classes until "Programming 2" where we cover Java, which I hear is great for learning OO programming?

 

In the meantime, I am working on some web based projects, really wanting to nail some basic OO PHP, as I may be getting a little web development/design work during my college.

 

I do also want to look at some frameworks. Have been interested in SF2 for quite some time, but feel I should have a good understanding of OO PHP before I tackle that.

 

One thing to also note, I am using online tutorial resources to assist. However, the only "OO PHP Blog Tutorial" I was able to find was this: http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/

I am following it extremely loosely as it doesn't appear to be a very good tutorial and I am setting my blog up and structuring it a little differently, as I have my own ideas. In fact I haven't really followed that tutorial at all, besides looking at the code a little.

 

 

Link to comment
Share on other sites

That tutorial is indeed a VERY bad example of OOP. While it might use a few classes, the entire approach isn't OOP at all.

 

The book you said you have 'Objects, Patterns & Practice' is probably the best resource getting around.

Link to comment
Share on other sites

Well that makes me feel better, knowing that I was able to diagnose a terrible tutorial of OOP. I think it is probably due to the fact I am reading that great book and seeing that the approach doesn't coincide with what I have been learning. lol

 

I do find it hard to tackle some of the topics (takes me a bit of re-reading/staring). But I find it is VERY rich in information and confident that I will have a nice foundation by the time I read (and understand) that book. I am just really dieing to do something practical as I study :D 

Link to comment
Share on other sites

Ok. I have been looking at this briefly. One thing I immediately notice, is that the "client code" portion has more code than I am used to.

Is this normal, or for the sake of illustration? Could/should/would I move some of that code in to the class itself?

 

I was under the understanding that very little code is meant to be used in the markup. Mainly just instantiating the class with/withour some paramaters?

 

 

Link to comment
Share on other sites

so basically, I am really starting to understand the basics of oo php . Now I am finding I have a weakness in the design of the code. How much code to put in the hands if the client code and how much work to let the classes do. I initially had the idea that you let the classes to ALL of the work, while the while the client code simply creates the object.

I shamefully skipped to chapter 6 of my book, that deals with oo design.

 

Link to comment
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.