{"id":5285,"date":"2013-05-08T12:00:12","date_gmt":"2013-05-08T16:00:12","guid":{"rendered":"https:\/\/www.caskeys.com\/dc\/?p=5285"},"modified":"2017-01-15T15:21:58","modified_gmt":"2017-01-15T20:21:58","slug":"class-tables","status":"publish","type":"post","link":"https:\/\/www.caskeys.com\/dc\/class-tables\/","title":{"rendered":"Class &#8211; Tables"},"content":{"rendered":"<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\n&lt;?php \r\n\r\nclass class_tables {\r\n\r\n\t\/*\r\n\tTables\r\n\tDamon Vaughn Caskey\r\n\t2013-03-21\r\n\t\r\n\tMiscellaneous table functions.\r\n\t*\/\r\n\t\r\n\tpublic \t$cMarkup \t\t= NULL;\t\/\/Resulting markup output. Typically a table.\r\n\tprivate $oUtl\t\t\t= NULL;\t\/\/Utility class object.\r\n\t\r\n\tfunction __construct($oDep)\r\n\t{\r\n\t\t\/*\r\n\t\tConstructor\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013_01_21\r\n\t\t\r\n\t\tClass constructor.\r\n\t\t*\/\r\n\t\t\r\n\t\t\/* Import object dependencies. *\/\r\n\t\t$this-&gt;oUtl = $oDep&#x5B;'Utl'];\r\n\t\t\t\t\t\t\r\n\t\t\/* Verify object dependencies. *\/\r\n\t\tif(!$this-&gt;oUtl)\ttrigger_error(&quot;Missing object dependency: Utility.&quot;, E_USER_ERROR);\t\t\r\n\t}\r\n\t\r\n\tpublic function tables_db_output($oDB, $bRowCount = TRUE, $cFieldSkip = NULL, $cAuxLink = array(&quot;Header&quot; =&gt; NULL, &quot;Link&quot; =&gt; NULL, &quot;Target&quot; =&gt; &quot;_blank&quot;, &quot;Text&quot; =&gt; &quot;Go&quot;), $cAuxLinkFields = NULL, $cRowStyle = NULL)\r\n\t{\t\r\n\t\t\/*\r\n\t\ttables_db_output\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-03-20\r\n\t\t\r\n\t\tCreate complete table markup from database query.\r\n\t\t\r\n\t\tPreconditions:\r\n\t\t\tExecuted database query.\r\n\t\t\t$oDB: Object with object variables populated by query.\r\n\t\t\t$bRowCount: True = Display a row count preceding table.\r\n\t\t\t$cFieldSkip&#x5B;'&lt;fieldname&gt;', ...]: Array of fields from query to skip when creating table.\r\n\t\t\t$cAuxLink&#x5B;'Link', 'Text', 'Target']: Adds action link to end of table.\r\n\t\t\t\tLink: Page name.\r\n\t\t\t\tTarget: Page target (_new, _blank, etc.)\r\n\t\t\t\tText: Text to display.\t\t\t\t\r\n\t\t\t$cAuxLinkFields&#x5B;'&lt;fieldname&gt;', ...]: Fields to pass as part of action link.\r\n\t\t\t$cRowStyle&#x5B;even, odd]: Array to override default alternate row style.\r\n\t\t\t\t\r\n\t\tPostconditions:\r\n\t\t\tPopulate and return $cMarkup with table markup string.\r\n\t\t*\/\r\n\t\t\r\n\t\t$i\t\t\t\t\t\t= NULL;\t\/\/Working counter.\r\n\t\t$iRowCount \t\t\t\t= NULL;\t\/\/Count of records retrieved by query.\r\n\t\t$cOutput\t\t\t\t= NULL;\t\/\/Output string.\r\n\t\t$cFieldMetadata\t\t\t= NULL;\t\/\/Metadata collection (field name, type, etc.) for database columns.\r\n\t\t$cFieldMetaDataName \t= NULL; \/\/Individual item name from metadata colelction.\r\n\t\t$cFieldMetaDataValue\t= NULL;\t\/\/Individual item value from metadata collection.\r\n\t\t$iFields\t\t\t\t= NULL;\t\/\/Field counter\/index.\r\n\t\t$cFieldName\t\t\t\t= NULL; \/\/Field name array.\r\n\t\t$cName\t\t\t\t\t= NULL;\t\/\/Table markup write in: Field name.\r\n\t\t$cValue \t\t\t\t= NULL;\t\/\/Table markup write in: Field value.\r\n\t\t$cLink \t\t\t\t\t= NULL;\t\/\/Table markup write in: Action link.\r\n\t\t\r\n\t\t\/* Add extra markup if cAuxLink has a value, otherwise leave NULL. &quot; *\/\r\n\t\t\r\n\t\tif($cAuxLink&#x5B;&quot;Link&quot;] != NULL)\r\n\t\t{\t\t\t\r\n\t\t\t$cAuxLink&#x5B;&quot;Link&quot;] .= &quot;?&quot;;\r\n\t\t\t$cAuxLink&#x5B;&quot;Header&quot;] = $cAuxLink&#x5B;&quot;Header&quot;] != NULL ? &quot;&lt;th&gt;&quot;.$cAuxLink&#x5B;&quot;Header&quot;].&quot;&lt;\/th&gt;&quot; : &quot;&lt;th&gt;Action&lt;\/th&gt;&quot;;\t\t\t\r\n\t\t}\r\n\t\t\r\n\t\tif($bRowCount)\r\n\t\t{\r\n\t\t\t$iRowCount = $oDB-&gt;iDBRowCount; \r\n\t\t\t$cOutput .= '&lt;span class=&quot;row_count&quot;&gt;' .$iRowCount. ' records found.&lt;\/span&gt;&lt;br\/&gt;&lt;br\/&gt;';\r\n\t\t}\r\n\t\t\r\n\t\t$cOutput .= '&lt;div title=&quot;Table&quot; class=&quot;overflow&quot;&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; bgcolor=&quot;#CCCCCC&quot;&gt;&lt;tr&gt;';\t\r\n\t\t\r\n\t\t\/* Zero counter *\/\r\n\t\t$i = 0;\r\n\t\t\r\n\t\t\/* Loop each column in query result. *\/\t\r\n\t\tforeach($oDB-&gt;cDBMeta as $cFieldMetadata)\r\n\t\t{\t\t\t\t\t\r\n\t\t\t\/* Loop coloumn metadata collection. *\/\r\n\t\t\tforeach($cFieldMetadata as $cFieldMetaDataName =&gt; $cFieldMetaDataValue)\r\n\t\t\t{\t\t\t\r\n\t\t\t\t\/* Column name? *\/\t\t\t\t\r\n\t\t\t\tif($cFieldMetaDataName == 'Name')\r\n\t\t\t\t{\t\t\t\t\t\t\r\n\t\t\t\t\t\/* Check field skip array before using this field name *\/\r\n\t\t\t\t\tif(!in_array($cFieldMetaDataValue, $cFieldSkip))\r\n\t\t\t\t\t{\t\t\t\t\t\t\r\n\t\t\t\t\t\t\/* Output to table header markup and populate name array. *\/\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t$cOutput .= &quot;&lt;th&gt;&quot;.$cFieldMetaDataValue.&quot;&lt;\/th&gt;&quot;;\t\/\/Populate table header markup.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\r\n\t\t\t\t\t$cFieldName&#x5B;$i] = $cFieldMetaDataValue;\t\t\t\t\/\/Populate Name array.\r\n\t\t\t\t\t\r\n\t\t\t\t\t\/* Increment field count. *\/\r\n\t\t\t\t\t$iFields++;\r\n\t\t\t\t}\t\t\t\t\t\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/* Increment counter. *\/\r\n\t\t\t$i++;\t\t\t\t\t\t\t\r\n\t\t}\t\t\r\n\t\t\t\t\t\r\n\t\t$cOutput .= $cAuxLink&#x5B;&quot;Header&quot;];\t\r\n\t\t\t\t\r\n\t\t\/* Output query results as table. *\/\r\n\t\twhile($oDB-&gt;db_line(SQLSRV_FETCH_NUMERIC))\r\n\t\t{\t\t\r\n\t\t\r\n\t\t\t$cLink \t\t= $cAuxLink&#x5B;&quot;Link&quot;];\r\n\t\t\t\r\n\t\t\t\/* Increment line counter *\/\r\n\t\t\t$iLine++;\r\n\t\t\t\r\n\t\t\t\/* Insert table row and style. *\/\r\n\t\t\t$cOutput .= &quot;&lt;tr bgcolor='&quot;.$this-&gt;oUtl-&gt;utl_color_alternation($iLine).&quot;'&gt;&quot;;\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\tfor ($i = 0; $i &lt; $iFields; $i++)\r\n\t\t\t{\t\t\t\t\t\r\n\t\t\t\t$cName\t\t= $cFieldName&#x5B;$i];\r\n\t\t\t\t$cValue \t= $oDB-&gt;cDBLine&#x5B;$i];\t\t\t\t\t\t\t\t\r\n\t\t\t\r\n\t\t\t\t\/* Check field skip array before using this field. *\/\r\n\t\t\t\tif(!in_array($cName, $cFieldSkip))\r\n\t\t\t\t{\t\t\t\t\r\n\t\t\t\t\t$cOutput .= &quot;&lt;td&gt;&quot;.$cValue.&quot;&lt;\/td&gt;&quot;;\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\tif($cLink != NULL)\r\n\t\t\t\t{\t\t\t\r\n\t\t\t\t\tif(in_array($cName, $cAuxLinkFields))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t$cLink .= $cFieldName&#x5B;$i].&quot;=&quot;.$cValue.&quot;&amp;&quot;;\r\n\t\t\t\t\t}\t\t\t\t\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif($cLink != NULL)\r\n\t\t\t{\r\n\t\t\t\t$cOutput .= '&lt;td&gt;&lt;a href=&quot;'.$cLink.'&quot; target=&quot;'.$cAuxLink&#x5B;&quot;Target&quot;].'&quot;&gt;'.$cAuxLink&#x5B;&quot;Text&quot;].'&lt;\/a&gt;&lt;\/td&gt;';\t\t\t\t\t\t\t\t\r\n\t\t\t}\r\n\t\t}\r\n\t\t\t\t\t\r\n\t\t$cOutput .= &quot;&lt;\/table&gt;&lt;br\/&gt;&lt;br\/&gt;&lt;\/div&gt;&quot;;\r\n\t\t\r\n\t\t$this-&gt;cMarkup = $cOutput;\r\n\t\t\r\n\t\treturn $this-&gt;cMarkup;\r\n\t}\t\t\r\n}\r\n?&gt;\r\n\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tables class.<\/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":true,"_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":[71],"tags":[27,234],"class_list":["post-5285","post","type-post","status-publish","format-standard","hentry","category-technology-temerity","tag-coding","tag-coding-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lNM5-1nf","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/comments?post=5285"}],"version-history":[{"count":2,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5285\/revisions"}],"predecessor-version":[{"id":6182,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5285\/revisions\/6182"}],"wp:attachment":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/media?parent=5285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/categories?post=5285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/tags?post=5285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}