-4

这是我的代码,因为我想打印多个 pdf 文件,但问题是我需要使用复选框获取值,例如在导入文件时它显示记录,然后以 pdf 格式打印单个记录,但我想打印整个记录在 1 个 pdf 文件中,带有不同的页面,例如如果我选择 4 复选框,它会在 pdf 中生成 4 页,请查看我的代码,

    <?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

0

您发布的代码为表格的每一行使用一个单独的submit按钮。这只会发送一行进行处理。

要选择多行,您需要在带有单个submit按钮的单个表单中使用复选框。使用数组语法设置复选框的名称(例如 name='cbox[]'),并将复选框的值设置为提交表单时要处理的行的标识。

提交表单时,复选框的值将传递给您的 PHP 脚本,并且可以作为$_POST. 未选中的框将被忽略,因此您可以简单地处理 ID 列表。

下面的代码说明了这个过程

<?php
$records = [
    ['id'=>1,
    'name'=>'John Doe',
    'email'=>'johndoe@example.com',
    'phone'=>'01234 567890'
    ],
    ['id'=>2,
        'name'=>'Jane Doe',
        'email'=>'janedoe@example.com',
        'phone'=>'01234 567890'
    ],
    ['id'=>3,
        'name'=>'John Smith',
        'email'=>'johnsmith@example.com',
        'phone'=>'01234 567890'
    ],
    ['id'=>4,
        'name'=>'Jane Smith',
        'email'=>'janesmith@example.com',
        'phone'=>'01234 567890'
    ]
];

?>
<!doctype html>
<html lang="en-GB">
<head>
    <title>Demo selecting multiple checkboxes</title>
</head>
<body>
<?php
// Check to see if the form has been submitted. If so, extract the IDs from
// $_POST['cbox'] and echo them.
if (isset($_POST['submit'])) {
    foreach($_POST['cbox'] as $cbox) {
        echo "Processing record ID $cbox<br>";
    }
} else {
// No submission yet, so display the table for selection
// Note - just a single form with one submit button
?>
<form method="post">
    <?php
    // Generate table with checkboxes
    echo '<table>';
    foreach($records as $record) {
        echo '<tr>';
        echo "<td>{$record['name']}</td><td>{$record['email']}</td><td>{$record['phone']}</td>";
        // Echo check box here, using array syntax for name and record id for value
        echo '<td><input type="checkbox" name="cbox[]" value="'.$record['id'].'"></td>';
        echo '</tr>';
    }
    echo '</table>';
    ?>
    <input type="submit" name="submit">
</form>
<?php
}
?>
</body>
</html>
于 2021-07-24T03:47:40.123 回答