IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

traiter un fichier CSV en PHP


Sujet :

PHP & Base de données

  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2021
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2021
    Messages : 2
    Par défaut traiter un fichier CSV en PHP
    Bonjour à tous, j'ai besoin d'aide concernant un projet, je souhaiterais traiter un fichier CSV: lire ligne par ligne et ranger les lignes contenant une information n°2 identique dans une même variable. Le but étant de d'avoir un site web avec un volet de données sur le coté et à chaque clic dessus, une nouvelle information s'affiche, je ne sais pas du tout comment m'y prendre. De plus si vous pensez que le php n'est pas le meilleur langage pour cela merci de me faire part de vos idées.
    Merci de votre aide.

  2. #2
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 563
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 563
    Par défaut
    montrez nous le code que vous avez essayé.

  3. #3
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2021
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Avril 2021
    Messages : 2
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    <?php
    // Load the database configuration file
    include_once 'config.php';
     
    // Get status message
    if(!empty($_GET['status'])){
        switch($_GET['status']){
            case 'succ':
                $statusType = 'alert-success';
                $statusMsg = 'data has been imported successfully.';
                break;
            case 'err':
                $statusType = 'alert-danger';
                $statusMsg = 'Some problem occurred, please try again.';
                break;
            case 'invalid_file':
                $statusType = 'alert-danger';
                $statusMsg = 'Please upload a valid CSV file.';
                break;
            default:
                $statusType = '';
                $statusMsg = '';
        }
    }
    ?>
     
    <!-- Display status message -->
    <?php if(!empty($statusMsg)){ ?>
    <div class="col-xs-12">
        <div class="alert <?php echo $statusType; ?>"><?php echo $statusMsg; ?></div>
    </div>
    <?php } ?>
     
    <div class="row">
        <!-- Import link -->
        <div class="col-md-12 head">
            <div class="float-right">
                <a href="javascript:void(0);" class="btn btn-success" onclick="formToggle('importFrm');"><i class="plus"></i> Import</a>
            </div>
        </div>
        <!-- CSV file upload form -->
        <div class="col-md-12" id="importFrm" style="display: none;">
            <form action="importData.php" method="post" enctype="multipart/form-data">
                <input type="file" name="file" />
                <input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT">
            </form>
        </div>
     
        <!-- Data list table --> 
        <table class="table table-striped table-bordered">
            <thead class="thead-dark">
                <tr>
                    <th>#ID</th>
                    <th>Tache</th>
                    <th>UG</th>
                    <th>Noeud</th>
                    <th>Session</th>
                    <th>Version Session</th>
                    <th>Uproc</th>
                    <th>Date début</th>
                    <th>Date fin</th>
                    <th>Durée moy verif cond</th>
                    <th>Durée moy exe</th>
                </tr>
            </thead>
            <tbody>
            <?php
            // Get rows
            $result = $db->query("SELECT * FROM test1 ORDER BY id DESC");
            if($result->num_rows > 0){
                while($row = $result->fetch_assoc()){
            ?>
                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo $row['tache']; ?></td>
                    <td><?php echo $row['UG']; ?></td>
                    <td><?php echo $row['noeud']; ?></td>
                    <td><?php echo $row['session']; ?></td>
                    <td><?php echo $row['versionsession']; ?></td>
                    <td><?php echo $row['uproc']; ?></td>
                    <td><?php echo $row['datedeb']; ?></td>
                    <td><?php echo $row['datefin']; ?></td>
                    <td><?php echo $row['durmoycond']; ?></td>
                    <td><?php echo $row['durmoyexe']; ?></td>
                </tr>
            <?php } }else{ ?>
                <tr><td colspan="5">Rien de detecte...</td></tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
     
    <!-- Show/hide CSV upload form -->
    <script>
    function formToggle(ID){
        var element = document.getElementById(ID);
        if(element.style.display === "none"){
            element.style.display = "block";
        }else{
            element.style.display = "none";
        }
    }
    </script>
    deuxieme programme:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    <?php
    // Load the database configuration file
    include_once 'config.php';
     
    if(isset($_POST['importSubmit'])){
     
        // Allowed mime types
        $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
     
        // Validate whether selected file is a CSV file
        if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $csvMimes)){
     
            // If the file is uploaded
            if(is_uploaded_file($_FILES['file']['tmp_name'])){
     
                // Open uploaded CSV file with read-only mode
                $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
     
                // Skip the first line
                fgetcsv($csvFile);
     
                // Parse data from CSV file line by line
                while(($line = fgetcsv($csvFile)) !== FALSE){
                    // Get row data
                    $tache   = $line[0];
                    $UG  = $line[1];
                    $noeud  = $line[2];
                    $session = $line[3];
                    $versionsession = $line[4];
                    $uproc = $line[5];
                    $datedeb = $line[6];
                    $datefin = $line[7];
                    $durmoycond = $line[8];
                    $durmoyexe = $line[9];
     
                    // Check whether name already exists in the database with the same email
                    $prevQuery = "SELECT id FROM test1 WHERE UG = '".$line[1]."'";
                    $prevResult = $db->query($prevQuery);
     
                    // if($prevResult->num_rows > 0){
                        // Update member data in the database
                        // $db->query("UPDATE test1 SET tache = '".$tache."', UG = '".$UG."', noeud = '".$noeud."', session = '".$session."', versionsession = '".$versionsession."', uproc = '".$uproc."', datedeb = '".$datedeb."', datefin = '".$datefin."', durmoycond = '".$durmoycond."', durmoyexe = '".$durmoyexe."'");
                    // }else{
                        // Insert test1 data in the database
                        $db->query("INSERT INTO test1 (tache, UG, noeud, session, versionsession, uproc, datedeb, datefin, durmoycond, durmoyexe) VALUES ('".$tache."', '".$UG."', '".$noeud."', '".$session."', '".$versionsession."', '".$uproc."', '".$datedeb."' , '".$datefin."' , '".$durmoycond."', '".$durmoyexe."')");
                    // }
                }
     
                // Close opened CSV file
                fclose($csvFile);
     
                $qstring = '?status=succ';
            }else{
                $qstring = '?status=err';
            }
        }else{
            $qstring = '?status=invalid_file';
        }
    }
     
    // Redirect to the listing page
    header("Location: index.php".$qstring);

  4. #4
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 563
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 563
    Par défaut
    d'après ce code, vous chargez un fichier CSV dans une table et ensuite vous affichez le contenu de la table. donc du point de vue algorithmique ça m'a l'air très bien..
    est ce qu'ensuite le code ne fonctionne pas comme vous voulez à un moment ?

Discussions similaires

  1. Traiter fichier CSV en PHP
    Par Ploss dans le forum Langage
    Réponses: 10
    Dernier message: 08/01/2010, 12h52
  2. Réponses: 1
    Dernier message: 10/01/2008, 13h52
  3. [CSV] Créer un fichier csv depuis php
    Par jbidou88 dans le forum Langage
    Réponses: 5
    Dernier message: 07/05/2007, 17h41
  4. Réponses: 2
    Dernier message: 13/03/2007, 11h19
  5. [CSV] Fichier CSV en PHP
    Par rimeh dans le forum Langage
    Réponses: 1
    Dernier message: 11/01/2007, 12h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo