Jump to content

How to insert PHP and HTML mixed code to php variable for Mpdf printing ?


realeez

Recommended Posts

Dear Friends,

I am alot before doing the mixed content of php and HTML in single variable in PHP.. 

I could not get the data even though there is not SYNTAX error.. 

please look my code and advise

 

$htmlcontent .="
<tr>
	<td colspan=\"8\">".$details."></td>
</tr>";
	for($k=0;$k<count($reg_years[$details]);$k++) {
			$year =  (int)($reg_years[$details][$k]);
			$singlecount[$year] = array_filter($result[$details],function($details1) use ($year){	
			return ($details1['reg_year'] == $year && $details1['bench_type'] == 1);
		});
		$divisioncount[$year] = array_filter($result[$details],function($details2) use ($year){
			return ($details2['reg_year'] == $year && $details2['bench_type'] == 2);
		});
		$fullcount[$year] = array_filter($result[$details],function($details3) use ($year){
			return ($details3['reg_year'] == $year && $details3['bench_type'] >= 3);
		}); 
		$rpcount[$year] = array_filter($result[$details],function($rp) use ($year){	
			return ($rp['reg_year'] == $year && $rp['bench_type'] == 'RP');
		}); 
		$mjccount[$year] = array_filter($result[$details],function($mjc) use ($year){	
			return ($mjc['reg_year'] == $year && $mjc['bench_type'] == 'MJC');
		}); 
		$cocount[$year] = array_filter($result[$details],function($co) use ($year){	
			return ($co['reg_year'] == $year && $co['bench_type'] == 'X');
		}); 
		$total = 0;
		$total = (int)(count($singlecount[$year])+count($divisioncount[$year])+count($fullcount[$year])+count($rpcount[$year])+count($mjccount[$year])+count($cocount[$year]));
		
			
		
		$htmlcontent .="
		<tr>
			<td>".$year."</td>
			<td align=\"center\"> 
			      if(count($singlecount[$year])>0) {
					echo (count($singlecount[$year]));
				  }
				  else {
					echo "-";
				  }	
					
			</td>
			<td align=\"center\">
			      if(count($divisioncount[$year])>0) {
					".count($divisioncount[$year])."
				  } else {
					"-"
				  }	
				</td>
				<td align=\"center\">
if (count($fullcount[$year]) > 0)
{
    echo (count($fullcount[$year]));
}
else
{
    echo "-";
} </td > < td align =\"center\">
   					if(count($rpcount[$year])>0)  { 
   					 echo (count($rpcount[$year]));
					
            		 } else { echo " - "; }  
            	</td>
							
				<td align=\"center\">
				 if(count($mjccount[$year])>0)  { 
   					 echo (count($mjccount[$year])); 
				  } else { echo " - "; }  
                </td>
							
			<td align=\"center\"> 
					 if(count($cocount[$year])>0)  { 
   					 echo (count($cocount[$year])); 
			
            		 } else { echo " - "; }  
			</td>
			<td align=\"center\"> 
					
   					echo $total; 
            		
			</td> 			
		</tr>";

}
}

$htmlcontent .= "</tbody></table>";

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($htmlcontent);
$mpdf->Output();

Waiting for FAST reply

 

Thanks

Anes

Link to comment
Share on other sites

image.png.2d76fa4117a83cf470d8c98328ba6be8.png

You can't embed php code in the middle of a string like that. You could do...

$htmlcontent .="
        <tr>
            <td>".$year."</td>
            <td align=\"center\">
        ";
             
if(count($singlecount[$year])>0) {
    $htmlcontent .= (count($singlecount[$year]));
}
else {
    $htmlcontent .=  "-";
}    
                    
$htmlcontent .=  "</td>";

// etc

 

Edited by Barand
Link to comment
Share on other sites

the Ternary Operator would produce shorter code and can be concatenated directly inside the string being built.

you can also change any escaped double-quotes \" to single-quotes ' and put php variables directly into strings without using concatenation, simplifying the syntax.

slightly off topic, you are repeating code 6 times, once for every bench_type value/range. there's at least two problems with this - 1) it is taking you a long time to produce code, and now that you are making a change to the code, you must make the change in 6 places, and 2) to add a new value will require that you find all the code using the values and add to it, then re-test the code to make sure there were no mistakes. instead of a hard-code approach, of writing out code for each value, repeated by the number of values, you need to use a dynamic processing approach, where you will organize/preprocess the data, then simply loop over the data to produce the output.

it would take knowing what the overall data looks like to specifically help, but if you index(pivot) the data using the details, year, and bench_type, as array indexes, when you retrieve the data, you can probably reduce all the above to three nested foreach(){} loops, that will 'automatically' adjust if any new bench_type values are added to the data.

if you do post an example of your $reg_years data, please use var_export() so that the data can be used for testing.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.