Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Write a php recursive function to display the content of “sample.json” file in a table The file content should display under the “Recursive” section as shown in the included picture. It should display...

1 answer below »
Write a php recursive function to display the content of “sample.json” file in a table
The file content should display under the “Recursive” section as shown in the included picture.
It should display the sections from sample.json file in a table similar to that of “function processData($data)” Please take a look at “function processData($datat)” to understand how it’s functioning.
Write php function in “function processRecursive($data)” in the function.php file
You can use the “function processData($data)” for understanding
“function upload($filename, $dir)” reads the file and your recursive function in “function processData($data) displays the file.
I used “Visual Studio Code” as my edito
I used “XAMPP control Panel v3.2.4” running “Apache” and “MySQL” as server to run index.php
Do not wo
y about the “include($root. ‘/includes/header.php’)”,
“include($root. ‘/includes/menu.php’)” and “include($root. ‘/includes/footer.php’)” You should be able to import functions.php, index.php and lab4.css in your visual studio code or editor of choice and start working.
Answered Same Day Jun 10, 2021

Solution

Sanghamitra answered on Jun 11 2021
151 Votes
website/.~lock.recursive.docx#
,LAPTOP-I8ULTUQN/HP,,10.06.2020 17:53,file:
C:/Users/HP/AppData/Roaming/Li
eOffice/4;
website/Capture.JPG
website/functions.php
?php
function processData($data){
$dataA
ay = json_decode($data, true);
if(is_a
ay($dataA
ay)){
$result = "";
foreach($dataA
ay as $key=>$val){
$result.="td class='key'>".$key.":
td
td class='value'>";
if(gettype($val)=="a
ay"){
$result.= "
";
foreach($val as $subkey=>$subval){
$result.="td class='subkey'>".$subkey.":
td
td class='subvalue'>";
if(gettype($subval)=="a
ay"){
$result.= "
";
foreach($subval as $lowkey=>$lowval){
$result.="td class='lowkey'>".$lowkey.":
td
td class='lowvalue'>";
                                     if(gettype($lowval)=="a
ay"){
                                         $result.= "
";
                                         foreach($lowval as $lvkey=>$lvval){
                                             $result.="td class='lowkey'>".$lvkey.":
td
td class='lowvalue'>";
                                             if(gettype($lvval)!="a
ay"){
                                                    $result.=$lvval."
td
t
";
                                                }else{
                                                    $result.="
td
t
";
                                                }
                                         }
                                         $result.="
table>";
                                     }else{
                                         $result.=$lowval."
td
t
";
                                        
                                     }

}
$result.="
table>";
}else{
$result.=$subval."
td
t
";
}
}
$result.="
table>";
}else{
$result.=$val."
td
t
";
}

}
$result.="
table>";
}else{
echo "Not a valid format!";
}
return $result;
}
function processRecursive($data,$level=0,$recursive=false,$key=''){

    
$table='';
if($key != ''){
            $table = '
';
        }else{
            $table = '
';
        }
        
        foreach ($data as $key=>$row) {
            if(!$recursive){
$table .= "td class='key'>".$key.":
td
td class='value'>" ;
            }else{
                
                $table .= "td class='key'>".$key.":
td
td class='value'>" ;
            }
if (is_a
ay($row)) {
                $level =$level + 1;

Recursive mode starts
                 if($key == 'tags' || $key == 'friends'){
$table .= processRecursive($row,$level,true,$key);
                 }else{
                     $table .= processRecursive($row,$level,true);
                 }
                
} else {
                $level = 0;
$table .= $row;
                $table .= "
td
t
";
}
        }

$table .= '
table>';
        
return $table;
}
function upload($filename, $dir){
$uploadOK = true;
$file = $_FILES[$filename]["name"];
$target = $dir.$file;
$fileType = strtolower(pathinfo($target, PATHINFO_EXTENSION));
if($_FILES[$filename]["size"]>5000000){
echo "File is too large! ";
$uploadOK = false;
}
if($fileType!="json"){
echo "Invalide file type! Use '.json' file";
$uploadOK = false;
}
if($uploadOK==false){
echo "File was not uploaded. ";
}else{
if(move_uploaded_file($_FILES[$filename]["tmp_name"], $target)){
echo "The file '".basename($file)."' has been uploaded";
}else{
echo "E
or occured uploading file.";
}
}
return $target;
}
?
website/index.php
?php
$local = true;
$root = $_SERVER["DOCUMENT_ROOT"];
$root .= "/Website";
if ($local == false) {
$root = $_SERVER['CONTEXT_DOCUMENT_ROOT'];
}
$docRoot = "http:
" . $_SERVER['HTTP_HOST'];
if ($local == false) {
$docRoot = "http:
" . $_SERVER['HTTP_HOST'] . "/~ics325su2004/";
}
$docRoot .= "/";
include($root . '/includes/header.php');
include($root . '/includes/menu.php');
include('functions.php');
if (isset($_POST["file"])) {
$dir = "uploads/";
$target = upload("jsonFile", $dir);
$data = file_get_contents($target);
$result_iterative = processData($data);
    $datap = json_decode($data, true);
$result_recursive = processRecursive($datap);
}
?
link href="lab4.css" rel="stylesheet"
form action="index.php" method="post" enctype="multipart/form-data"

div
form
?php
if (isset($_POST['file'])) {
?
Iterative Pattern
div
echo $result_iterative;
?

div

div
Recursive Pattern
div
echo $result_recursive;
                
?

div

div

div
?php
}
include($root . '/includes/footer.php');
?
website/lab4.css
table.a
ayTable {
width: 100%;
border: solid 1px silver;
padding: 5px;
}
td.key {
padding: 5px;
background-color: tomato;
font-weight: bold;
text-align: right;
vertical-align: top;
max-width: 100px;
}
td.value {
padding: 5px;
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here