tma general guides blogs

Upload: reham-alsabbagh

Post on 04-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 TMA General Guides Blogs

    1/15

  • 8/13/2019 TMA General Guides Blogs

    2/15

    something like blog_post_tags that will hold two foreign keys, one will be the blog post s IDand the other will be the tag ID that the blog post is associated with. This way we can assign asmany tags as we want to a blog post but can still edit the information about that specific tagacross all posts with a simple MySQL query.

    Now that we have outlined what we want our database to look like, lets create it. Ill be usingPhpMyAdmin since that is the most widely used MySQL admin client. There are a few differentnaming conventions you can use when creating your database, table, and field names. Ipersonally like to use all lowercase and underscores in place of spaces.

    Note: If you do not have PHP and MySQL on your system or a server that can run it, Irecommend downloading a stand-alone install of Apache, PHP, and MySQL.MAMPis good forMacs andWAMPis good for PCs.

    First we need to create our database, Im going to call it nettuts_blog.

    Next, we will create our tables; the first will be blog_posts.

    blog_posts will have five fields: id, title, post, author_id, and date_posted. For idwe are going to make it the primary key and set it to auto-increment. What this will do isgenerate our unique id for us. Each time we add a post it will give it a number starting at one andmoving up for as many posts as we have.

    Now we also need to set the variable type for each field. The id will be set to type int, short for

    integer, since it can only be a number and we will set the max length to 11. The field title willbe set to type varchar with a max length of 255. The post field will be type text and we willnot set a max length since posts can be very long. author_id will be the same as id but we

    will not set it as our primary key or have it auto increment, and we will set date_posted to typeDate.

    http://www.mamp.info/en/index.phphttp://www.mamp.info/en/index.phphttp://www.mamp.info/en/index.phphttp://www.wampserver.com/en/http://www.wampserver.com/en/http://www.wampserver.com/en/http://www.wampserver.com/en/http://www.mamp.info/en/index.php
  • 8/13/2019 TMA General Guides Blogs

    3/15

    Our next table will be people. We are not calling it authors because down the road we mightwant to create the ability to register and post comments and those people would not beconsidered authors.

    people will contain five fields also: id, first_name, last_name, url, and email.

    id will be set as an int, the primary key, and to auto increment, the same way we set id of

    blog_posts. first_name, last_name, url, and email will all be set to type varchar with amax length of 255.

    Our next table will be tags and will for now contain only two fields: id and name. Downthe road we could make this more complex by adding a description but for this tutorial, we willnot. As we did before id will be set as int, the primary key, and to auto increment. name will

    be type varchar and have a max length of 255.

    And our last table, blog_post_tags, will have only two fields: blog_post_id and tag_id.

    They will both be set to type int with a max length of 11. As you most likely noticed, we did notset a primary key for this table. This is because we will never be getting data out of this tableunless we are asking for a specific blog post or all posts of a specific tag id.

    Part 2) Creating our Objects in PHP

    Before we start into our actual PHP code we need to create our files and folders, for this tutorialwe will have our index.php in our root folder then an includes folder that will hold our CSS stylesheet, our JavaScript files, includes.php that will hold references to our objects and MySQLconnection, and blogpost.php that will hold our BlogPost obejct.

    Now that we have our database set, we need to create the objects in PHP that will handle the datafor us. Objects in programming are a way to pull together different attributes (as variables) andmethods that all relate to the same thing. Objects also help us organize our programs much more.Before we jump into our objects for our blog, letsjust create a simple object so that we can

    illustrate what they are in a more real life term.

    Our object will be called Bike, now there are two types of things each object has, properties

    and methods. Properties define the object and methods are what the object does. For example

    our Bike object would have properties like wheel size, number of gears, and perhaps the frame

    size. For methods we might have something like Pedal.

  • 8/13/2019 TMA General Guides Blogs

    4/15

    Now to move back to our blog, we only need one object for now called BlogPost. BlogPost

    will have six properties, id, title, post, author, tags, and date posted. So lets make it in PHP.

    To define an object in PHP we define it as a class. A class is the structure of each object, or as

    wikipedia describes it, In object-oriented programming, a class is a programming language

    construct that is used as a blueprint to create objects. This blueprint includes attributes andmethods that the created objects all share. (http://en.wikipedia.org/wiki/Concrete_class). We aregoing to open up our blogpost.php page and define our first object.

    Note: In each section of the tutorial, Im going to leave out the opening and closing PHP tags;

    you will need to include them at the start and end of your document.

    class BlogPost

    {

    }

    In our class we need first define our properties. To do this, we have to create variablesbut with

    public in front of them. Just a quick note, if you are using PHP4 then you will need to usevar instead of public.

    view plaincopy to clipboardprint?

    1. class BlogPost2. {3. public $id;4. public $title;5. public $post;6. public $author;7. public $tags;8. public $datePosted;9. }

    Now that we have all our properties defined, we want to define our first method. Methods arealso described as functions, but the main difference is that a method is a function inside anobject. So all methods are also functions but not all functions are methods.

    Our first method will be what is called a constructor; this method is automatically calledwhenever we make a new instance of the BlogPost object.

    The common use of a constructor is so that you can create a new object and set the properties ofthat object quickly.

    So what we want to do is create a new function called __construct() and we are going to pass infive values, id, title, post, author id, and date posted. For each variable name we are going to putin before the word so we can tell inside our functions what variables are being passed in andwhat variables are already present.

    http://en.wikipedia.org/wiki/Concrete_classhttp://en.wikipedia.org/wiki/Concrete_classhttp://en.wikipedia.org/wiki/Concrete_classhttp://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://en.wikipedia.org/wiki/Concrete_class
  • 8/13/2019 TMA General Guides Blogs

    5/15

    view plaincopy to clipboardprint?

    1. class BlogPost2. {3. public $id;4.

    public $title;5. public $post;

    6. public $author;7. public $tags;8. public $datePosted;9.10. function __construct($inId, $inTitle, $inPost, $inAuthorId, $inDatePosted)11. {12.13. }14. }

    The problem here is that, with this current code, every time we create a new instance of BlogPostwe need to supply all those properties. But what if we want to make a new blog post and havenot defined those variables yet? To handle this, we need to overload the arguments for our

    function so that if we call the function and dont pass in one of the arguments, it will

    automatically set it to the default value.

    view plaincopy to clipboardprint?

    1. function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)

    2.

    {3. }As you can see, all that we do to accomplish our task is set each argument to the value null.Now inside our constructor, we need to set each of our variables to our passed in values. To dothis we want to set them to the object we are in right now; We can do this with the this

    keyword. Unlike many other languages to access a property in PHP you use -> where in mostlanguages (I.E. JavaScript, ASP.NET) you use ..

    view plaincopy to clipboardprint?

    1.

    function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)2. {3. $this->id = $inId;4. $this->title = $inTitle;5. $this->post = $inPost;6. }

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    6/15

    This works for id, title, and post. But what about our other ones? For date we are going to needto reformat the data we got from MySQL to be more readable. Thats easily accomplished; We

    just explode it (also known as splitting in other programming languages) and then put it backtogether. MySQL gives it to us in this format yyyy-mm-dd, so if we explode it using - as ourseparator we will get an array holding three values. The first will hold our year, the next will

    hold our month, and the last will be the day. Now all we do is put them back together inwhatever format we want. I am going with mm/dd/yyyy.

    view plaincopy to clipboardprint?

    1. $splitDate = explode("-", $inDatePosted);2. $this->datePosted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];

    For the author, all we have to do is ask the database for the person with the id of our author ID.We can do this with a basic MySQL query.

    view plaincopy to clipboardprint?

    1. $query = mysql_query("SELECT first_name, last_name FROM People WHERE id = " .$inAuthorId);

    2. $row = mysql_fetch_assoc($query);3. $this->author = $row["first_name"] . " " . $row["last_name"];

    Left Join

    Now the tags will be slightly more difficult. We are going to need to talk to the database, so weneed to create a MySQL query. We wont worry about the database connection right now, that

    will be defined outside of this class. Now all we have is the blog posts ID. We can compare thatto the blog posts tags in our blog_post_tags table but then we will on ly get back the tags ID andwill need to do another query to get the information about the tag. Thats no good; We want to beefficient so lets do it in just one query!

    To do this we are going to do what is called a left join, this means we are going to also selectdata from another table but only when it matches the data from the left or our other selected

    data. So first lets just get all tag IDs that are associated with our blog posts ID in the

    blog_post_tags table.

    view plaincopy to clipboardprint?

    1. $query = mysql_query("SELECT * FROM blog_post_tags WHERE blog_post_tags.blog_post_id = " . $inId);

    Now lets add our left join and tell our query we only want the data in the tags table:

    view plaincopy to clipboardprint?

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    7/15

    1. $query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);

    So now the query is selecting all from the tags and blog_posts_tags tables where, firstblog_post_tags.blog_post_id equals the one we provided and then also returns the information

    about each tag that has that tag_id that is in the same row of data as our blog_post_id.

    Now we want to process that data in PHP with a simple while loop. We are going to also createtwo arrays that will hold our data: one for the tag name, and the other for the tag id. We will alsomake a string to hold all of our tags. We first will set it to No Tags so that if we return no data

    from our MySQL query, it will return that we have no tags, otherwise that value will beoverwritten with the tag names.

    view plaincopy to clipboardprint?

    1. $postTags = "No Tags";2.

    $tagArray = array();3. $tagIDArray = array();

    4. while($row = mysql_fetch_assoc($query)5. {6. array_push($tagArray, $row["name"]);7. array_push($tagIDArray, $row["id"]);8. }

    Now we will check to make sure if the array has a length greater then zero (we dont want toperform all of this extra code if we dont have to). Next, for each tag in our tag name array, we

    are going to concatenate a string of tags. We will use a simple if else statement.

    view plaincopy to clipboardprint?

    1. if (sizeof($tagArray) > 0)2. {3. foreach ($tagArray as $tag)4. {5. if ($postTags == "No Tags")6. {7. $postTags = $tag;8. }9. else10. {11. $postTags = $postTags . ", " . $tag;12. }13. }14.}15.$this->tags = $postTags;

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    8/15

    You most likely noticed that we did not use the tag id arrays. We are going to leave those alonefor now and come back to them later. We just want to get our blog up and running first.

    The last step for our class is to add if statements for each property so that if we pass in nothing, itwill not try to set the current objects property to nothing (this will cause an error). Here is the

    entire BlogPost class with the if statements added:

    view plaincopy to clipboardprint?

    1.

  • 8/13/2019 TMA General Guides Blogs

    9/15

    36. $query = mysql_query("SELECT first_name, last_name FROM people WHERE id= " . $inAuthorId);

    37. $row = mysql_fetch_assoc($query);38. $this->author = $row["first_name"] . " " . $row["last_name"];39. }40.

    41. $postTags = "No Tags";42. if (!empty($inId))43. {44. $query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags)

    ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);45. $tagArray = array();46. $tagIDArray = array();47. while($row = mysql_fetch_assoc($query))48. {49. array_push($tagArray, $row["name"]);50.

    array_push($tagIDArray, $row["id"]);51. }

    52. if (sizeof($tagArray) > 0)53. {54. foreach ($tagArray as $tag)55. {56. if ($postTags == "No Tags")57. {58. $postTags = $tag;59. }60. else61. {62. $postTags = $postTags . ", " . $tag;63. }64. }65. }66. }67. $this->tags = $postTags;68.}69.70.}71.72.?>

    Now that our object class is complete, most of the hard stuff is done! Now all we have to do isset up our database connection and the HTML to display our posts!

    Part 3) Fetching the Data From MySQL and Displaying it With PHP

  • 8/13/2019 TMA General Guides Blogs

    10/15

    Before we do anything we need to set up our includes.php file to hold a reference to ourBlogPost object and connect to our MySQL database. First lets include our object with a simpleinclude statement:

    view plaincopy to clipboardprint?

    1. include 'blogpost.php';Now lets add our database connection:

    view plaincopy to clipboardprint?

    1. $connection = mysql_connect("localhost", "username", "password") or die ("

    Sorry, we were unable to connect to the database server.

    ");

    2. $database = "nettuts_blog";3. mysql_select_db($database, $connection) or die ("

    Sorry, we were unable

    to connect to the database.

    ");

    Next, we need to retrieve our blog posts from the database. To do this, were going to make a

    function that can take up to two arguments. We will overload both of them; so you can call thefunction with either 0, 1, or 2 arguments.

    view plaincopy to clipboardprint?

    1. function GetBlogPosts($inId=null, $inTagId=null)2. {3.4.

    }

    Inside our function we need to check to see what arguments were passed in and create ourMySQL query accordingly.

    view plaincopy to clipboardprint?

    1. function GetBlogPosts($inId=null, $inTagId =null)2. {3. if (!empty($inId))4. {5.

    6. }7. else if (!empty($inTagId))8. {9.10. }11. else12. {

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    11/15

    13.14. }15.}

    So what we are doing here is asking if each argument is not empty, the reason we are using the

    empty function instead of just doing the standard != null comparisonis because empty not onlychecks if the variable is null, but if it empty as well (I.E. ). Now we will write a querydepending on what variables we have. If we pass in a blog post ID we just want that single blogpost, if we give the function a inTagId we want all posts that have that tag, and otherwise we justwant all blog posts in our database.

    view plaincopy to clipboardprint?

    1. if (!empty($inId))2. {3. $query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORD

    ER BY id DESC");4. }5. else if (!empty($inTagId))6. {7. $query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (bl

    og_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =". $tagID . " ORDER BY blog_posts.id DESC");

    8. }9. else10.{11. $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");12.

    }

    The next step is to process the data returned from each query, create the objects, and then addthem to an array to return.

    view plaincopy to clipboardprint?

    1. $postArray = array();2. while ($row = mysql_fetch_assoc($query))3. {4. $myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row['

    firstname'] . " " . $row['lastname'], $row['dateposted']);5. array_push($postArray, $myPost);6. }7. return $postArray;

    Here is the entire includes.php files code:

    view plaincopy to clipboardprint?

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    12/15

    1.

    Now we can move on to displaying our data, lets open up our index.php file and set up a basicHTML page. Inside our body well create a division with an id of main that will contain our

    blog. We will give our blog a title and then have a second div inside main that will be calledblogPosts.

    view plaincopy to clipboardprint?

    1.

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    13/15

    2. My Simple Blog3. 4.5. 6.

    Inside our blogPosts div, well put in some PHP so that we can display ourposts. First we aregoing to include our includes.php file and then call our GetBlogPosts function with no argumentsto get all our blog posts and set that to an array called blogPosts.

    view plaincopy to clipboardprint?

    1.

    Now, we will use a foreach loop to display our blog posts. What a foreach loop does for us rightnow is takes an array and executes the code in the loop for each item in the array, you could alsouse a normal for loop to achieve this but a foreach loop requires less code.

    view plaincopy to clipboardprint?

    1. foreach ($blogPosts as $post)2. {3.4.

    }

    Inside the loop, we use $post as the current array item, and since $blogPosts is an array ofBlogPost objects we can simply use -> to access each property that we want. Lets start by justechoing out the title of each blog post to our page and just add a
    as an example.

    view plaincopy to clipboardprint?

    1. foreach ($blogPosts as $post)2. {3. echo $post->title . "
    ";4.

    }

    If we go to our database and insert some fake data, then open up index.php in a browser, we willget something like this:

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    14/15

    Lets really build our posts in HTML. Each post will be wrapped in a div with a class of post.

    Then we will have the post title inside an h1 tag and the actual post inside a

    tag.

    view plaincopy to clipboardprint?

    1. foreach ($blogPosts as $post)2. {3. echo "";4. echo "" . $post->title . "";5. echo "

    " . $post->post . "

    ";6. echo "";7. }

    Lets also give each post a footer that will include the posts author, date posted, and the posts

    tags. We will put all of this information within a span tag with a class footer.

    view plaincopy to clipboardprint?

    1. foreach ($blogPosts as $post)2. {3. echo "";4. echo "" . $post->title . "";5. echo "

    " . $post->post . "";6. echo "Posted By: " . $post->author . " Posted On: " . $post-

    >datePosted . " Tags: " . $post->tags . "";7. echo "";8. }

    Now lets look at our index.php file again in a browser:

    http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/http://net.tutsplus.com/tutorials/php/how-to-create-an-object-oriented-blog-using-php/
  • 8/13/2019 TMA General Guides Blogs

    15/15

    Looks like its all working! Next, lets add some styling.