-4

I need your help with my homework: I'm setting up a project which generates a pdf certificate for a list of student stored on a MySQL database. For the pdf generator, I used fpdf and I have any problem with that. But for the interface I displayed the data on a table and added a button on each row to print data on the pdf.

so what I want is that, when the user click on the button 'print' each data in this row is stored on a variable that I can reuse on the script that provide the pdf.

while($res = mysqli_fetch_array($result)) {         
    echo "<tr>";
    echo "<td>".$res['ID']."</td>";
        echo "<td>".$res['Name']."</td>";
        echo "<td>".$res['Parcours']."</td>";
    echo "<td>".$res['Appreciation']."</td>";
    echo "<td>".$res['SingDate']."</td>";
    echo "<td><input type='submit' id='print' class='btn btn-outline-info waves-effect' value='Print'/></td>";
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

Put the ID in the value of the submit button. Then the script that you can submit to can get the value of the button that they used to submit the form.

echo "<td><input type='submit' name='printID' class='btn btn-outline-info waves-effect' value='{$res['ID']}'/></td>";

In the form's action script, use $_POST['printID'] or $_GET['printID'] to get the ID that they selected to print. It can look up the rest of the information about the student in the database using this ID.

$stmt = $mysqli->prepare("SELECT Name, Parcours, Appreciation
    FROM students
    WHERE ID = ?");
$stmt->bind_param("i", $_GET['printID']);
$stmt->execute();
$stmt->bind_result($name, $parcours, $appreciation);
$stmt->fetch();

$pdf->addPage('L'); 
$pdf->useImportedPage($pageId, 10, 10); 
$pdf->SetTextColor(54, 90, 168); 
$pdf->SetFont('Times','i',18); 
$pdf->Text(38,95.6, $name); 
$pdf->Text(38,135.2, $parcours); 
$pdf->Text(106,175.3, $appreciation);

Replace students with the actual name of your table.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank You that is helpfull, I begin to see sharp on this.And What should I do if i want to have the value of each data on tha row on my new script. Because I want to use them one by one to place them in a specified position in to the PDF. – Andrainiony Apr 24 '19 at 12:59
  • Do you mean you want to keep this page open so you can print multiple certicates for different students? – Barmar Apr 24 '19 at 13:02
  • $pdf->addPage('L'); $pdf->useImportedPage($pageId, 10, 10); $pdf->SetTextColor(54, 90, 168); $pdf->SetFont('Times','i',18); $pdf->Text(38,95.6, $_GET('name')); $pdf->Text(38,135.2, $_GET('Parcours')); $pdf->Text(106,175.3, $_GET('Appreciation')); I want to do something like this on the scipt that provide pdf – Andrainiony Apr 24 '19 at 13:10
  • The script should use `$_GET['printID']` to get the ID of the student, and then look up the rest of the data in the database. – Barmar Apr 24 '19 at 13:12
  • I just want to print one certificat for each student(i.e) each row. and I want to place each information in the right place – Andrainiony Apr 24 '19 at 13:13
  • Thank you, I'm trying to edit my code, and I still have some error. i'll send feedback here soon – Andrainiony Apr 24 '19 at 14:10
  • Warning: I don't know anything about `fpdf`. I just copied what you wrote in the comment, and put in my variables. – Barmar Apr 24 '19 at 14:18
  • "Call to a member function bind_param() on a non-object " I have this err message. I don't know how to solve this – Andrainiony Apr 25 '19 at 04:31
  • The whole point of this site is to provide a library of answers to common questions like that, did you try searching for it to find out? – Barmar Apr 25 '19 at 15:49
  • Did you replace the table name `students` with the actual name of your table. I assumed it would be obvious that I had to make an assumption, and you have to change the query to match your actual circumstances. – Barmar Apr 25 '19 at 15:50
  • I have replaced every variable and the query – Andrainiony Apr 26 '19 at 07:14
  • It's work now! The err was in my query. Thank you very much – Andrainiony Apr 29 '19 at 06:42