{"id":1213,"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\/"},"modified":"2018-01-19T19:24:37","modified_gmt":"2018-01-19T19:24:37","slug":"using-pears-html_table-class","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/using-pears-html_table-class\/","title":{"rendered":"Using PEAR&#8217;s HTML_Table Class"},"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>A Simpler Approach to Tables<\/h2>\n<p>Within the current incarnation of (X)HTML tables are the building blocks to well organized  and attractive designs.  They allow the developer an incredible amount of control over  the visual representation of information and graphics.  However they come with a catch,  they are an absolute headache to write into your PHP code.  The simple process of querying  a database and showing the results in a table becomes a disorganized and ungainly process  looking something like this:<\/p>\n<p><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;\">\/\/echo\u00a0our\u00a0table <br \/><\/span><span style=\"color: #007700;\">echo\u00a0<\/span><span style=\"color: #dd0000;\">\"&lt;table\u00a0width=\"100%\"\u00a0border=\"1\"&gt;\"<\/span><span style=\"color: #007700;\">; <br \/>echo\u00a0<\/span><span style=\"color: #dd0000;\">\"&lt;tr&gt; <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;th&gt;Name&lt;\/th&gt; <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;th&gt;E-mail&lt;\/th&gt; <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;th&gt;Phone\u00a0Number&lt;\/th&gt; <br \/>&lt;tr&gt;\"<\/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\u00a0echo\u00a0<\/span><span style=\"color: #dd0000;\">\"&lt;tr&gt; <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;td\u00a0bgcolor=\"#COCOCO\"&gt;\"\u00a0<\/span><span style=\"color: #007700;\">.\u00a0<\/span><span style=\"color: #0000bb;\">$row<\/span><span style=\"color: #007700;\">[<\/span><span style=\"color: #dd0000;\">'name'<\/span><span style=\"color: #007700;\">]\u00a0.\u00a0<\/span><span style=\"color: #dd0000;\">\"&lt;\/td&gt; <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;td\u00a0bgcolor=\"#COCOCO\"&gt;\"\u00a0<\/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.\u00a0<\/span><span style=\"color: #dd0000;\">\"&lt;\/td&gt; <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;td\u00a0bgcolor=\"#COCOCO\"&gt;\"\u00a0<\/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;\">]\u00a0.\u00a0<\/span><span style=\"color: #dd0000;\">\"&lt;\/td&gt; <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/tr&gt;\"<\/span><span style=\"color: #007700;\">; <br \/>} <br \/>echo\u00a0<\/span><span style=\"color: #dd0000;\">\"&lt;\/table&gt;\"<\/span><span style=\"color: #007700;\">; <br \/><\/span><span style=\"color: #0000bb;\">?&gt; <br \/><\/span> <\/span> <\/code><\/span><\/p>\n<p>Clearly this is not an eloquent solution.  We either need to use multiple echo statements  or as we use the above string one together to make one long statement.<\/p>\n<p>Thankfully there is a solution: the PEAR HTML_Table class.<\/p>\n<p>What is PEAR HTML_Table?<\/p>\n<p><a href=\"http:\/\/pear.php.net\/\" target=\"_blank\">(PHP Extension and Application Repository)<\/a> (PEAR) is a large body of PHP classes developed to make your life as a PHP programmer easier,  by providing a library of prewritten code while also establishing a series of coding standards.<\/p>\n<p>The HTML_Table class makes building tables much simpler, helps you  maintain cleaner more readable code and brings the advantages of object oriented code to your work.   If you are unfamiliar with working with objects in PHP, I recommend that you read either the relevant  section of the <a href=\"http:\/\/us3.php.net\/manual\/en\/language.oop.php\">PHP documentation<\/a> or this  article will make very little sense. You will also need to make sure that you download and install a  copy of the code as well as it&#8217;s dependencies, all of which can be found  <a href=\"http:\/\/pear.php.net\/package-info.php?package=HTML_Table\">here<\/a>.  Instructions for  installing PEAR and it&#8217;s various classes is beyond the scope of this document but the  <a href=\"http:\/\/pear.php.net\/manual\/en\/\">PEAR documentation<\/a> is very good and should be  able to answer your questions.<\/p>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/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=\"nortrup200308184658.html?page=2\">2<\/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=\"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=\"nortrup200308184658.html?page=2\">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-1213","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1213","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=1213"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1213\/revisions"}],"predecessor-version":[{"id":3117,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1213\/revisions\/3117"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}