{"id":398,"date":"2014-05-14T20:51:19","date_gmt":"2014-05-15T02:51:19","guid":{"rendered":"http:\/\/arcanasphere.com\/blog\/?p=398"},"modified":"2014-05-14T20:51:19","modified_gmt":"2014-05-15T02:51:19","slug":"postworlds-viewing-pages-php-classes","status":"publish","type":"post","link":"https:\/\/arcanasphere.com\/blog\/2014\/05\/postworlds-viewing-pages-php-classes\/","title":{"rendered":"PostWorlds, Viewing Pages, PHP Classes"},"content":{"rendered":"<p>I looked at this giant post about to hit Facebook and thought it might be better as a blog post. \u00a0I&#8217;m working on a Play By Post site in the style of the site I once loved, and spent a while working on how to view a page that had been stored in the database. \u00a0There were multiple &#8220;hard way&#8221;s to write one of the pages.<\/p>\n<p>All of the code here is very simplified from what I&#8217;m trying to (except the HTML examples), and the variable names are not the same as in my real code.<\/p>\n<p>The easiest way was to intermingle the programming language and the HTML\/web page language at every step of the way. \u00a0The PHP scripting and HTML code would be so intermingled that none of it could be easily re-used, or even understood after any time has passed. \u00a0I&#8217;m not even going to write that here. \u00a0It would have been a piece of script to open the database connection and connect to this &#8220;row&#8221; of data, followed by enough HTML to get to where the next piece of data is needed, followed by PHP script to retrieve that piece of data alone, followed by enough HTML to get to where the next piece of data is needed. \u00a0Everything would have been horribly tangled up, and a pure nightmare to look at.\u00a0<!--more Where I separate my scripting code and my HTML code by creating objects. --><\/p>\n<p>Another way was to write a relatively easy\/straightforward function. The function would collect all of the data and make it available to an array &#8211; or a list of pieces of data. \u00a0To the best of my knowledge, it would have returned a numbered array. So, i would need to remember and\/or document something like:<\/p>\n<blockquote>\n<pre><span style=\"color: #3366ff;\">$pagepart = array();\r\n$pagepart = GetPageFromDatabase($pageid);<\/span><\/pre>\n<\/blockquote>\n<p>Then I need to remember that<\/p>\n<blockquote>\n<pre><span style=\"color: #3366ff;\">$pagepart[0]<\/span> \/* is the title. *\/\r\n<span style=\"color: #3366ff;\">$pagepart[1]<\/span> \/* is the \"body\" of the page. *\/\r\n<span style=\"color: #3366ff;\">$pagepart[2]<\/span> \/* is any unique stylesheet information. *\/\r\n<span style=\"color: #3366ff;\">$pagepart[3]<\/span> \/* is the maximum number of new posts to show on the front page. *\/<\/pre>\n<\/blockquote>\n<p>There are, at present, fourteen pieces of information that would have to be kept track of this way. \u00a0The finished code would look like:<\/p>\n<blockquote>\n<pre>&lt;html&gt;\r\n &lt;head&gt;\r\n &lt;title&gt;<span style=\"color: #3366ff;\">&lt;?php echo $pagepart[0]; ?&gt;<\/span>&lt;\/title&gt;\r\n &lt;link rel=\"stylesheet\" href=\"<span style=\"color: #3366ff;\">&lt;?php echo $pagepart[2]; ?&gt;<\/span>\" \/&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n  <span style=\"color: #3366ff;\">&lt;?php echo $pagepart[1] ?&gt;<\/span>\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<\/blockquote>\n<p>That&#8217;s better, but it&#8217;s going to suck if I want to edit it a year later. \u00a0I have yet to get a function in PHP to return an array with sensible keys. \u00a0For some reason arrays with sensible keys bother the hell out of me when classes exist. Plus, learning to use classes is learning another skill to work toward a better future.<\/p>\n<p>So, I could use classes. Now I can just access something that looks more like<\/p>\n<blockquote>\n<pre><span style=\"color: #3366ff;\">$pagepart = new PageInformations(0,0,0,0,$pageid);\r\n$pagepart-&gt;PopulateFromDatabase();<\/span><\/pre>\n<\/blockquote>\n<p>Later instead of trying to remember if <span style=\"color: #3366ff;\">$pagepart[0]<\/span> is the title or style or body of the page, I have a nicely named piece of data.<\/p>\n<blockquote>\n<pre><span style=\"color: #3366ff;\">echo $pagepart-&gt;title; <span style=\"color: #000000;\">\/* is the title *\/<\/span>\r\necho $pagepart-&gt;body; <span style=\"color: #000000;\">\/* is the body of the page *\/<\/span>\r\necho $pagepart-&gt;style; <span style=\"color: #000000;\">\/* is the unique style information *\/<\/span>\r\necho $pagepart-&gt;ShowThisManyPosts; <span style=\"color: #000000;\">\/* is the maximum number of posts to show on the front page *\/<\/span><\/span><\/pre>\n<\/blockquote>\n<p>Now everything piece of data has an obvious name instead of an obfuscated secret agent number. This should make editing the whole thing easier months or years later when it&#8217;s not fresh in mind\/memory. It also does a nice thing separating my programming language code mostly away from my &#8220;make the web page&#8221; code.<\/p>\n<p>With all of the programming code written on the top of the page, the rest can resemble:<\/p>\n<blockquote>\n<pre>&lt;html&gt;\r\n &lt;head&gt;\r\n &lt;title&gt;<span style=\"color: #3366ff;\">&lt;?php echo $pagepart-&gt;title; ?&gt;<\/span>&lt;\/title&gt;\r\n &lt;link rel=\"stylesheet\" href=\"<span style=\"color: #3366ff;\">&lt;?php echo $pagepart-&gt;style; ?&gt;<\/span>\" \/&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n  <span style=\"color: #3366ff;\">&lt;?php echo $pagepart-&gt;body ?&gt;<\/span>\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<\/blockquote>\n<p>There are regrettably other times when the PHP scripting \/ code need to be much more entwined with the HTML\/web page code than this. \u00a0Whenever possible, I want that last example to represent the bulk of my code. \u00a0The scripting is done first. \u00a0HTML code is written next, only calling a small piece of scripting code when something is needed. \u00a0 That, for the most part, should keep the programming with the programming and the web page with the web page. \u00a0Then if there&#8217;s a programming error I&#8217;m not searching through page code, or if the page does something weird like align itself to the right in a 20-pixel wide DIV, I won&#8217;t have to dig through very much PHP scripting to find out why.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I looked at this giant post about to hit Facebook and thought it might be better as a blog post. \u00a0I&#8217;m working on a Play By Post site in the style of the site I once loved, and spent a while working on how to view a page that had been stored in the database. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/arcanasphere.com\/blog\/2014\/05\/postworlds-viewing-pages-php-classes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PostWorlds, Viewing Pages, PHP Classes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[10],"tags":[142,141],"class_list":["post-398","post","type-post","status-publish","format-standard","hentry","category-nightly-update","tag-php","tag-postworlds"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8uTsK-6q","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/posts\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":3,"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":401,"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/posts\/398\/revisions\/401"}],"wp:attachment":[{"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arcanasphere.com\/blog\/wp-json\/wp\/v2\/tags?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}