Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    2

    Estrazione file excel da codice php

    Buongiorno a tutti!
    Ho un db e da questo db devo estrarre n file excel tanti quanti sono i campi differenti di una determinata colonna del db(codice_sgu).
    Da codice che vi posterò, il file viene estratto correttamente ma il problema è che i dati che ci sono al suo interno corrispondono all'intero contenuto del db (tutti i record).

    Posto il codice del mio progetto.

    Spero che mi potrete dare una mano!

    Per qualsiasi chiarimento contattatemi.

    Grazie.


    <?php


    error_reporting(E_ALL);


    $link = mysql_connect('localhost', 'root', '') or die ('not correct');


    mysql_select_db("05_fwul0100", $link);


    $query = "select count(distinct(codice_sgu)) from utenze";


    $result = mysql_query($query, $link) or die('Errore...');

    //conto il numero di occorrenze trovate nel db
    $numrows = mysql_num_rows($result);
    //se il database è vuoto lo stampo a video

    if ($numrows==0)
    {
    echo "Database vuoto!";
    }

    //Se invece trovo delle occorrenze...
    else
    {
    //Avvio un ciclo for che si ripete per il numero di occorrenze trovate
    for($x=0; $x<$numrows; $x++)
    {
    //Recupero il contenuto di ogni record rovato
    $resrow = mysql_fetch_row($result);
    $n = $resrow[0];

    echo"il risultato e': $n";
    }


    }



    $query = "select distinct(codice_sgu),descrizione from utenze";

    $result = mysql_query($query, $link) or die('Errore...');

    //conto il numero di occorrenze trovate nel db
    $numrows = mysql_num_rows($result);
    //se il database è vuoto lo stampo a video



    $sgu=array();

    if ($numrows==0)
    {
    echo "Database vuoto!";
    }

    //Se invece trovo delle occorrenze...
    else
    {


    echo"<TABLE>";

    echo"<tr style=height:30>
    <th><h3>PK</h3>
    <th><h3>ID_NOMINATIVO</h3>
    <th><h3>ID_INDIRIZZO</h3>
    <th><h3>ID_REGIONE</h3>
    <th><h3>ID_TIPO_NOMINATIVO</h3>
    <th><h3>NUMERO_LINEE</h3>
    <th><h3>CODICE NOMINATIVO</h3>
    <th><h3>COGNOME</h3>
    <th><h3>NOME</h3>
    <th><h3>PARTITA IVA</h3>
    <th><h3>DATA_NASCITA</h3>
    <th><h3>CATEGORIA_MERCEOLOGICA</h3>
    <th><h3>PAROLE_ESPLICATIVE</h3>
    <th><h3>CONSENSO_FW_TRATTAMENTO_DATI</h3>
    <th><h3>PROVINCIA</h3>
    <th><h3>CAP</h3>
    <th><h3>COMUNE</h3>
    <th><h3>TOPONIMO</h3>
    <th><h3>VIA</h3>
    <th><h3>CIVICO</h3>
    <th><h3>CODICE_SGU</h3>
    <th><h3>DESCRIZIONE</h3>
    <th><h3>CONTATTO</h3>
    <th><h3>FLAG_CONTATTABILITA'_TELEFONICA</h3>
    <th><h3>FLAG_PB_WF</h3>
    <th><h3>FLAG_CONSENSO_DBU</h3>
    <th><h3>FLAF_CONSENSO_AMR</h3>
    <th><h3>FLAF_CONSENSO_EDP</h3>
    <th><h3>FLAF_CONSENSO_FW</h3>
    <th><h3>VENDIBILITA'_FIBRA_RES</h3>
    <th><h3>VENDIBILITA'_DSL_RES</h3>
    <th><h3>VENDIBILITA'_D2P_RES</h3>
    <th><h3>VENDIBILITA'_WHS_RES</h3>
    <th><h3>VENDIBILITA'_FIBRA_SHP</h3>
    <th><h3>VENDIBILITA'_DSL_SHP</h3>
    <th><h3>VENDIBILITA'_WSP_SHP</h3>
    <th><h3>DATA_SCADENZA_VINCOLO_ALTRO_GESTORE</h3>
    <th><h3>TECNOLOGIA</h3>
    <th><h3>AREA_RACCOLTA</h3>
    <th><h3>MINIPOP</h3>
    <th><h3>AREA_RESIDENZIALE</h3>
    <th><h3>CODICE_OPERATORE</h3>
    <th><h3>DESCRIZIONE_CAMPAGNA</h3>
    <th><h3>DATA_VALIDITA'_DA</h3>
    <th><h3>DATA_VALIDITA'_A</h3>
    <th><h3>TIPOLOGIA_CAMPAGNA</h3>
    <th><h3>NOME_LISTA</h3>
    <th><h3>BATCH_NUMERICO_INCREMENTALE</h3>
    <th><h3>LOAD_DATE</h3></tr>";
    //Avvio un ciclo for che si ripete per il numero di occorrenze trovate
    for($x=0; $x<$n; $x++)

    {
    //creazione dell array per memorizzare i codici sgu
    $g = mysql_fetch_row($result);
    $sgu[]=$g[0];
    //echo "x: [$x]
    ";
    echo "
    sgu: $sgu[$x]
    ";



    $cur = mysql_query ("SELECT * from utenze where codice_sgu = $sgu[$x]");

    //echo "cur : $cur";



    while ($riga = mysql_fetch_row($cur))




    {

    header ("Content-Type: application/vnd.ms-excel");
    header ("Content-Disposition: inline; filename=$g[0]/$g[1].xls");
    echo"<TR><td>$riga[0]<td>$riga[1]<td>$riga[2]<td>$riga[3]<td>$riga[4]<td>$riga[5]<td>$riga[6]<td>$riga[7]<td>$riga[8]<td>$riga[9]<td>$riga[10]<td>$riga[11]
    <td>$riga[12]<td>$riga[13]<td>$riga[14]<td>$riga[15]<td>$riga[16]<td>$riga[17]<td>$riga[18]<td>$riga[19]<td>$riga[20]<td>$riga[21]<td>$riga[22]<td>$riga[23]
    <td>$riga[24]<td>$riga[25]<td>$riga[26]<td>$riga[27]<td>$riga[28]<td>$riga[29]<td>$riga[30]<td>$riga[31]<td>$riga[32]<td>$riga[33]<td>$riga[34]<td>$riga[35]
    <td>$riga[36]<td>$riga[37]<td>$riga[38]<td>$riga[39]<td>$riga[40]<td>$riga[41]<td>$riga[42]<td>$riga[43]<td>$riga[44]<td>$riga[45]<td>$riga[46]<td>$riga[47]
    <td>$riga[48]<td>$riga[49]<td></TR>";



    }



    }

    echo"</TABLE>";

    }




    mysql_close($link);
    ?>

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    128
    ma già il primo

    $numrows = mysql_num_rows($result);

    ti dà un numero errato? hai provato a fare echo $numrows; per vedere quante ne tira fuori?

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    2
    grazie diabolik per la risp.

    Cmq ho provato a fare un echo e mi dà come risultato 1.

    Aspetto tue notizie!

    Grazie ciao.

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    128
    ok quindi la query è giusta

    questo codice

    Codice PHP:
    $query "select count(distinct(codice_sgu)) from utenze";


    $result mysql_query($query$link) or die('Errore...');

    //conto il numero di occorrenze trovate nel db
    $numrows mysql_num_rows($result);
    //se il database è vuoto lo stampo a video

    if ($numrows==0)
    {
    echo 
    "Database vuoto!";
    }

    //Se invece trovo delle occorrenze...
    else
    {
    //Avvio un ciclo for che si ripete per il numero di occorrenze trovate
    for($x=0$x<$numrows$x++)
    {
    //Recupero il contenuto di ogni record rovato
    $resrow mysql_fetch_row($result);
    $n $resrow[0];

    echo
    "il risultato e': $n";

    in realtà ha un solo scopo, ovvero quello di dirti quanti valori diversi ci sono nella colonna. Se la query è giusta, $numrows sarà sempre 1, è inutile fare un ciclo. Molto meglio fare

    Codice PHP:
    $query mysql_query("select count(distinct(codice_sgu)) as numero from utenze");
    $resrow mysql_fetch_array($query);
    $n resrow["numero"
    PS: per favore, utilizza i tag php per scrivere del codice, così risulta più leggibile

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.