{"id":1245,"date":"2018-01-19T19:22:36","date_gmt":"2018-01-19T19:22:36","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/pear-html_table-displaying-tabular-data-in-php-page-2\/"},"modified":"2018-01-19T19:24:38","modified_gmt":"2018-01-19T19:24:38","slug":"pear-html_table-displaying-tabular-data-in-php-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/pear-html_table-displaying-tabular-data-in-php-page-2\/","title":{"rendered":"PEAR HTML_Table: Displaying Tabular Data in PHP Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By W. Jason Gilmore<\/div>\n<div class=\"\">on June 1, 2011<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>Adding Interactive Row Highlighting<\/h2>\n<div class=\"articlePara\">Alright, this is really starting to shape up nicely! I&#8217;d like to add one last feature, a bit of interactivity which will bring specific attention to each row as the user mouses over. Believe it or not this is most easily accomplished using CSS&#8217; <code>hover<\/code> selector. Add the following to your CSS definitions:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>#games tr:hover {\n  background-color: #A4E666;\n}<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">The <code>hover<\/code> selector works in conjunction with all modern browsers. You&#8217;ll also want to comment out or remove the <code>altRowAttributes()<\/code> method call. With the CSS in place, any row mouseovers will cause the row to turn green, as demonstrated in Figure 5.<\/div>\n<div class=\"articlePara\"><a href=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/HTML_Tablefigure5.png\" target=\"_BLANK\"><img decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/HTML_Tablefigure5.png\" alt=\"\" width=\"400\"\/><br \/><strong>Figure 5.<\/strong><\/a> Adding a Bit of Interactivity<\/div>\n<h2>Sorting Rows Interactively<\/h2>\n<div class=\"articlePara\">While you can make an educated guess regarding how the user would like to sort the table, it&#8217;s always ideal to give the user the power to override this default. Add interactive sorting using the <a href=\"http:\/\/tablesorter.com\/\" target=\"_blank\">Tablesorter<\/a> jQuery plugin. Once configured, the user can click on any table header to resort the contents. Of course, to integrate this feature you&#8217;ll need to reference both jQuery and the Tablesorter plugin in your document. Additionally, you&#8217;ll need to reformat the table a bit to use both the <code>THEAD<\/code> and <code>TBODY<\/code> tags. I&#8217;ve provided all of the code used to integrate this interactive feature in the following listing, highlighting key lines:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>&lt;html&gt;\n &lt;head&gt;\n\n&lt;script type=\"text\/javascript\" \n        src=\"http:\/\/www.google.com\/jsapi\"&gt;&lt;\/script&gt;\n&lt;script type=\"text\/javascript\"&gt;\n  google.load(\"jquery\", \"1.5\");\n&lt;\/script&gt;\n\n<strong>&lt;script type=\"text\/javascript\" src=\"jquery.tablesorter.min.js\"&gt;&lt;\/script&gt;<\/strong>\n \n&lt;script type=\"text\/javascript\"&gt;\n  google.setOnLoadCallback(function() {\n    <strong>$(\"#games\").tablesorter(); <\/strong>\n  });\n&lt;\/script&gt;\n\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;?php\n\nrequire_once \"HTML\/Table.php\";\n\n$games = array(\n array(\"Call of Duty: Modern Warfare 2\", \"Activision Inc.\", \"$59.99\"),\n array(\"Super Mario Galaxy 2\", \"Nintendo\", \"$49.99\"),\n array(\"Grand Theft Auto IV\", \"Rockstar Games\", \"$19.99\"),\n array(\"NBA 2K11\", \"2K Sports\", \"$39.99\")\n);\n\n<strong>$table = new HTML_Table(array(\"id\" =&gt; \"games\", \n          \"class\" =&gt; \"tablesorter\"), 0, TRUE);<\/strong>\n\n<strong>$head =&amp; $table-&gt;getHeader();<\/strong>\n$head-&gt;setHeaderContents(0, 0, 'Title');\n$head-&gt;setHeaderContents(0, 1, 'Publisher');\n$head-&gt;setHeaderContents(0, 2, 'Price');\n\n<strong>$body =&amp; $table-&gt;getBody();<\/strong>\nfor ($rows = 0; $rows &lt; count($games); $rows++)\n{\n  for($cols = 0; $cols &lt; count($games[0]); $cols++)\n  {\n    $body-&gt;setCellContents($rows, $cols, $games[$rows][$cols]);\n  }\n\n}\n\necho $table-&gt;toHtml();\n\n?&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p><\/code><\/div>\n<h2>Conclusion<\/h2>\n<div class=\"articlePara\">Using powerful tools such as HTML_Table, CSS and jQuery, it really is incredible what&#8217;s possible with just a few lines of code. What tools do you use to format your tables? Tell us about them in the comments!<\/div>\n<h2>About the Author<\/h2>\n<div class=\"articlePara\">Jason Gilmore is founder of the publishing, training, and consulting firm <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"_blank\">WJGilmore.com<\/a>. He is the author of several popular books <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"_blank\">&#8220;Easy PHP Websites with the Zend Framework&#8221;<\/a>, <a href=\"http:\/\/www.wjgilmore.com\/\" target=\"_blank\">&#8220;Easy PayPal with PHP&#8221;<\/a>, and <a href=\"http:\/\/www.amazon.com\/dp\/1430231149\/\" target=\"_blank\">&#8220;Beginning PHP and MySQL, Fourth Edition&#8221;<\/a>. Follow him on Twitter at <a href=\"http:\/\/www.twitter.com\/wjgilmore\">@wjgilmore<\/a>.<\/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=\"Jason_Gilmore06012011.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Jason_Gilmore06012011.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><\/div>\n","protected":false},"excerpt":{"rendered":"<p>HTML_Table is a great PEAR package that makes tabular data presentation a breeze. Learn how to integrate CSS and jQuery with HTML_Table to create 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-1245","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1245","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=1245"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"predecessor-version":[{"id":2142,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1245\/revisions\/2142"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}