Search code examples
phpjsondatahandler

PHP Datahandling


I've been searching and trying to find a way to handle data in a smart way with php.

example: {"number":"test","first_name":"test","last_name":"test","company":"tes123321D323","call_result":"test","call_duration":"500","comment":"test"}

I have this json string and I want to get the data from it and echo it in a table. What data is taken form the json is configurable by the user. So lets say The user only wants to display a sum of which companys have called.

I could create a function per time I handle the data differently but I want my code to be short and well it should adapt what kind of string / paramaters it gets.

How should I display this data after I've json decoded it? Whats the best way to display an object multiple times with 1 function

Code

$component_data[0] is a mysql query with multiple json strings.

    if($component_data[0]->datatype !== "text"){
        foreach($component_data as $val){
            $isJira = ($val->datatype == "jirajson") ? true : false;

            $comp_objarr[] = json_decode(trim($val->data));
        }

        if($isJira){
            foreach($comp_objarr as $object){
                $comp_objarrnew[$object->_displayName][] = $object;
            }
        }else{
            $comp_objarrnew[] = $comp_objarr;
        }

        $uid = 0;
        if(isset($_GET['uid'])){
            $uid = $_GET['uid'];
        }

        $var = "";
        $i = 0;
        $i2 = 0;
        echo "<table>";
        foreach($comp_objarrnew as $key => $object){
            if($isJira){
                if($i == $uid){
                    usort($comp_objarr, 'cmp');
                    echo "<tr><th colspan=\"7\">$key</th></tr>";
                    echo "<tr>";
                    foreach($object as $value){
                        $var .= "<tr>";
                        foreach($value as $key => $item){
                            if($i2 == 0){
                                echo "<th>$key</th>";
                            }
                            $var .= "<td>".$item."</th>";
                        }
                        $i2++;
                        if($i2 > 0){
                            echo "</tr>";
                        }
                        $var .= "</tr>";
                    }
                }
                $i++;
            }else{
                foreach($object as $value){
                    if($i == 0){
                        $var .= "<tr>";
                        foreach($value as $key => $item){
                            $var .= "<th>".$key."</th>";
                        }
                        $var .= "</tr>";
                        $i++;
                    }

                    $var .= "<tr>";
                    foreach($value as $item){
                        $var .= "<td>".$item."</td>";
                    }
                    $var .= "</tr>";
                }
            }
        }
        echo $var;
        echo "</table>";
    }else{
        $i = 0;
        $var = "";

        echo "<table>";
        foreach($component_data as $val){
            if($i == 0){
                echo "<tr>";
            }

            $var .= "<tr>";
            foreach($val as $key => $info){
                if($i == 0){
                    echo "<th>$key</th>";
                }
                $var .= "<td>$info</td>";
            }
            $i++;
            $var .= "</tr>";
        }
        echo $var;
        echo "</table>";
    }
}

This is how it's currently being displayed

and this is one of the displays that a user could add:

like grouping the data per day.

Users can add an unlimited amount of displays. such as only display company name group by count company name and so on.

How should I edit my code if I want it to easily display something else? without creating lots of if statements.


Solution

  • Figured it out.

    I used a database and I defined settings for each json string on how to display it:

    Width height length sort by group by.

    Then in my class I made a function to iterate over the settings and check how to display the data.