• User

    PHP Esportazione DB CSV

    Ciao a tutti,

    Ho creato una pagina in php che mi esporta il DB in csv e funziona perfettamente.

    Il mio problema è che non riesco a fare in modo che me lo esporti a seconda delle province che scelgo tramite un select di un form.

    Le due pagine sono queste:

    Prima:

    [php]
    <?php
    include 'auth.inc.php';

    if ($_SESSION['admin_level'] < 1) {
    header('Refresh: 5; URL=index.php');
    echo '<p><strong></strong>You are not authorized for this page.</strong></p>';
    echo '<p>You are now being redirected to the main page. If your browser ' .
    'doesn't redirect you automatically, <a href="main.php">click ' .
    'here</a>.</p>';
    die();
    }

    include 'db.inc.php';

    $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
    die ('Unable to connect. Check your connection parameters.');
    mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));

    // retrieve information
    $query = 'SELECT
    Lista, Lista_id
    FROM
    Lista
    ORDER BY
    Lista
    ';
    $result = mysql_query($query, $db) or die(mysql_error($db));

    // determine number of rows in returned result
    $num_movies = mysql_num_rows($result);

    $table = <<<ENDHTML
    <div style="text-align: center;">
    <h2>Lista dei contatti</h2>
    <table border="1" cellpadding="2" cellspacing="2"
    style="width: 70%; margin-left: auto; margin-right: auto;">
    ENDHTML;

    // loop through the results
    while ($row = mysql_fetch_assoc($result)) {
    extract($row);

    $table .= <<<ENDHTML
    <tr>
     <td><a href="admin.php?Lista_id=$Lista_id">$Lista</a></td>
     <td><a href="mysql_to_csv.php?Lista_id=$Lista_id">Esporta</a></td>
     <td>
         <form action="mysql_to_csv_provincia.php?Lista_id=$Lista_id" method="POST">
            
            <select name="Provincia"> 
                  <option value="">Seleziona Provincia</option> 
                  <option value="AG">AG</option> 
                  <option value="AL">AL</option> 
                  <option value="AN">AN</option> 
                  <option value="AO">AO</option> 
                  <option value="AP">AP</option> 
                  <option value="AQ">AQ</option> 
                  <option value="AR">AR</option> 
                  <option value="AT">AT</option> 
                  <option value="AV">AV</option> 
                  <option value="BA">BA</option> 
                  <option value="BG">BG</option> 
                  <option value="BI">BI</option> 
                  <option value="BL">BL</option> 
                  <option value="BN">BN</option> 
                  <option value="BO">BO</option> 
                  <option value="BR">BR</option> 
                  <option value="BS">BS</option> 
                  <option value="BT">BT</option> 
                  <option value="BZ">BZ</option> 
                  <option value="CA">CA</option> 
                  <option value="CB">CB</option> 
                  <option value="CE">CE</option> 
                  <option value="CH">CH</option> 
                  <option value="CI">CI</option> 
                  <option value="CL">CL</option> 
                  <option value="CN">CN</option> 
                  <option value="CO">CO</option> 
                  <option value="CR">CR</option> 
                  <option value="CS">CS</option> 
                  <option value="CT">CT</option> 
                  <option value="CZ">CZ</option> 
                  <option value="EN">EN</option> 
                  <option value="FC">FC</option> 
                  <option value="FE">FE</option> 
                  <option value="FG">FG</option> 
                  <option value="FI">FI</option> 
                  <option value="FM">FM</option> 
                  <option value="FR">FR</option> 
                  <option value="GE">GE</option> 
                  <option value="GO">GO</option> 
                  <option value="GR">GR</option> 
                  <option value="IM">IM</option> 
                  <option value="IS">IS</option> 
                  <option value="KR">KR</option> 
                  <option value="LC">LC</option> 
                  <option value="LE">LE</option> 
                  <option value="LI">LI</option> 
                  <option value="LO">LO</option> 
                  <option value="LT">LT</option> 
                  <option value="LU">LU</option> 
                  <option value="MB">MB</option> 
                  <option value="MC">MC</option> 
                  <option value="ME">ME</option> 
                  <option value="MI">MI</option> 
                  <option value="MN">MN</option> 
                  <option value="MO">MO</option> 
                  <option value="MS">MS</option> 
                  <option value="MT">MT</option> 
                  <option value="NA">NA</option> 
                  <option value="NO">NO</option> 
                  <option value="NU">NU</option> 
                  <option value="OG">OG</option> 
                  <option value="OR">OR</option> 
                  <option value="OT">OT</option> 
                  <option value="PA">PA</option> 
                  <option value="PC">PC</option> 
                  <option value="PD">PD</option> 
                  <option value="PE">PE</option> 
                  <option value="PG">PG</option> 
                  <option value="PI">PI</option> 
                  <option value="PL">PL</option> 
                  <option value="PN">PN</option> 
                  <option value="PO">PO</option> 
                  <option value="PR">PR</option> 
                  <option value="PT">PT</option> 
                  <option value="PU">PU</option> 
                  <option value="PV">PV</option> 
                  <option value="PZ">PZ</option> 
                  <option value="RA">RA</option> 
                  <option value="RC">RC</option> 
                  <option value="RE">RE</option> 
                  <option value="RG">RG</option> 
                  <option value="RI">RI</option> 
                  <option value="RM">RM</option> 
                  <option value="RN">RN</option> 
                  <option value="RO">RO</option> 
                  <option value="SA">SA</option> 
                  <option value="SI">SI</option> 
                  <option value="SM">SM</option> 
                  <option value="SO">SO</option> 
                  <option value="SP">SP</option> 
                  <option value="SR">SR</option> 
                  <option value="SS">SS</option> 
                  <option value="SV">SV</option> 
                  <option value="TA">TA</option> 
                  <option value="TE">TE</option> 
                  <option value="TN">TN</option> 
                  <option value="TO">TO</option> 
                  <option value="TP">TP</option> 
                  <option value="TR">TR</option> 
                  <option value="TS">TS</option> 
                  <option value="TV">TV</option> 
                  <option value="UD">UD</option> 
                  <option value="VA">VA</option> 
                  <option value="VB">VB</option> 
                  <option value="VC">VC</option> 
                  <option value="VE">VE</option> 
                  <option value="VI">VI</option> 
                  <option value="VR">VR</option> 
                  <option value="VS">VS</option> 
                  <option value="VT">VT</option> 
                  <option value="VV">VV</option> 
                  <option value="ZA">ZA</option> 
           
        </select>
            
            <input type="submit" name="submit" value="submit" />
         </form>
     </td>
    </tr>
    

    ENDHTML;
    }

    $table .= <<<ENDHTML
    </table>
    <p>$num_movies Liste</p>
    </div>
    <p style="text-align: center;"><a href="home.php">Torna indietro</a></p>
    ENDHTML;

    echo $table;
    ?>
    [/php]Seconda
    [php]
    <?php
    //file: exportcsv.inc.php

    function exportMysqlToCsv($table,$filename = 'export.csv')
    {
    $csv_terminated = "\n";
    $csv_separator = ";";
    $csv_enclosed = '"';
    $csv_escaped = "\";

    $Provincia = $_POST['Provincia'];
    
    $sql_query = 'SELECT Titolo, Ragione_Sociale, Nome_Cognome, Indirizzo, CAP, Localita, Provincia, Telefono, Fax, Cellulare, Email, Sito FROM contatti WHERE Provincia = '. $Provincia . ' AND contatti.Lista_id = ' . $_GET ;
    
    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);
    
    $schema_insert = '';
    
    for ($i = 0; $i < $fields_cnt; $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for
    
    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;
    
    // Format the data
    while ($row = mysql_fetch_array($result))
    {
        $schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++)
        {
            if ($row[$j] == '0' || $row[$j] != '')
            {
    
                if ($csv_enclosed == '')
                {
                    $schema_insert .= $row[$j];
                } else
                {
                    $schema_insert .= $csv_enclosed .
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                }
            } else
            {
                $schema_insert .= '';
            }
    
            if ($j < $fields_cnt - 1)
            {
                $schema_insert .= $csv_separator;
            }
        } // end for
    
        $out .= $schema_insert;
        $out .= $csv_terminated;
        
        $stampa = html_entity_decode($out, ENT_COMPAT, "UTF-8");
        
    } // end while
     echo $Provincia;
    echo $stampa;
    exit;
    

    }
    ?>
    [/php]