Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 records from db with the same user id and different claim ids. I want to generate 2 records with different data using array. The problem is the mpdf still appears, but it contains the same data value for these 2 mpdf attachments. How to solve? Please help me.

What I have tried:

PHP
$config['child-page']= 'test.php'; 
if (isset($_GET['action'])){
	if ($_GET['action'] == 'submit') {
		$mySQL2 = 'SELECT A.* FROM table A WHERE A.month = "09-2022" AND A.status = 2 ORDER BY A.id';
		$result2 = mysql_query($mySQL2);
	
		if ($result2){
			if (mysql_num_rows($result2) > 0){
				$upload_dir = './upload/test/';
				$ListAttachment = array();
				ini_set('memory_limit', '256M');
				require_once ('./mpdf/mpdf.php');

				while ($row2 = mysql_fetch_assoc($result2)){	

					$mySQL3 = 'SELECT A.*, B.gender FROM staff_detail A INNER JOIN profile B ON B.id = A.user_id WHERE A.user_id = '.$data['user'].' AND A.company = '.$row2['company'].'  AND A.month = "'.$row2['month'].'" ';
					//echo $mySQL3.'<br/>';
					//exit();
					$result3 = mysql_query($mySQL3);
					if ($result3){
						if (mysql_num_rows($result3) > 0){
							while ($row3 = mysql_fetch_assoc($result3)){
								$ListData[$row3['user_id']][$row3['id']]['gender'] = $row3['gender'];
								$ListData[$row3['user_id']][$row3['id']]['value1'] = $row3['value1'];
								$ListData[$row3['user_id']][$row3['id']]['value2'] = $row3['value2'];

								
								//Get e-Statement Type 
								$ValidProcess = true;
								if (!empty($row3['type'])){
									$ValidProcess = true;
									
									$filename = $ListStatementType[$row3['estatement_type']] . ' ' .
												$ListCompany[$row3['company']] . ' (' . strtoupper($FilterMonth) . ') - ' .
												$row3['staff_ic_name'] . ' (' . $row3['payroll_id'] . ').pdf';
									
									$ListAttachment[$row3['id']] = $filename;
									
									
									if ($row3['type'] == 1){
										if ($ValidProcess){
											
											//FOOTER 		
											$RptFooter .= '<tr>
											<td colspan="6" style="text-align:left; border-left:none; border-right:none; border-bottom:none;font-style:italic;font-size:10px;">
											This is computer generated payslip. No signature is required. 
											</td>
											</tr>';

											////////////////////////////////////Load HTML///////////////////////////////////
											$html = '<html lang="en">
											<head>
											
											<meta name="author" content="" />
											<title></title>
											
											</head>
											<body>';
											include Viewer.$config['child-page'].'-html.php';
											$html .= '
											</body>
											</html>';
											foreach ($ListAttachment as $value){
											$mpdf=new mPDF('UTF-8','A4', 7, '', 7, 7, 15, 50, 7, 7);
											$mpdf->useSubstitution=false;
											$mpdf->SetHTMLFooter($RptFooter);
											$mpdf->SetHTMLHeader($RptHeader);
											$mpdf->WriteHTML($html);
											$mpdf->SetWatermarkImage('./upload/tuah.png'); //watermark
											$mpdf->showWatermarkImage = true; //watermark
											$mpdf->SetProtection(array('print'), $setPassword);
											
											//if (file_exists($upload_dir.$value)){
												$mpdf->Output($upload_dir.$value,'F');
											//}
											}
										}
									}
									
								}
								else{
									$_SESSION[AppName]['sessionmessage']['type'] = 'danger';
									$_SESSION[AppName]['sessionmessage']['message'][] = 'No record.';
								}
							}
						}
					}
				}
			}
			else{
				//status management belum approved
				$_SESSION[AppName]['sessionmessage']['type'] = 'danger';
				$_SESSION[AppName]['sessionmessage']['message'][] = 'No record.';
			}
		}
		else{
			//status management belum approved
			$_SESSION[AppName]['sessionmessage']['type'] = 'danger';
			$_SESSION[AppName]['sessionmessage']['message'][] = 'No record.'; 
		}
		//exit();
	}
}


HTML
foreach ($ListData as $key => $value){
        foreach($value as $key2 => $value2){
                //$i++;
                $html .= '<table class="tabular">';
                $html .= '<tr><td colspan="8" style="text-align:left;border:none;">'.$value2['total1'].'</td></tr>';
$html .= '<tr><td colspan="8" style="text-align:left;border:none;">'.$value2['total2'].'</td></tr>';
       }
}
Posted
Updated 23-Aug-23 7:15am
v4
Comments
Richard Deeming 24-Aug-23 4:13am    
$mySQL3 = 'SELECT A.*, B.gender FROM staff_detail A INNER JOIN profile B ON B.id = A.user_id WHERE A.user_id = '.$data['user'].' AND A.company = '.$row2['company'].'  AND A.month = "'.$row2['month'].'" ';


Your code is almost certainly vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]

1 solution

The problem happens in the loop where you're iterating through the data to generate your PDF records. Since I can't see all of your code, I'll focus on the part that seems relevant where you're building the HTML content for your PDFs. It looks like like you're attempting to populate the HTML content for each record using $value2['total1'] and $value2['total2'], these variables are not present in your code snippet. You might need to adjust this part of your code to correctly reference the data from your $ListData array -
PHP
foreach ($ListData as $user_id => $claims) {
    foreach ($claims as $claim_id => $claim_data) {
        $html .= '<table class="tabular">';
        $html .= '<tr><td colspan="8" style="text-align:left;border:none;">' . $claim_data['value1'] . '</td></tr>';
        $html .= '<tr><td colspan="8" style="text-align:left;border:none;">' . $claim_data['value2'] . '</td></tr>';
        // ...
    }
}


Using proper debugging and output checks can be very helpful in identifying issues like this. You can use var_dump or print_r to inspect the contents of variables and arrays in your code to ensure that the data is being processed correctly.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900