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 »
The expert can work on this till 8.00AM Thursday, 11th 20 Indian standard time.I have included the requirement in the attached zip folder. This is a php and very light html coding


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 editor I used “XAMPP control Panel v3.2.4” running “Apache” and “MySQL” as server to run index.php Do not worry 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 DayJun 10, 2021

Answer To: Write a php recursive function to display the content of “sample.json” file in a table The file...

Sanghamitra answered on Jun 11 2021
142 Votes
website/.~lock.recursive.docx#
,LAPTOP-I8ULTUQN/HP,,10.06.2020 17:53,file:///C:/Users/HP/AppData/Roaming/LibreOffice/4;
website/Capture.JPG
website/functions.php
function processData($data){
$dataArray = json_decode($data, true);
if(is_array($dataArray)){
$result = "";
foreach($dataArray as $key=>$val){
$result.="";
}

}
$result.="
".$key.": ";
if(gettype($val)=="array"){
$result.= "";
foreach($val as $subkey=>$subval){
$result.="";
}
}
$result.="
".$subkey.": ";
if(gettype($subval)=="array"){
$result.= "";
foreach($subval as $lowkey=>$lowval){
$result.="";
                                        
                                     }

}
$result.="
".$lowkey.": ";
                                     if(gettype($lowval)=="array"){
                                         $result.= "";
                                         foreach($lowval as $lvkey=>$lvval){
                                             $result.="";
                                                }else{
                                                    $result.="";
                                                }
                                         }
                                         $result.="
".$lvkey.": ";
                                             if(gettype($lvval)!="array"){
                                                    $result.=$lvval."
";
                                     }else{
                                         $result.=$lowval."
";
}else{
$result.=$subval."
";
}else{
$result.=$val."
";
}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 .= "";
}
        }

$table .= '
".$key.":" ;
            }else{
                
                $table .= "
".$key.":" ;
            }
if (is_array($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 .= "
';
        
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 "Error occured uploading file.";
}
}
return $target;
}
?>
website/index.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);
}
?>







if (isset($_POST['file'])) {
?>


Iterative Pattern


echo $result_iterative;
?>



Recursive Pattern


echo $result_recursive;
                
?>



}
include($root . '/includes/footer.php');
?>
website/lab4.css
table.arrayTable {
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