Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- CREATED BY Greg Karz
- v1.0
- Bugs:
- - Object in Object (ex. {obj:{...}}) turns to array (ex. {obj:{...}} -> {obj:[...]})
- Todo:
- - Add Objects, text, numbers, arrays [...]
- - Better HTML5 support in the form.
- Please include my name in any project that uses this script. thx.
- Copyright (C) 2011 Greg Karz
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- $ob = json_decode('{"string":"str", "longString":"fdgsdfgdfgdfgagasgdfgsdfg", "bool_t": true, "bool_f": false, "obj": {"v1": 10, "v2": 20}, "arr": ["val1", "val2", "val3"]}');
- function doObject ($obj, $name = '', $parentKey = '') {
- $ret = ($parentKey == '' ? "<form method='post'>\n<script>\nfunction $(id) {\nreturn document.getElementById(id);\n}\n</script>\n" : '');
- $ret .= "<ul>\n";
- $ret .= "<li onMouseUp=\"if(this.getAttribute('open') == 'false') {for(var i = 1; i < this.parentNode.children.length; i ++) {this.parentNode.children[i].style.display='block';} this.setAttribute('open', 'true');} else {for(var i = 1; i < this.parentNode.children.length; i ++) {this.parentNode.children[i].style.display='none';} this.setAttribute('open', 'false');}\" open=\"true\" onMouseOver=\"this.style.backgroundColor='#cccccc';\" onMouseOut=\"this.style.backgroundColor='#ffffff';\" style=\"-moz-user-select:none;cursor: pointer;background-color: #ffffff; border: 1px dashed #ccc;\">" . $name . "</li>\n";
- $ret2 = '';
- foreach($obj as $key => $value) {
- $g = gn($parentKey, $key);
- if(gettype($value) == 'object') {
- $ret .= doObject ($value, $key . ' (Object)', $g);
- } else if(gettype($value) == 'array') {
- $ret .= doObject ($value, $key . ' (Array)', $g);
- } else if(gettype($value) == 'boolean') {
- $ret2 .= "<li><table width=\"300px\" border=\"0\"><tr align=\"left\"><td>" . $key . "</td><td align=\"left\"><input type=\"checkbox\"" . ($value ? 'checked ' : '') . ' onClick="if($(\'' . $g . '\').value == \'(bool)true\'){$(\'' . $g . '\').value = \'(bool)false\';}else{$(\'' . $g . "').value = '(bool)true';}\" /><input type=\"hidden\" id='" . $g . "' name='" . $g . "' value=\"" . ($value ? '(bool)true' : '(bool)false') . "\"></td></tr></table></li>\n";
- } else {
- if(strlen($value) < 10) {
- $ret2 .= "<li><table width=\"300px\" border=\"0\"><tr align=\"left\"><td>" . $key . "</td><td align=\"right\"><input name='" . $g . "' type=\"text\" value=\"" . $value . "\" /></td></tr></table></li>\n";
- } else {
- $ret2 .= "<li><table width=\"300px\" border=\"0\"><tr align=\"left\"><td>" . $key . "</td><td align=\"right\"><textArea name='" . $g . "'>" . $value . "</textArea></td></tr></table></li>\n";
- }
- }
- }
- if($ret2 != '') {
- $ret .= "<ul>" . $ret2 . "</ul>\n";
- }
- $ret .= "</ul>\n" . ($parentKey == '' ? "<input type='submit' value='submit' />\n</form>" : "");
- return $ret;
- }
- function gn($pk, $k) {
- return $pk . ($pk == '' ? '' : '[') . $k . ($pk == '' ? '' : ']');
- }
- function formatData($dat) {
- $tmpDat;
- foreach($dat as $key => $value) {
- if(gettype($value) == 'object') {
- $tmpDat[$key] = formatData($value);
- } else if(gettype($value) == 'array') {
- $tmpDat[$key] = formatData($value);
- } else {
- if(strpos($value, '(bool)') !== false) {
- $b = explode('(bool)', $value);
- $b = $b[1];
- $tmpDat[$key] = ($b == 'true');
- } else {
- $tmpDat[$key] = $value;
- }
- }
- }
- return $tmpDat;
- }
- echo "<!DOCTYPE html><div style=\"width: 800px;\">" . (!empty($_POST) ? doObject (formatData($_POST), 'Data:') : doObject ($ob, 'Data:')) . "</div><br/><b>JSON:</b><br/><code>" . json_encode(formatData($_POST)) . "</code>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement