Xdega Posted August 5, 2011 Share Posted August 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/ Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252306 Share on other sites More sharing options...
trq Posted August 5, 2011 Share Posted August 5, 2011 There already exists classes built into php for doing this. See mysqli or pdo. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252310 Share on other sites More sharing options...
Xdega Posted August 5, 2011 Author Share Posted August 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252316 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Trust me XDega, if you want to know the importance of the OOP paradigm, then do a little bit of C++ or Python. I didn't understand it for awhile until I started programming for desktop applications. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252317 Share on other sites More sharing options...
Xdega Posted August 5, 2011 Author Share Posted August 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252318 Share on other sites More sharing options...
trq Posted August 5, 2011 Share Posted August 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252323 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/ Why did they even bother with OOP in this tutorial? It seems that its just wrapped around a class and everything else is no different than procedural coding. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252325 Share on other sites More sharing options...
Xdega Posted August 5, 2011 Author Share Posted August 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252333 Share on other sites More sharing options...
trq Posted August 5, 2011 Share Posted August 5, 2011 I read that same book about 3 times. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252347 Share on other sites More sharing options...
Xdega Posted August 5, 2011 Author Share Posted August 5, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252806 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Xdega, what tutorial/book are you referring to? Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252809 Share on other sites More sharing options...
Xdega Posted August 5, 2011 Author Share Posted August 5, 2011 http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804 I guess it was one of the top books recommended to me by several sources. I have learn't a TON from it so far. Although It's a somewhat slow read, as I find it a little hefty to digest. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1252816 Share on other sites More sharing options...
Xdega Posted August 8, 2011 Author Share Posted August 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243884-oo-php-trying-to-printdump-a-database-table/#findComment-1254397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.