{"id":1216,"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-4\/"},"modified":"2018-01-19T19:24:37","modified_gmt":"2018-01-19T19:24:37","slug":"using-pears-html_table-class-page-4","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/using-pears-html_table-class-page-4\/","title":{"rendered":"Using PEAR&#8217;s HTML_Table Class Page 4"},"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<div class=\"articleHeader\">A Few Last Tricks<\/div>\n<div class=\"articlePara\">And before we close here are few last tricks that come along with this very useful class.<\/div>\n<h2>Alternating Rows<\/h2>\n<div class=\"articlePara\">One of the most common tasks when creating tables containing database output is alternating  colors to make it easier to read. The HTML_Table class makes this easy to do, using the  altRowAttributes function. Using our example from before the function works like this:<\/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;\">\/\/..database\u00a0retrieval...\u00a0 <\/p>\n<p><\/span><span style=\"color: #007700;\">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 \/>} <br \/><\/span><span style=\"color: #ff8000;\">\/\/alternate\u00a0between\u00a02\u00a0colors <br \/><\/span><span style=\"color: #0000bb;\">$attributes1\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">bgcolor\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #ff8000;\">#336699; <br \/><\/span><span style=\"color: #0000bb;\">$attributes2\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #0000bb;\">bgcolor\u00a0<\/span><span style=\"color: #007700;\">=\u00a0<\/span><span style=\"color: #ff8000;\">#COCOCO; <br \/><\/span><span style=\"color: #0000bb;\">$table<\/span><span style=\"color: #007700;\">-&gt;<\/span><span style=\"color: #0000bb;\">altRowAttributes<\/span><span style=\"color: #007700;\">(<\/span><span style=\"color: #0000bb;\">1<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$attributes1<\/span><span style=\"color: #007700;\">,\u00a0<\/span><span style=\"color: #0000bb;\">$attributes2<\/span><span style=\"color: #007700;\">); <\/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\">This will result in the rows switching colors with every row, starting with the second (as our first row is a header).<\/div>\n<div class=\"articleHeader\">Outputting to HTML<\/div>\n<div class=\"articlePara\">Another useful feature is the toHtml() function which works much the same as the display  function but instead returns the html rather than outputting directly.  This is useful if you  are building the table in a function or need to display the table in a place other than where  you built it.<\/div>\n<div class=\"articlePara\">In parting, I hope that I have shown you the benefits of the HTML_Table class.  There are  several other functions which I felt were beyond the scope of this introduction but I very  much encourage you to browse the code yourself and find them.  I think you will find the  documentation to be very well done.<\/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=\"nortrup200308189ba9.html?page=3\">\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=\"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=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">4<\/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-1216","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1216","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=1216"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":3120,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1216\/revisions\/3120"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}