Bonjour à toute la communauté, je débute avec CodeIgniter et je veux enregistrer le contenu d'un fichier scv dans ma base de données mysql.
voici comment j'ai procédé : Model :
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
<?php
class Queries extends CI_Model {
 
     function __construct() { 
          parent::__construct(); 
 
     }
 
    public function getBiens(){
        $query = $this->db->get('biens');
        if($query->num_rows() >0){
            return $query->result();
        }
    }
 
        public function addBien($data){
          return  $this->db->insert('biens', $data);
        }
    public function getSinglePost($id){
        $query = $this->db->get_where('biens', array('id' => $id));
        if($query->num_rows() >0){
            return $query->row();
        }
    }
 
    public function updateBien($data,$id){
       return $this->db->where('id', $id)
                   ->Update('biens',$data);
    }
 
    public function supBien($id){
    return  $this->db->delete('biens',['id'=>$id]); 
    }
 
}
 
?>
code Contoller :
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
 
 
     function __construct() {
          parent::__construct();
          $this->load->model('queries');
          $this->load->library('csvimport'); 
          } 
 
	public function index()
	{
	    $this->load->model('queries');
        $biens = $this->queries->getBiens();
       /* $bien = 4;
        echo '<pre>';
        print_r($bien);
        print_r($biens);
        echo '</pre>';
        exit(); */
		$this->load->view('welcome_message',['biens'=>$biens]);
 
	}  
...........
...........
// fonction pour lire les données du fichier et les enregistrer dans ma base
 function importcsv() { 
       //chargement du model  
	     $this->load->model('queries');
         //appel à la fonctin getBiens
        $biens = $this->queries->getBiens();
 
            //On initialise l'erreur
 
            $biens['error'] = '';    
 
             //configuration du fichier à uploder
             // le path du fichier
             $config['upload_path'] = './uploads/';
             // le type de fichier
             $config['allowed_types'] = 'csv';
             // taille du fichier
             $config['max_size'] = '1000';
 
             // chargement de la librairie pour le upload
             $this->load->library('upload',$config);
 
             //  S'il y a une erreur au moment du upload
            if (!$this->upload->do_upload()) {
              //on récupère l'erreur dans une variable 
              $error['error'] = $this->upload->display_errors(); 
                // On redurige vers la page d'acccueil avec la variable dans l'url
              $this->load->view('welcome_message',$error); 
 
                  // S'il n'y a pas eu de problème au moment du upload 
                        } else { 
 
                            $file_data = $this->upload->data(); 
                prnt_r($file_data);
                            $file_path =  './uploads/'.$file_data['file_name'];
                            if ($this->csvimport->get_array($file_path)) {
 
                                    $csv_array = $this->csvimport->get_array($file_path); 
                                    foreach ($csv_array as $row){ 
                                    $insert_data = array( 
                                                         'valbiens'=>$row['valbiens'],
                                                          'durbiens'=>$row['durbiens'],
                                                      ); 
                                        $this->queries->addBien($insert_data);
                                            }
 
                                  $this->session->set_flashdata('success','Csv Data Imported Succesfully');
 
                                 return redirect('welcome'); 
                                        //echo "<pre>"; print_r($insert_data); 
                                        } else 
 
                                 $biens['error'] = "Error occured"; 
 
                                   return redirect('welcome');
                            } 
               }
Code de la vue :
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
 
     <DOCTYPE HTML>
    <html>
        <head>
           <title>  Biens  </title>
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
             <!--  cdn pour integrer jquery dans notre appli  -->
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <!--  cdn pour integrer datatable dans notre appli  -->
            <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.css"/>
            <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.js"></script> 
             <!--  ajouter bootstrap avant le script  -->
            <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
            <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" />
             <!--  Ajout du script -->
           <script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
            <link rel="stylesheet" type="text/css" href=<?php echo base_url(); ?>assets/css/style.css>
            <link rel="stylesheet" type="text/css" href=<?php echo base_url(); ?>assets/css/bootstrap.css>
            <link rel="stylesheet" type="text/css" href="style.css"> 
 
 
        </head>
        <body>
        <div class="container-fluid bg-info blochaut xls" style="height: 100px; margin-bottom: 2%"></div>
        <div class="col-md-11">
               <div class="col-md-6" style="height: 100px; margin-bottom: 2%"> 
                   <?php echo anchor('welcome/ajout','Ajouter un bien', ['class'=>'btn btn-primary glyphicon glyphicon-saved']); ?>
                   <br><br>
                       <?php echo form_open('welcome/importcsv',['class'=>'form-horizontal'])?>
                        <input type="file" name="biens">
                        <?php echo form_submit(['name'=>'sumbmit', 'value'=>'Envoyer','class'=>'btn btn-success']);?>               
                       <?php echo form_close()?>
                 </div>
            <div class="">
                 <div><?php //if(isset($error)) echo $error; exit(); 
                     ?></div>
                <?php 
                  if(isset($error)) redirect('welcome',['error'=>$error]);
                ?>
 
                <table class="table table-striped table-hover " style="font-family: Arial; font-size:14;" id="book-table">
                    <thead>
                    <tr>
 
                        <th>VALEUR DU BIEN</th>
                        <th>DUREE D'UTILISATION</th>
                        <th>TAUX AMORTISSEMENT </th>
                        <th>VALEUR DE L'AMORTISSEMENT</th>
                        <th>VALEUR NETTE COMPTABLE </th>
                        <th>DATE D'ENREGISTREMENT</th>
                        <th>TRAITEMENTS </th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php if(count($biens)):  ?>
                        <?php foreach($biens as $bien): ?>
                    <tr style="" >
 
                        <td> <?php 
                              $number = $bien->valbiens;
                              $nombre = number_format($number, 0, ',', ' ');
                            echo  $nombre." "."FCFA"; ?> </td>
                        <td><?php echo  $bien->durbiens ;?></td>
                        <td><?php 
                            $taux = $bien->tauxAmort ;
                            $nombre = number_format($taux, 2, ',', ' ');
                             echo  $nombre." "."%";
                            ?></td>
                        <td><?php
 
                            $valAmort =  $bien->valAmort; 
                             $nombre = number_format( $valAmort, 0, ',', ' ');
                             echo  $nombre." "."FCFA";
                            ?></td>
                        <td><?php 
                            $vnc =  $bien->vnc; 
                            $nombre = number_format( $valAmort, 0, ',', ' ');
                             echo  $nombre." "."FCFA";
                            ?>
                        </td>
                        <td>
                            <?php echo  $bien->date_ajout ;?>
                        </td>
                        <td> 
                           <?php echo anchor("welcome/modifier/{$bien->id}", 'Update', ['class'=>'label label-success ']); ?> 
 
                            <?php echo anchor("welcome/supprimer/{$bien->id}", 'Delete', ['class'=>'label label-danger'],array('class'=>'delete','onclick'=>'return confirm("Voulez vous vraiment supprimer ?")')); ?>
                        </td>
                    </tr>
                    <?php endforeach;  ?>
                    <?php else:   ?>
                    <tr> <td rowspan="7"> PAS DE DONNEES A AFFICHER </td></tr>
                    <?php endif;   ?>
                    </tbody>
                </table>
             </div>
            </div>
          <script language="JavaScript">
                function sure(){
                       return(confirm('Etes-vous sûr de vouloir supprimer cette news ?'));
                }
 
          </script>
 
            <script type="text/javascript">
	         $(document).ready(function() {
	         	//créer une instance de la datatable pour notre tableau
	         $('#book-table').DataTable(
                               {
                        "language": {
                            "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json"
                        }
                    }
 
             );
 
 
	         });
 
 
        </script>
        </body>
    </html>
le problème est que quand j’exécute le programme j'obtient cette erreur : " You did not select a file to upload."

Avez vous une idée de ce qui pourrait être ? ou bien une autre méthode plus simple que ça?