Hello,

I needed to send an email to the user with all records related to that user. I haven't been coding for long. I know this code is working but probably there is a lot of more ways to improve it. It has a lot of if statements that can probably be reduced.

Please let me know what you think.

Thank you,

while($row = mysqli_fetch_array($q) ){


///////SEND MESSAGE AFTER COLLECTING ALL RECORDS 
if($lastis != $row['is_id']){
	if(!empty($msg)){
		$body = 'Hello ' . $teacher . '<br/><br/> Here is the list of reports due by ' . date('F') . ' 15th. <br/><br/>';

		$table =  '<table align="left" cellspacing="2" cellpadding="2" width="750" border="1px dashed black">
		<tr>
			<th align="center" width="35%"><b>Client Name</b></th>
			<th align="center" width="10%"><b>DOB</b></th>
			<th align="center" width="15%"><b>Template</b></th>
			<th align="center" width="10%"><b>Due Date</b></th>
			<th align="center" width="30%"><b>Link</b></th>
		</tr>';

		$endtable = '</table><div style="clear:both"></div>';
		$body .= $table . $msg . $closing . $endtable . ' <br/><br/> Thank you,<br/>Company<br/><br/>';


		$to = $is_email;
		$subject = "Reports Due By " . date('F') . ' 15th';
		$from = "xxxxx";


		//if($is == 19){
			//$ret=runner_mail(array('to' => $to, 'subject' => $subject, 'htmlbody' => $body,  'from'=>$from));
		echo $body;
		//}
	}
	$msg = '';
	$closing = '';
}/////// END SEND MESSAGE


	//////////////PREPARE FOR CLOSING REPORTS IF STATEMENT///////////////
	$dob = strtotime($row['DOB']);
	$fyear = $year - 3;

	$start_date = strtotime($fyear . '-' . $month . '-01');
	$mday = date('t',$start_date);
	$end_date = strtotime($fyear . '-' . $month . '-' . $mday);
	//////////// CLOSING PREPARE END ///////////////////////////////////


	/////////// IFSP REPORTS //////////////////
	if($row['month'] == $month || $row['month'] == $m2 || $row['month'] == $m3 || $row['month'] == $m4){ 


		///////ANNUAL REPORTS//////////
		if($row['month'] == $month){


			//check if adjusted age required 	
			if($row['gest_weeks'] == 0){
				$page = "pre-docs-ca.php";
			}
			else{
				$page = "pre-docs-aj.php";
			}

			////templates
			$template = 'Annual';
			$tem = 'a';
		}

		////////// QUARTERLY REPORTS////////////////
		if($row['month'] == $m2 ){

			if($row['gest_weeks'] == 0){
				$page = "docs1.php";
			}
			else{
				$page = "pre-quart-aj.php";
			}

			$template = 'Quarterly';
			$tem = 'q';
		}

		/////////SEMI-ANNUAL REPORTS//////////////
		if($row['month'] == $m3 ) {

			//check if adjusted age required 	
			if($row['gest_weeks'] == 0){
				$page = "pre-docs-ca.php";
			}
			else{
				$page = "pre-docs-aj.php";
			}

			$template = 'Semi-Annual';
			$tem = 'sa';

		}

		///////QUARTERLY REPORTS///////////////////
		if($row['month'] == $m4 ) {

			if($row['gest_weeks'] == 0){
				$page = "docs1.php";
			}
			else{
				$page = "pre-quart-aj.php";
			}
			$template = 'Quarterly';
			$tem = 'q';
		}

		if( ($dob >= $start_date) && ($dob <= $end_date) ){ 

			//skip one loop to not duplicate on closing 

		} 

		else{
			//////store all records on $msg variable ///////

			$teacher = $row['full_name'];	
			$is_email = $row['is_email'];
			$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

			$msg .= '<tr bgcolor="' . $bg . '">
				<td align="left">' . $row['client_name'] . '</td>
				<td align="left" style="text-align:center">' . format_date($row['DOB']) . '</td>
				<td align="left" style="text-align:center">' . $template . '</td>
				<td align="left" style="text-align:center">' . date('m/').'15/'.date('y') . '</td>
				<td align="left" style="text-align:center"><a href="http://mydomain.com/'.$page.'?cid='.$row['case_no'].'&temp='.$tem.'&rdate='.$r_date.'">Click to Download</a></td>
				</tr>';
		}

		//$sql = "INSERT INTO ies_reports (case_no,client_name,dob,infant_specialist,service_coordinator,report_date,status,template) VALUES ('".$row['case_no']."','".$row['client_name']."','".$row['DOB']."','".$row['infant_specialist']."','".$row['service_coordinator']."','".$r_date."','1','".$tem."')";
		//$query = mysqli_query($conn,$sql)or die(mysqli_error($conn));
		//echo "case inserted ". $row['case_no']. " <br/>";

		//$count++;

	}/////END IF IFSP REPORTS


	//////// CLOSING REPORTS ////////////
	if( ($dob >= $start_date) && ($dob <= $end_date) ){ 


			//prepare due date
			$cdate = strtotime('+3 years', strtotime($row['DOB']));
			$closed_date = date('Y-m-d',$cdate);
			$fdate = date('m/d/y',strtotime('-1 months', strtotime($closed_date)));
			////end due date

			$template = 'Closing';
			$tem = 'c';

			if($row['gest_weeks'] == 0){
				$page = "pre-docs-ca.php";
			}
			else{
				$page = "pre-docs-aj.php";
			}


		$closing .= '<tr bgcolor="ffffff">
			<td align="left">' . $row['client_name'] . '</td>
			<td align="left">' . format_date($row['DOB']) . '</td>
			<td align="left" style="text-align:center">' . $template . '</td>
			<td align="left" style="text-align:center">' . $fdate . '</td>
			<td align="left" style="text-align:center"><a href="http://mydomain.com/'.$page.'?cid='.$row['case_no'].'&temp='.$tem.'&rdate='.$r_date.'">Click to Download</a></td>
			</tr>';				

		//$sql = "INSERT INTO ies_reports (case_no,client_name,dob,infant_specialist,service_coordinator,report_date,status,template) VALUES ('".$row['case_no']."','".$row['client_name']."','".$row['DOB']."','".$row['infant_specialist']."','".$row['service_coordinator']."','".$r_date."','1','".$tem."')";
		//$query = mysqli_query($conn,$sql)or die(mysqli_error($conn));

	}

$is = $row['is_id'];
$lastis = $row['is_id'];//


}

/////SEND LAST EMAIL FROM LOOP
$body = 'Hello ' . $teacher . '<br/><br/> Here is the list of reports due by ' . date('F') . ' 15th. <br/><br/>';

$table =  '<table align="left" cellspacing="2" cellpadding="2" width="750" border="1px dashed black">
<tr>
	<th align="center" width="35%"><b>Client Name</b></th>
	<th align="center" width="10%"><b>DOB</b></th>
	<th align="center" width="15%"><b>Template</b></th>
	<th align="center" width="10%"><b>Due Date</b></th>
	<th align="center" width="30%"><b>Link</b></th>
</tr>';

$endtable = '</table><div style="clear:both"></div>';
$body .= $table . $msg . $closing . $endtable . ' <br/><br/> Thank you,<br/>Company<br/><br/>';	

$to = $is_email;
$subject = "Reports Due By " . date('F') . ' 15th';
$from = "xxxxx";

//$ret=runner_mail(array('to' => $to, 'subject' => $subject, 'htmlbody' => $body,  'from'=>$from));

echo $body;

    I might try to remove some repetitive code (the Don't Repeat Yourself principle). For instance, we could get rid of some repeated comparisons and streamline things a bit with a switch() construct in this section (assuming it meets your functional needs?):

            /////////// IFSP REPORTS //////////////////
            $gest_weeks_is_0 = (isset($row['gest_weeks']) and $row['gest_weeks'] == 0);
            $template = '';
    
        switch($row['month']) {
            ///////ANNUAL REPORTS//////////
            case $month:                
                //check if adjusted age required     
                $page = $gest_weeks_is_0 ? "pre-docs-ca.php" : "pre-docs-aj.php";
                ////templates
                $template = 'Annual';
                $tem = 'a';
                break;
    
            ////////// QUARTERLY REPORTS////////////////
            case $m2:
                $page = $gest_weeks_is_0 ? "docs1.php" : "pre-quart-aj.php";
                $template = 'Quarterly';
                $tem = 'q';
                break;
    
            /////////SEMI-ANNUAL REPORTS//////////////
            case $m3:
                $page = $gest_weeks_is_0 ? "pre-docs-ca.php" : "pre-docs-aj.php";
                $template = 'Semi-Annual';
                $tem = 'sa';
                break;
    
            ///////QUARTERLY REPORTS///////////////////
            case $m4:
                $page = $gest_weeks_is_0 ? "docs1.php" : "pre-quart-aj.php";
                $template = 'Quarterly';
                $tem = 'q';
                break;
    
        }
        if(!empty($template)) { // one of the cases was triggered
            if( ($dob >= $start_date) && ($dob <= $end_date) ){ 
    
                //skip one loop to not duplicate on closing 
    
            } 
    
            else{
                //////store all records on $msg variable ///////
    
                $teacher = $row['full_name'];    
                $is_email = $row['is_email'];
                $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
    
                $msg .= '<tr bgcolor="' . $bg . '">
                    <td align="left">' . $row['client_name'] . '</td>
                    <td align="left" style="text-align:center">' . format_date($row['DOB']) . '</td>
                    <td align="left" style="text-align:center">' . $template . '</td>
                    <td align="left" style="text-align:center">' . date('m/').'15/'.date('y') . '</td>
                    <td align="left" style="text-align:center"><a href="http://mydomain.com/'.$page.'?cid='.$row['case_no'].'&temp='.$tem.'&rdate='.$r_date.'">Click to Download</a></td>
                    </tr>';
            }
        } /////END IF IFSP REPORTS
    

      WOW this is so much better!!! 62 lanes of code were reduced to 34!

        Write a Reply...