dabadee
dabadee

Reputation: 149

adding several json arrays and filtering them according to menu elements clicks

Edit: I've tried to follow the instructions and apparently everytime a query is executed a new array is added, but, when i generate the JSON file, the second array contains the values of the first array and those of the second. What's wrong with this?

Here's my problem: i have to solve two problems:

1) I have to create a JSON file with PHP consisting of a succession of several arrays, like in the following example:

{
    "data": {
        "cliente": [
            {
                "id": "Sentra",
                "nome": 4,
                "cognome": "Sentra",
                "indirizzo": 4,
                "email": "Sentra",
                "tipo_contatto": 4,
                "telefono_1": "Sentra",
                "telefono_2": 4,
                "telefono_3": "Sentra"
            }
        ],
        "accettazione": [
            {
                "id": "Sentra",
                "nome": 4,
                "cognome": "Sentra",
                "indirizzo": 4,
                "email": "Sentra",
                "tipo_contatto": 4,
                "telefono_1": "Sentra",
                "telefono_2": 4,
                "telefono_3": "Sentra"
            },                
            {
                "id": "Taurus",
                "data": 4,
                "id_operatore": "Taurus",
                "dispositivo": 4,
                "note": "Taurus",
                "password": 4,
                "tipo_avviso": "Taurus",
                "id_cliente": 4,
                "difetto_dichiarato": "Taurus",
                "codice": 4
            }
        ]
    }
}

Here i have two arrays, but i need to have loads of them, anyway, in my PHP page, here's what i have:

<?php

session_start();

include_once('UniversalConnect.php');

class Login
{

    private $hookUp;
    private $sql;
    private $sql2;
    private $sql3;
    private $sql4;

    public function __construct()
    {
        $this->hookUp = UniversalConnect::doConnect();

        $this->sql = "SELECT * FROM cliente";
        $this->sql2 = "SELECT * FROM accettazione";
        $this->sql3 = "SELECT * FROM riparazione";
        $this->sql4 = "SELECT * FROM operatore";

        // Tabella cliente

        $response = array();
        $data = array();

        if ($result = $this->hookUp->query($this->sql)) {
            while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
                echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['nome'] . "</td>";
                echo "<td>" . $row['cognome'] . "</td>";
                echo "<td>" . $row['indirizzo'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
                echo "<td>" . $row['tipo_contatto'] . "</td>";
                echo "<td>" . $row['telefono_1'] . "</td>";
                echo "<td>" . $row['telefono_2'] . "</td>";
                echo "<td>" . $row['telefono_3'] . "</td>";
                echo "</tr>";

                $data[] = array("id" => $row['id'], "nome" => $row['nome'], "cognome" => $row['cognome'], "indirizzo" => $row['indirizzo'], "email" => $row['email'], "tipo_contatto" => $row['tipo_contatto'],
                    "telefono_1" => $row['telefono_1'], "telefono_2" => $row['telefono_2'], "telefono_3" => $row['telefono_3']);

                $response['cliente'] = $data;

                $fp = fopen("cliente.json", "w");
                fwrite($fp,json_encode($response));
                fclose($fp);
            }
        }

        // Tabella accettazione

        if ($rs = $this->hookUp->query($this->sql2)) {
            while ($rw = mysqli_fetch_array($rs, MYSQLI_BOTH)) {
                echo "<tr>";
                echo "<td>" . $rw['id'] . "</td>";
                echo "<td>" . $rw['data'] . "</td>";
                echo "<td>" . $rw['id_operatore'] . "</td>";
                echo "<td>" . $rw['dispositivo'] . "</td>";
                echo "<td>" . $rw['note'] . "</td>";
                echo "<td>" . $rw['password'] . "</td>";
                echo "<td>" . $rw['tipo_avviso'] . "</td>";
                echo "<td>" . $rw['id_cliente'] . "</td>";
                echo "<td>" . $rw['difetto_dichiarato'] . "</td>";
                echo "<td>" . $rw['codice'] . "</td>";
                echo "</tr>";

                $data[] = array("id" => $rw['id'], "data" => $rw['data'], "id_operatore" => $rw['id_operatore'], "dispositivo" => $rw['dispositivo'], "note" => $rw['note'], "password" => $rw['password'],
                    "tipo_avviso" => $rw['tipo_avviso'], "id_cliente" => $rw['id_cliente'], "difetto_dichiarato" => $rw['difetto_dichiarato'], "codice" => $rw['codice']);

                $response['accettazione'] = $data;

                $file = file_get_contents("cliente.json");
                $data = json_decode($file);
                unset($file);
                file_put_contents("cliente.json", json_encode($data));
                unset($data);

                $fp = fopen("cliente.json", "w");
                fwrite($fp,json_encode($response));
                fclose($fp);
            }
        }
    }
}

$worker = new Login();

?>

The file executes four queries. In the first i create the json file, and the first query creates the first array, then with the second i create the second, and the second, "accettazione", has to become the second array separated by a comma on the first one, and so on...

Unfortunately, when the second query is executed, the second array becomes the first array, it doesn't become the second after the first one. I know how to push new objects to an existing array, but that's not the situation.

2) The second question is about how this data should be generated in my page:

In the following code, when i click on a "a" tag, the "linked" piece of code (i don't attach it here, but it's an html div), the code is shown (it's an empty table), which has to host the JSON data. As you can see from the example, tmp_div is a sort of array which indicates the content (div 0, div 1, div 2, div3).

The idea is that, when I click on tmp_div 0, json.data.cliente have to be shown, and the same for the others, but i want to do it dinamically, not everytime specifying different cases. How do i have to edit the code to reach the goal?

<!--Funzione di selezione della voce del menu e dell'apparizione dei relativi dati-->

<script type="text/javascript">

    $('.sidebar-menu a').click(function (e) {
        hideContentDivs();
        var tmp_div = $(this).parent().index();
        $(".main-sections div").eq(tmp_div).show();

        $.getJSON("lista.json", function(json){

            if (tmp_div == 0) {
                var data = json.data.cliente;

                for (var i = 0; i < data.length; i++) {
                    var object = data[i];
                    for (property in object) {
                        var value = object[property];
                        console.log(property + "=" + value);
                    }
                }
            } else if (tmp_div == 1) {

                var data = json.data.accettazione;

                for (var i = 0; i < data.length; i++) {
                    var object = data[i];
                    for (property in object) {
                        var value = object[property];
                        console.log(property + "=" + value);
                    }
                }
            } else if (tmp_div == 2) {
                var data = json.data.riparazione;

                for (var i = 0; i < data.length; i++) {
                    var object = data[i];
                    for (property in object) {
                        var value = object[property];
                        console.log(property + "=" + value);
                    }
                }
            } else if (tmp_div == 3) {
                var data = json.data.operatore;

                for (var i = 0; i < data.length; i++) {
                    var object = data[i];
                    for (property in object) {
                        var value = object[property];
                        console.log(property + "=" + value);
                    }
                }
            }
        });


    });

    function hideContentDivs() {
        $(".main-sections div").each(function () {
            $(this).hide();
        });
    }

    hideContentDivs();

</script>

Upvotes: 1

Views: 67

Answers (1)

Muhammad Abdul-Rahim
Muhammad Abdul-Rahim

Reputation: 2010

To answer your first question:

Unfortunately, when the second query is executed, the second array becomes the first array, it doesn't become the second after the first one. I know how to push new objects to an existing array, but that's not the situation.

Right now you're calling $data = array() in your loop. You're overwriting the existing array each iteration of the loop. Because of that, $data[] = ... will always push to element 0, since it's pushing onto an empty array.

Furthermore, you're doing this: $response['cliente'] = $data; which means you will always rewrite $response with whatever $data is. This is not pushing new objects onto an existing array; you are always overwriting your arrays.

To answer your second question:

This is more of a design question in the end. You have a lot of possible answers. A very quick thing you can do for now would probably be make a function, but that's just a small step forward. You may have better advice in Code Review instead.

Upvotes: 3

Related Questions