{"id":244,"date":"2011-12-09T17:37:28","date_gmt":"2011-12-09T22:37:28","guid":{"rendered":"http:\/\/littlesvr.ca\/grumble\/?p=244"},"modified":"2011-12-09T17:37:28","modified_gmt":"2011-12-09T22:37:28","slug":"smaler-php-generated-html","status":"publish","type":"post","link":"http:\/\/littlesvr.ca\/grumble\/2011\/12\/09\/smaler-php-generated-html\/","title":{"rendered":"Smaler PHP-generated HTML"},"content":{"rendered":"<p>By Andrew Smith<\/p>\n<p>I will have something like this on one of the pages in the website:<\/p>\n<p><a href=\"http:\/\/littlesvr.ca\/grumble\/wp-content\/uploads\/2011\/12\/pofileupload.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-245\" title=\"pofileupload\" src=\"http:\/\/littlesvr.ca\/grumble\/wp-content\/uploads\/2011\/12\/pofileupload.png\" alt=\"\" width=\"386\" height=\"753\" \/><\/a><\/p>\n<p>Each of those dropdown lists has the 190 language codes I mentioned in the previous post. It may not sound like a lot but 1900 &lt;option&gt; values really is a lot of HTML. It&#8217;s very easy to generate it all in PHP (one loop basically) but the result still has to be pulled over the internet, and suck up my bandwidth.<\/p>\n<p>I had to think about this one for a bit, but I came up with a decent solution.<\/p>\n<p>In my PHP loop I create two javascript arrays: one for visible name and one for value. I also have ten sets of dropdowns printed as HTML, but with the dropdown lists empty.<\/p>\n<p>Then I write some javascript that will onload populate all the dropdown lists with all the names and values from the arrays.<\/p>\n<p>This made the HTML sent to the browser probably 90% smaller, which made me very happy.<\/p>\n<p>Just because it&#8217;s funny, here&#8217;s what some the php code looks like, I&#8217;m glad noone else will be working with me on this so I won&#8217;t have to explain it :)<\/p>\n<blockquote>\n<pre>\u00a0 $languages = array();\r\n\u00a0 for ( $rowNum = 0; ($row = mysql_fetch_row($result)) != FALSE; $rowNum++)\r\n\u00a0 {\r\n\u00a0\u00a0\u00a0 $languages[$row[0]] = $row[1];\r\n\u00a0 }\r\n?&gt;\r\n\u00a0\u00a0\u00a0 &lt;script type=\"text\/javascript\"&gt;\r\n\u00a0\u00a0\u00a0 function contentOnLoad()\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 var languageCodes = new Array(&lt;?php \r\n\u00a0 # make the PHP array a Javascript array\r\n\u00a0 $firstElement = TRUE;\r\n\u00a0 foreach ($languages as $code =&gt; $name)\r\n\u00a0 {\r\n\u00a0\u00a0\u00a0 if ($firstElement)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 $firstElement = FALSE;\r\n\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 echo \",\";\r\n\r\n\u00a0\u00a0\u00a0 echo \"'\" . $code . \"'\";\r\n\u00a0 }<\/pre>\n<\/blockquote>\n<p>That&#8217;s PHP, HTML, and JS all in one, still cracks me up :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By Andrew Smith I will have something like this on one of the pages in the website: Each of those dropdown lists has the 190 language codes I mentioned in the previous post. It may not sound like a lot but 1900 &lt;option&gt; values really is a lot of HTML. It&#8217;s very easy to generate &hellip; <\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-andrew","4":"post-244","6":"format-standard","7":"category-ostd","8":"category-safeforseneca"},"_links":{"self":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":3,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":248,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/244\/revisions\/248"}],"wp:attachment":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}