-2

I have a display form as on image, if click button "add jadwal" automatic save to list table.

Asking : how to list data datatables after clicking button "submit/simpan" to be data json as this below? thanks

 {
"mapel": [
  {
    "kelas": "1",
    "hari": "Senin",
  },
  {
    "kelas": "2",
    "hari": "Senin",
  },
]

}

image

5
  • Please include all necessary information in the question itself, don't just post a link or an image. Commented Mar 12, 2019 at 9:57
  • I already update my question. Sorry my english bahasa not good Commented Mar 12, 2019 at 12:54
  • I got this from another post. Try this Commented Mar 12, 2019 at 13:20
  • No convert html to json but get all list datatables to array json Commented Mar 12, 2019 at 13:30
  • are you going to use Codeigniter-3 as you tagged in your question? Commented Mar 13, 2019 at 9:33

1 Answer 1

0

If you are able to obtain a DOMDocument object representing your HTML, then you just need to traverse it recursively and construct the data structure that you want.

Converting your HTML document into a DOMDocument should be as simple as this:

function html_to_obj($html) {
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    return element_to_obj($dom->documentElement);
}

Then, a simple traversal of $dom->documentElement which gives the kind of structure you described could look like this:

function element_to_obj($element) {
    $obj = array( "tag" => $element->tagName );
    foreach ($element->attributes as $attribute) {
        $obj[$attribute->name] = $attribute->value;
    }
    foreach ($element->childNodes as $subElement) {
        if ($subElement->nodeType == XML_TEXT_NODE) {
            $obj["html"] = $subElement->wholeText;
        }
        else {
            $obj["children"][] = element_to_obj($subElement);
        }
    }
    return $obj;
}

Test case

<html lang="en">
    <head>
        <title> This is a test </title>
    </head>
    <body>
        <h1> Is this working? </h1>  
        <ul>
            <li> Yes </li>
            <li> No </li>
        </ul>
    </body>
</html>



header("Content-Type: text/plain");
echo json_encode(html_to_obj($html), JSON_PRETTY_PRINT);

Output

{
    "tag": "html",
    "lang": "en",
    "children": [
        {
            "tag": "head",
            "children": [
                {
                    "tag": "title",
                    "html": " This is a test "
                }
            ]
        },
        {
            "tag": "body",
            "html": "  \n        ",
            "children": [
                {
                    "tag": "h1",
                    "html": " Is this working? "
                },
                {
                    "tag": "ul",
                    "children": [
                        {
                            "tag": "li",
                            "html": " Yes "
                        },
                        {
                            "tag": "li",
                            "html": " No "
                        }
                    ],
                    "html": "\n        "
                }
            ]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

4 Comments

No convert html to json but get all list datatables to array json
Can you form the json manually by iterating through the datatables?
I can't. it's my problem. how to get all data in datatables
What is the problem? The code I have provided can also be used to read data from html controls on your form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.