Regioni province comuni 2016

spacca!  0 scarso!  1 versione stampa invia per email scheda utente totale visite  682
Finalmente mi sono deciso ed ho aggiornato la mia vecchia tabella dei comuni.  Ho utilizzato i dati Istat, aggiornati al 1 luglio 2016 per creare le tabelle.
Per le select questa volta ho preferito sfruttare la struttura json per popolare le option.

Per il momento butto qui il codice e l'esempio
<html>
    <head>
        <title>Select Regioni Province Comuni</title>
        <meta charset="UTF-8">
        <script src="../js/jquery-2.0.3.min.js"></script>
        <script>
            $(document).ready(function () {
                query("regioni");
                $("select").change(function () {
                    switch ($(this).attr('name')) {
                        case "regioni":
                            empty("comuni");
                            empty("province");
                            query("province");
                            break;
                        case "province":
                            empty("comuni");
                            query("comuni");
                            break;
                        case "comuni":
                            break;
                    }
                });
            });
            function empty(name) {
                $("select[name='" + name + "']").find('option').not(":first").remove();
            }
            function query(name) {
                $.getJSON("comuni_istat.php", $("#ricerca").serialize(), function (response) {
                    $.each(response[name], function (i, item) {
                        $("select[name='" + name + "']").append($('<option>', {
                            value: item.code,
                            text: item.name
                        }));
                    });
                });
            }
        </script>
    </head>
    <body>
        <form id="ricerca">
            <select name="regioni">
                <option value="">-</option>
            </select>
            <select name="province">
                <option value="">-</option>
            </select>
            <select name="comuni">
                <option value="">-</option>
            </select>
        </form>
    </body>
</html>

<?php

$isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
if (!$isAjax) {
    $user_error = 'Access denied - not an AJAX request...';
    trigger_error($user_error, E_USER_ERROR);
}

$dataIstat = "2016-07-01";
$sql = new mysqli("localhost", "root", 'password', 'database');

$response = array(
    "regioni" => array(),
    "province" => array(),
    "comuni" => array()
);
if (empty($_REQUEST['regioni'])) {
    $query = "SELECT * FROM istat_regioni "
            . "WHERE aggiornamentoIstat = '" . $dataIstat . "' "
            . "ORDER BY nomeRegione";
    $result = $sql->query($query);
    while ($row = $result->fetch_assoc()) {
        $response['regioni'][] = array(
            "name" => utf8_encode($row['nomeRegione']),
            "code" => $row['codiceRegione']
        );
    }
} else {
    $query = "SELECT * FROM istat_province "
            . "WHERE aggiornamentoIstat = '" . $dataIstat . "' "
            . "&& codiceRegione = " . (int) $_REQUEST['regioni'] . " "
            . "ORDER BY nomeProvincia";
    $result = $sql->query($query);
    while ($row = $result->fetch_assoc()) {
        $response['province'][] = array(
            "name" => utf8_encode($row['nomeProvincia']),
            "code" => $row['codiceProvincia']
        );
    }
}
if (!empty($_REQUEST['province'])) {
    $query = "SELECT * FROM istat_comuni "
            . "WHERE aggiornamentoIstat = '" . $dataIstat . "' "
            . "&& codiceProvincia = " . (int) $_REQUEST['province'] . " "
            . "ORDER BY denominazioneIta";
    $result = $sql->query($query);
    while ($row = $result->fetch_assoc()) {
        $response['comuni'][] = array(
            "name" => utf8_encode($row['denominazioneIta']),
            "code" => $row['codiceIstat']
        );
    }
}
echo json_encode($response);
 
 

Inserisci un commento

non sei loggato

CAPTCHA