-4

複数のpdfファイルを印刷したいので、ここに私のコードがありますが、問題は、ファイルをインポートしてレコードを表示し、単一のレコードをpdfで印刷するときのように、チェックボックスを使用して値を取得する必要があることですが、 4つのチェックボックスを選択すると、pdfで4ページになるように、差分ページを含む1つのpdfファイルにレコード全体が含まれます。私のコードを見てください。

    <?php

    $connect = mysqli_connect("localhost", "root", "", "excel");
    $output = '';
    if(isset($_POST["import"]))
    {
    $extension = end(explode(".", $_FILES["excel"]["name"])); // For getting Extension of selected file

    $allowed_extension = array("xls", "xlsx", "csv"); //allowed extension
    if(in_array($extension, $allowed_extension)) //check selected file extension is present in allowed extension array
    {
    $file = $_FILES["excel"]["tmp_name"]; // getting temporary source of excel file
    include("PHPExcel/IOFactory.php"); // Add PHPExcel Library in this code
    $objPHPExcel = PHPExcel_IOFactory::load($file); // create object of PHPExcel library by using load() method and in load method define path of selected file

    $output .= "<label class='text-success'>Data Inserted</label><br /><table class='table table-bordered'>
    <thead class='thead-dark'>
    <tr>
        <th>Client Name</th>
        <th>Client Email</th>
        <th>Client PHone:</th>
        <th>Action:</th>
        <th>Selection:</th>
    </tr>
    </thead>
    ";
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
    {
    $highestRow = $worksheet->getHighestRow();
    for($row=0; $row<=$highestRow; $row++)
    {
        
        $output .= "<tr>";
        $name = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
        $email = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
        $phone = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(2, $row)->getValue());
        if ($name!='') {
            $sql = "INSERT INTO excel_data(names,mails,phone) VALUES('".$name."', '".$email."','".$phone."')";
            mysqli_query($connect, $sql);
        }
        $datafecth2=mysqli_query($connect,"SELECT * FROM excel_data");
        while ($dd=mysqli_fetch_array($datafecth2)) {
            $recordid = $dd['id'];
        }

        $output .= '<td>'.$name.'</td>';
        $output .= '<td>'.$email.'</td>';
        $output .= '<td>'.$phone.'</td>';

            $output .= "<td> <form action='gen_pdf.php?docid=$recordid' method='post'>
            <button type='submit' name='btn_pdf' class='btn btn-success'>Gen PDF</button>
            </form>
            </td>";
        $output .= "<td><input type='checkbox' class='form-check-input' name='delete[]' value='$recordid' ></td>";
        
        
        $output .= '</tr>';
    }
    } 
    $output .= "<form action='gen_pdf2.php?docid=delete[]' method='post'>
    <button type='submit' name='btn_pdf' class='btn btn-success'>Gen PDF</button>
    </form>";
    $output .= '</table>';
    }
    else
    {
    $output = '<label class="text-danger">Invalid File</label>'; //if non excel file then
    }
    }
    ?>

    <html>
    <head>
    <title>Import Excel to Mysql using PHPExcel in PHP</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <style>
    body
    {
    margin:0;
    padding:0;
    background-color:#f1f1f1;
    }
    .box
    {
    width:700px;
    border:1px solid #ccc;
    background-color:#fff;
    border-radius:5px;
    margin-top:100px;
    }
    
    </style>
    </head>
    <body>
    <div class="container box">
    <h3 align="center">Data Insertion Practice</h3><br />
    <form method="post" enctype="multipart/form-data">
        <label>Select Excel File</label>
        <input type="file" name="excel" />
        <br />
        <input type="submit" name="import" class="btn btn-info" value="Import" />
    </form>
    <br />
    <br />

    <?php
    echo $output;
    echo $output1;
    ?>
    </div>
    <br>
    <br>
    <br>
    </body>
    </html>

以下は、gen_pdf.php のコードです。

              <?php
            $connect = mysqli_connect("localhost", "root", "", "excel");
            require('fpdf/fpdf.php');
            $recordprint=$_GET['docid'];
            if(isset($_POST['btn_pdf'])){

                $pdf = new FPDF('P','mm','A4');
                $pdf->SetFont('arial','b','14');
                $pdf->AddPage();
                $pdf->Cell('25','10','Post ID','1','0','C');
                $pdf->Cell('35','10','Name','1','0','C');
                $pdf->Cell('33','10','Email','1','0','C');
                $pdf->Cell('35','10','Phone','1','0','C');
                $sql=  "SELECT * FROM excel_data WHERE id = '$recordprint'";
                $data = mysqli_query($connect,$sql);
                while ($row = mysqli_fetch_array($data))
                {
                    $pdf->Cell('25','10',$row['id'],'1','0','C');
                    $pdf->Cell('35','10',$row['names'],'1','0','C');
                    $pdf->Cell('33','10',$row['mails'],'1','0','C');
                    $pdf->Cell('35','10',$row['phone'],'1','1','C');
                }
                $pdf->output('');
            }
            ?>
4

1 に答える 1