mardi 20 décembre 2022

Combining foreach and while in PHP

I need help, my code below does not show me all the rows that come from the database. I have 8 rows but they show me only 6 rows. if I try to change the break to put it a little lower, it displays all the rows except that it repeats the same file in the checkbox.

<?php
ob_start();
include('_TEMPLATE_/header.inc.php');
?>
<?php  

    if(isset($_POST['download'])){
        $file_to_remove = glob("downloads/*.zip");
        foreach ($file_to_remove as $file) {
            unlink($file);
        }

        if(!isset($_POST['theme-files'])){
            $error = "<div class='alert alert-danger' role='alert'>Attention, Vous devez au moins selectionner un fichier pour télécharger !!!</div>";
        }else{
            $themes = $_POST['theme-files'];

            $characters = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz";
            $string_length = 4;

            $random_characters = substr(str_shuffle($characters), 0, $string_length);
            $zip_name = "Cvs_{$random_characters}.zip";

            $zip_name = "Cvs.zip";
        
            $zip = new ZipArchive();
            $zip->open("downloads/{$zip_name}", ZipArchive::CREATE);
            foreach ($themes as $file) {
                $zip->addFile($file, basename($file));
            }
            $zip->close();
            if(! headers_sent()){
                header("location: downloads/$zip_name");
                ob_end_flush();
            }else{
                echo'<script type="text/javascript">window.location.href="downloads/'.$zip_name.'"</script>';
            }
            

            //echo'<script> location.replace("downloads/'.$zip_name.'"); </script>';
            echo'<script type="text/javascript">window.location.href="viviers"</script>';
        }
    }
?>
<!-- Loader -->
<div id="preloader">
    <div id="status">
        <div class="spinner">
            <i class="ri-loader-line spin-icon"></i>
        </div>
    </div>
</div>
<div class="main-content">
    <div class="page-content">
        <div class="container-fluid">
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                        <h4 class="mb-sm-0"></h4>
                            <div class="page-title-right">
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item"><a href="javascript: void(0);">Nazox</a></li>
                                <li class="breadcrumb-item active">Viviers
                                </li>
                            </ol>
                        </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- end page title -->
            <!-- start page title -->
            <div class="row">
                <div class="col-lg-12">
                <div class="line__separator"></div>
                    <div class="card">
                    <h6 class="card-header"><?php echo $title; ?> | Viviers</h6>
                        <div class="card-body">
                            <div class="data__separator"></div>
                            <div class="dataTables_wrapper dt-bootstrap4 no-footer">
                            <form action="" method="post" enctype="multipart/form-data">
                                <table id="scroll-vertical-datatable" class="table dt-responsive nowrap w-100">
                                    <thead class="thead-light">
                                    <div>
                                    <button type="submit" class="btn btn-success" id="submit_prog" name="download"><i class="ri-download-fill mb-2"></i> Télécharger le CV</button>
                                    </div>
                                    <p class="error"></p>
                                        <?php echo @$error; ?>
                                        <tr>
                                            <th>
                                                Selectionner
                                            </th>
                                            <th>ID</th>
                                            <th>Date vivier</th>
                                            <th>Expérience</th>
                                            <th>Diplôme</th>
                                            <th>Prénom</th>
                                            <th>Nom</th>
                                            <th>Postnom</th>
                                            <th>Actions</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $query = "SELECT `apply`.`id`, `apply`.`sexe`, `apply`.`prenom`, `apply`.`nom`, `apply`.`postnom`, `apply`.`tel`, `apply`.`email`, `apply`.`datetime`, `apply`.`cv`, `apply`.`lettre`, `apply`.`id_offre`, `apply`.`status`, `apply`.`years`, `apply`.`diplome`, `postes`.`title` FROM `apply` JOIN `postes` ON `apply`.`id_offre` = `postes`.`id` ORDER BY `apply`.`id` DESC";
                                        //$query = "SELECT * FROM `apply`";
                                        $run_query = mysqli_query($conn, $query);
                                        $count = mysqli_num_rows($run_query);
                                        if($count < 1){
                                            echo '<tr>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td>
                                            <div class="nothing">
                                            <div class="alert alert-danger" role="alert">
                                            <i class="mdi mdi-alert-outline me-2"></i> Votre vivier est encore vide, revenez plus tard !!!
                                            </div></div></td>
                                            ';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '</tr>'; 
                                        }else{
                                        $i = 1;                                       
                                        while ($row = mysqli_fetch_assoc($run_query)) {
                                            $files = glob("uploads/cvs/*");
                                            foreach ($files as $theme_file){
                                                $id = $row['id'];
                                                //$user_id = $row['id'];
                                                $sexe = ucfirst($row['sexe']);
                                                $datetime = $row['datetime'];
                                                $date = date("d/m/Y", strtotime($datetime));
                                                $prenom = ucfirst($row['prenom']);
                                                $nom = ucfirst($row['nom']);
                                                $postnom = ucfirst($row['postnom']);
                                                $cv = $row['cv'];
                                                $lettre = $row['lettre'];
                                                $poste = ucfirst($row['title']);
                                                $status = $row['status'];
                                                $years = $row['years'];
                                                $diplome = ucfirst($row['diplome']);
                                                
                                        ?>
                                            <tr>                                               
                                                <td>
                                                <input type="checkbox" name="theme-files[]" value="<?php echo $theme_file; ?>">                                          
                                                </td>                                 
                                                <td><?php echo $i; ?></td>
                                                <td><?php echo $date; ?></td>
                                                <td><?php echo $years; ?> an(s)</td>
                                                <td>
                                                <span class="badge bg-info">
                                                    <?php echo $diplome; ?>
                                                </span>
                                                </td>
                                                <td>
                                                    <?php
                                                    if ($status == '1') {

                                                        echo '<span class="badge bg-info float-end mt-1"><i class="ri-lock-2-line"></i></span>';
                                                    }
                                                    ?>
                                                    <?php echo $prenom; ?>
                                                </td>
                                                <td><?php echo $nom; ?></td>
                                                <td><?php echo $postnom; ?></td>
                                                <td>
                                                <a onclick="cvIinfo(<?php echo $id; ?>)" id="<?php echo $id; ?>" class="userinfo view_cv" href="javascript:void(0)"><i class="mdi mdi-magnify-scan font-size-18"></i></a>
                                                
                                                <?php
                                                if(!empty($lettre)){
                                                    echo '
                                                    <a href="uploads/lettres/'.$lettre.'" class="me-3 text-primary" data-bs-toggle="tooltip" data-bs-placement="top" title="Lettre de motivation">
                                                    <i class="mdi mdi-paperclip font-size-18"></i></a></td>';
                                                } ?>
                                                
                                                </td>
                                            </tr>
                                        </div> 
                                        <?php $i++; ?>
                                        <?php } ?>

                                        <?php } ?>   
                                        <?php break;?>                                  
                                        <?php } ?>                                       
                                    </tbody>
                                </table>
                            </form>
                            </div>
                        </div>
                    </div>
                </div> <!-- end col -->
            </div> <!-- end row -->
            <!-- end row -->
        </div>
    </div>
    <!-- End Page-content -->
    <footer class="footer">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-6">
                    <script>
                        document.write(new Date().getFullYear())
                    </script> © <?php echo $copyright; ?>.
                </div>
                <div class="col-sm-6">
                    <div class="text-sm-end d-none d-sm-block">
                    Crafted with <i class="mdi mdi-heart text-danger"></i> | Version <?php echo $version; ?>
                    </div>
                </div>
            </div>
        </div>
    </footer>

</div>
<!-- end main content-->

</div>
<!-- END layout-wrapper -->

<!-- JAVASCRIPT -->
<script src="assets/libs/jquery/jquery.min.js"></script>
<script src="assets/js/timeout.js"></script>
<script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/libs/metismenu/metisMenu.min.js"></script>
<script src="assets/libs/simplebar/simplebar.min.js"></script>
<script src="assets/libs/node-waves/waves.min.js"></script>

<!-- Required datatable js -->
<script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="assets/libs/datatables.net-bs4/js/dataTables.bootstrap4.min.js"></script>
<!-- Buttons examples -->
<script src="assets/libs/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
<script src="assets/libs/datatables.net-buttons-bs4/js/buttons.bootstrap4.min.js"></script>
<script src="assets/libs/jszip/jszip.min.js"></script>
<script src="assets/libs/pdfmake/build/pdfmake.min.js"></script>
<script src="assets/libs/pdfmake/build/vfs_fonts.js"></script>
<script src="assets/libs/datatables.net-buttons/js/buttons.html5.min.js"></script>
<script src="assets/libs/datatables.net-buttons/js/buttons.print.min.js"></script>
<script src="assets/libs/datatables.net-buttons/js/buttons.colVis.min.js"></script>

<script src="assets/libs/datatables.net-keytable/js/dataTables.keyTable.min.js"></script>
<script src="assets/libs/datatables.net-select/js/dataTables.select.min.js"></script>

<!-- Responsive examples -->
<script src="assets/libs/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
<script src="assets/libs/datatables.net-responsive-bs4/js/responsive.bootstrap4.min.js"></script>

<!-- Datatable init js -->
<script src="assets/js/pages/datatables.init.js"></script>

<script src="assets/js/app.js"></script>


<script src="https://unpkg.com/pdfobject@2.2.8/pdfobject.min.js"></script>
</body>

</html>

<script>
    function cvIinfo(id){
        $.ajax({
            url: 'backend/view_modal.php',
            type: 'post',
            data: {userid: id},
            success: function(response){ 
                $('#show__modal').html(response); 
                $('#empModal').modal('show'); 
            }
        });
    }

    function save_note(id){
     
     var get_note = $('#get_note').val();
     $.ajax({
        url:'backend/update_note.php',
        type:'post',
        data:{get_note: get_note, id:id},
        success: function(response){
            if(response == 'success'){
                $('#result_note').css("color", "green").html('Mise à jour réussie...');
                return true;
            }else{
                $('#result_note').css("color", "red").html(response);
                return false;
            }
        }
     })
    }
    
</script>

<script>
$(document).ready(function() {

var $submit = $("#submit_prog").hide(),
    $cbs = $('input[name="theme-files[]"]').click(function() {
        $submit.toggle( $cbs.is(":checked") );
    });

});
</script>

<div id="show__modal"></div>

I would like each checbox to have its own file and on the other side to show me all the rows




Aucun commentaire:

Enregistrer un commentaire