{"id":1214,"date":"2018-01-19T19:22:34","date_gmt":"2018-01-19T19:22:34","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/using-pears-html_table-class-page-2\/"},"modified":"2018-01-19T19:24:37","modified_gmt":"2018-01-19T19:24:37","slug":"using-pears-html_table-class-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/using-pears-html_table-class-page-2\/","title":{"rendered":"Using PEAR&#8217;s HTML_Table Class Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Andy Nortrup<\/div>\n<div class=\"\">on August 18, 2003<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>So How do I Use This Thing?<\/h2>\n<div class=\"articlePara\">To show you the advantages of the HTML_Table class let&#8217;s recreate the table we used  before, this time using the HTML_Table class.<\/div>\n<div class=\"articlePhpEx\"><span style=\"font-family: courier;\"><code><span style=\"color: #000000;\"> <br \/><span style=\"color: #0000bb;\">&lt;?php <br \/><\/span><span style=\"color: #ff8000;\">\/\/get\u00a0our\u00a0query <br \/><\/span><span style=\"color: #0000bb;\">$connection\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">mysql_connect<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #dd0000;\">\"localhost\"<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #dd0000;\">\"me\"<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #dd0000;\">\"password\"<\/span><span style=\"color: #007700;\">); <br \/><\/span><span style=\"color: #0000bb;\">$sql\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #dd0000;\">\"SELECT\u00a0*\u00a0FROM\u00a0users\"<\/span><span style=\"color: #007700;\">; <br \/><\/span><span style=\"color: #0000bb;\">$result\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">mysql_query<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$sql<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$connection<\/span><span style=\"color: #007700;\">); <\/p>\n<p><\/span><span style=\"color: #ff8000;\">\/\/include\u00a0the\u00a0class <br \/><\/span><span style=\"color: #007700;\">require_once(<\/span><span style=\"color: #dd0000;\">\"\/HTML\/Table.php\"<\/span><span style=\"color: #007700;\">); <\/p>\n<p><\/span><span style=\"color: #ff8000;\">\/\/set\u00a0table\u00a0attributes <br \/><\/span><span style=\"color: #0000bb;\">$attributes\u00a0<\/span><span style=\"color: #007700;\">=\u00a0array(<\/span><span style=\"color: #dd0000;\">\"width\"<\/span><span style=\"color: #007700;\">=&gt;<\/span><span style=\"color: #dd0000;\">\"100%\"<\/span><span style=\"color: #007700;\">, <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #dd0000;\">\"border\"<\/span><span style=\"color: #007700;\">=&gt;<\/span><span style=\"color: #dd0000;\">\"1\"<\/span><span style=\"color: #007700;\">); <\/p>\n<p><\/span><span style=\"color: #ff8000;\">\/\/create\u00a0the\u00a0table\u00a0class <br \/><\/span><span style=\"color: #0000bb;\">$table\u00a0<\/span><span style=\"color: #007700;\">=\u00a0new\u00a0<\/span><span style=\"color: #0000bb;\">HTML_Table<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$attributes<\/span><span style=\"color: #007700;\">); <\/p>\n<p><\/span><span style=\"color: #ff8000;\">\/\/build\u00a0our\u00a0first\u00a0row <br \/><\/span><span style=\"color: #0000bb;\">$contents\u00a0<\/span><span style=\"color: #007700;\">=\u00a0array(<\/span><span style=\"color: #dd0000;\">\"Name\"<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #dd0000;\">\"E-Mail\"<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #dd0000;\">\"Phone\u00a0Number\"<\/span><span style=\"color: #007700;\">); <br \/><\/span><span style=\"color: #0000bb;\">$attributes\u00a0<\/span><span style=\"color: #007700;\">=\u00a0array(<\/span><span style=\"color: #dd0000;\">\"bgcolor\"<\/span><span style=\"color: #007700;\">=<\/span><span style=\"color: #dd0000;\">\"#336699\"<\/span><span style=\"color: #007700;\">); <br \/><\/span><span style=\"color: #0000bb;\">$table<\/span><span style=\"color: #007700;\">-&gt;<\/span><span style=\"color: #0000bb;\">addRow<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$contents<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$attributes<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #dd0000;\">\"TH\"<\/span><span style=\"color: #007700;\">); <\/p>\n<p><\/span><span style=\"color: #ff8000;\">\/\/loop\u00a0through\u00a0and\u00a0add\u00a0our\u00a0data <br \/><\/span><span style=\"color: #0000bb;\">$attributes\u00a0<\/span><span style=\"color: #007700;\">=\u00a0array(<\/span><span style=\"color: #dd0000;\">\"bgcolor\"<\/span><span style=\"color: #007700;\">=<\/span><span style=\"color: #dd0000;\">\"#COCOCO\"<\/span><span style=\"color: #007700;\">) <\/p>\n<p>while(<\/span><span style=\"color: #0000bb;\">$row\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">mysql_fetch_array<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$result<\/span><span style=\"color: #007700;\">))\u00a0{ <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000bb;\">$contents\u00a0<\/span><span style=\"color: #007700;\">=\u00a0array(<\/span><span style=\"color: #0000bb;\">$row<\/span><span style=\"color: #007700;\">[<\/span><span style=\"color: #dd0000;\">'name'<\/span><span style=\"color: #007700;\">],\u00a0<\/span><span style=\"color: #0000bb;\">$row<\/span><span style=\"color: #007700;\">[<\/span><span style=\"color: #dd0000;\">'email'<\/span><span style=\"color: #007700;\">],\u00a0<\/span><span style=\"color: #0000bb;\">$row<\/span><span style=\"color: #007700;\">[<\/span><span style=\"color: #dd0000;\">'phone'<\/span><span style=\"color: #007700;\">]); <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000bb;\">$table<\/span><span style=\"color: #007700;\">-&gt;<\/span><span style=\"color: #0000bb;\">addRow<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$contents<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$attributes<\/span><span style=\"color: #007700;\">); <br \/>} <\/p>\n<p><\/span><span style=\"color: #0000bb;\">$table<\/span><span style=\"color: #007700;\">-&gt;<\/span><span style=\"color: #0000bb;\">display<\/span><span style=\"color: #007700;\">(); <br \/><\/span><span style=\"color: #0000bb;\">?&gt; <br \/><\/span> <\/span> <\/code><\/span><\/div>\n<div class=\"articlePara\">As we can clearly see this approach is much cleaner and more efficient.   So lets take a walk through the code and see how it works.<\/div>\n<div class=\"articlePara\">The first part of the code, which retrieves our database results, remains the same.   Next we have a require_once statement to include the Table Class into our current document.<\/div>\n<div class=\"articlePara\">Next we define the attributes of our table.   This can be done by either creating a string of attributes, or creating an associative  array of the attributes and values.<\/div>\n<div class=\"articlePhpEx\"><span style=\"font-family: courier;\"><code><span style=\"color: #000000;\"> <br \/><span style=\"color: #0000bb;\">&lt;?php <br \/>$attributes\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #dd0000;\">\"width=\"100%\"\u00a0border=\"1\"\"<\/span><span style=\"color: #007700;\">;\u00a0<\/span><span style=\"color: #ff8000;\">\/\/string\u00a0of\u00a0attributes <br \/><\/span><span style=\"color: #0000bb;\">$attributes\u00a0<\/span><span style=\"color: #007700;\">=\u00a0array(<\/span><span style=\"color: #dd0000;\">\"width\"<\/span><span style=\"color: #007700;\">=&gt;<\/span><span style=\"color: #dd0000;\">\"100%\"<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #dd0000;\">\"border\"<\/span><span style=\"color: #007700;\">=&gt;<\/span><span style=\"color: #dd0000;\">\"1\"<\/span><span style=\"color: #007700;\">);\u00a0<\/span><span style=\"color: #ff8000;\">\/\/array\u00a0of\u00a0attributes <br \/><\/span><span style=\"color: #0000bb;\">?&gt; <br \/><\/span> <\/span> <\/code><\/span><\/div>\n<div class=\"articlePara\">Now that we have defined the way that we want the table to look we create an instance of the  class with our attributes as an argument.<\/div>\n<div class=\"articlePara\">In the next several lines we create the first line of our table. First we define the contents  in an array with each element representing a different cell of the table.  Next we define the  attributes of that row. Then we use the addRow function to add the row to the table.  Note that  we use the third argument to set the cell type to be a header. The addRow function has the following  attributes:<\/div>\n<div class=\"articlePhpEx\"><span style=\"font-family: courier;\"><code><span style=\"color: #000000;\"> <br \/><span style=\"color: #0000bb;\">&lt;?php <br \/>addRow<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">$contents\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">null<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$attributes\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">null<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$type\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #dd0000;\">'TD'<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$inTR\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">false<\/span><span style=\"color: #007700;\">) <br \/><\/span><span style=\"color: #0000bb;\">?&gt; <br \/><\/span> <\/span> <\/code><\/span><\/div>\n<div class=\"articlePara\">Now that we have a header row, we loop through our database results and add a row to the table  for each row of the results.<\/div>\n<div class=\"articlePara\">Lastly we add use the display() function to output our table.<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"nortrup20030818.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"nortrup20030818.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"nortrup200308189ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"nortrup20030818fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"nortrup200308189ba9.html?page=3\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Within the current incarnation of (X)HTML tables are the building blocks to well organized and attractive designs. They allow the developer an<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1214","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1214"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1214\/revisions"}],"predecessor-version":[{"id":3118,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1214\/revisions\/3118"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}