php read csv in codeigniter 3

19 Views Asked by At
public function processCsv($year = NULL, $month = NULL)
{
    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'csv';
    $config['max_size']      = 1024; // Tamaño máximo del archivo en kilobytes (1 MB)

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('userfile')) {
        // Si la carga falla, muestra un mensaje de error o realiza una acción apropiada
        $error = array('error' => $this->upload->display_errors());
        $this->session->set_flashdata('danger', 'Error al cargar el archivo CSV.');
    } else {
        // Si la carga es exitosa, procesa el archivo CSV aquí
        $upload_data = $this->upload->data();
        $file_path = $upload_data['full_path'];

        // Abre el archivo CSV cargado para procesarlo
        $fp = fopen($file_path, "r");
        $readingData = false;

        // Variables para rastrear las filas y columnas
        $currentRow = 0;
        $columnBData = array();

        $idMapping = array(
            'Werner'    => 31,
            'Pedro'     => 35,
            'Ray'       => 29,
            'Jussep'    => 40,
            'Eyleen'    => 28,
            'Jarlyn'    => 41,
            'Roger'     => 27
        );

        $id = array();

        // Verifica si los valores de año y mes se proporcionaron en la URL
        if (is_null($year) || is_null($month)) {
            // Si no se proporcionaron, usa los valores predeterminados (año y mes actuales)
            $year = date('Y');
            $month = date('m');
        }

        while ($data = fgetcsv($fp, 10000, ";")) {
            $currentRow++;

            // Leer datos de la columna B desde la fila 5 a la fila 11
            if ($currentRow >= 5 && $currentRow <= 11   ) {
                $columnBData = $data;

                unset($columnBData[0]);
                unset($columnBData[1]);
                unset($columnBData[2]);
                // A partir de aqui se mapea cada dia segun disponibilidad del mes

                $employeeName = array_keys($idMapping)[$currentRow - 5];
                $employeeId = $idMapping[$employeeName];

                // Mapeamos todos los keys del arreglo para matchearlos con el dia del mes
                $newArray = array();
                $index = 1;

                foreach ($columnBData as $value) {
                    if(!empty($value))
                    {
                        $newArray[$index] = $value;
                        $index++;
                    }
                }

         

i have this function whos help me to upload the csv in the table and chargue the people whos getting and horario update

<?php /* Vamos a sacar una table con todos los días del mes */ ?>

    <div class="block">
            <table class="table table-condensed table-bordered">
                <tr>
                    <td class="text-center" colspan="2">Semanas</td>
                    <?php
                    // Go through every day of this month
                    for($i=1;$i<=$total_days;$i++)
                    {
                        echo '<td class="text-center">' . $i . '</td>';
                    }
                    ?>
                </tr>
                <tr>
                    <td class="text-center" colspan="2">Días</td>
                    <?php
                    // Go through every day of this month
                    for($i2=1;$i2<=$total_days;$i2++)
                    {
                        $fecha = date($year . '-' . $month . '-' . $i2);
                        $day = date('N', strtotime($fecha));
                        $dia = '';
                        $dia_bg = '';
                        switch($day)
                        {
                            case 1:
                            $dia = 'L';
                            break;
                            case 2:
                            $dia = 'M';
                            break;
                            case 3:
                            $dia = 'MI';
                            break;
                            case 4:
                            $dia = 'J';
                            break;
                            case 5:
                            $dia = 'V';
                            break;
                            case 6:
                            $dia = 'S';
                            $dia_bg = 'bg-danger text-white';
                            break;
                            case 7:
                            $dia = 'D';
                            $dia_bg = 'bg-danger text-white';
                            break;
                        }
                        echo '<td class="text-center ' . $dia_bg . '">' . $dia . '</td>';
                    }
                    ?>
                </tr>
                <?php /* Ahora vamos con cada miembro de OCC */ ?>
                <tr>
                    <td>Werner</td>
                    <td class="text-center">1</td>
                    <?php
                    // Go through every day of this month
                    for($i3=1;$i3<=$total_days;$i3++)
                    {
                        $dia = date($year . '-' . $month . '-' . $i3);
                        $occ_id = 31; // Werner es el 31
                        $turno_bg = '';
                        // Ahora, en cada tabla, hay que buscar en la db si hay un registro para este dia y este occ
                        $turno = $this->OCC->get_occ_turn($occ_id, $dia);
                        if(!$turno)
                        {
                            $turno = '<a href="javascript:void(0)" data-bs-toggle="modal" data-bs-target="#modals" data-bs-remote="' . site_url('occ/assign_hours/' . $occ_id . '/' . $dia) . '"><i class="fa fa-plus"></i></a>';
                        }
                        else
                        {
                            switch($turno)
                            {
                                case 'A':
                                $turno_bg = 'bg-turno-a';
                                break;
                                case 'AB':
                                $turno_bg = 'bg-turno-ab';
                                break;
                                case 'B':
                                $turno_bg = 'bg-turno-b';
                                break;
                                case 'C':
                                $turno_bg = 'bg-turno-c';
                                break;
                                case 'O':
                                $turno_bg = 'bg-day-off';
                                break;
                            }

                            $turno = '<a href="javascript:void(0)" data-bs-toggle="modal" data-bs-target="#modals" data-bs-remote="' . site_url('occ/assign_hours/' . $occ_id . '/' . $dia) . '" class="text-white">' . $turno . '</a>';
                        }
                        echo '<td class="text-center ' . $turno_bg . '">' . $turno . '</td>';
                    }
                    ?>
                </tr>
                           
This the table

enter image description here i need to read the year and the month to publish into the table.

can someone help me with this?

i can read the csv without the mes and año, i need to read the number in the mes and the año to upload and save the data from the csv according to the mes in there

0

There are 0 best solutions below