Help with Looping. It's making me LOOPY

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steve745
Forum Newbie
Posts: 2
Joined: Wed Aug 08, 2012 9:59 am

Help with Looping. It's making me LOOPY

Post by steve745 »

I am working on a credit card payoff snowball script. It would be easier to google what that is than to explain it. It works as it should with just having 2 cards return from the sql query, 3 and the loop starts at the top of that section and messes all the figures below. I know and understand what is happening but am way lost on how to correct, workaround, fix or whatever is needed. I could screw it up more that I do know. I'm going to place the section where the problem is happening and a 2nd part with the full 500+ lines of code. Please don't laugh it's messy code it somewhat works and I taught myself how to write it.

Code: Select all

// Working Here on this section with 2 Credit cards works great.  With 3 or more it doesn't loop the code correctly


$result = mysql_query("SELECT * FROM creditors WHERE clientid='$clientid' ORDER BY apr DESC LIMIT $rowtg,1") or die(mysql_error());
 
 $row = mysql_fetch_assoc( $result );
$tinam=$tinam+$row['balance']*$row['apr'];
//echo " First".$prev_pmtsam . " " .$prev_pmtsct ;
echo "<TABLE BORDER='3' width='260' >";
echo "<td>";
echo "<table border='0' font-size: 10pt >";

	echo "<tr><td>"; 
	echo "Creditor " . "</td><td>"  . $row['crname'] . "</td></tr>";
	echo "<tr><td>";
	echo "Phone " . "</td><td> ". $row['crphone'] . "</td></tr>";
	echo "<tr><td>";  
	echo "Date Account was Opened " . "</td><td>  ". $row['dacopen'] . "</td></tr>";
	echo "<tr><td>";
	echo "Available Credit " . "</td><td> ".  $row['avcred'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "Monthly Payment " . "</td><td> ". $row['minpay'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "APR " . "</td><td>". $row['apr'] ."</td></tr>";
	echo "<tr><td>"; 
	echo "Balance " . "</td><td>". $row['balance'] ."</td></tr>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	echo "</table>";
	//$rowtg = $rowtg + 1;
	//$payamt = $row['minpay']; //Monthly Payment
	$counter = 1; //Number of payments
	$bdue=$row['balance'];
	$pcount=$row['tillpaid'];
	//$psplit = round($bdue/$pcount,0) ;
	echo "<table border=\"0\" cellspacing=\"15\">";
	echo "<tr><th><u>Payment  </u></th>";
	echo "<th> <u>Amount </u></th>";
	echo "<th> <u>To</u> <br /> <u>Interest</u></th>";
	echo "<th> <u>To</u> <br /> <u>Principal</u> </th><th> <u>Total</u> <br /><u> Paid</u> </th></tr></tr>";

$balance=$row['balance'];
$topri=0;
//$prev_pmtsam = 0;
	//$prev_pmtsct = 0;
	while ($topri < $row['balance']) {
	
	$totalp=$counter*$row['minpay'];
	$bal=$row['balance']-$topri;
	$iratecalc=$balance * $row['apr'] / 12;
	$topri=$topri+$row['minpay']-round($iratecalc,2);
	$balance= ($iratecalc+$balance)-$row['minpay'] ;
	if($topri + $row['minpay'] - $row['balance'] <= $row['minpay']) {
	echo "<tr><td>";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo date("M-Y","$startdate" );

	echo "</td><td>";
	echo "$".number_format($row['minpay'],2) ;
	echo "</td><td>";
	echo "$".number_format(round($iratecalc,2),2) ;
	echo "</td><td>";
	echo "$".number_format($topri,2) ;
	echo "</td><td>";
	echo "$".number_format($totalp,2) ;
	echo "</td></tr>";
	
	$finpay=$row['balance'] - $topri ;
	$counter = $counter + 1;
	
 }	
 
}

		//$counter = $counter - 1;
echo "</table>";
echo "<b>Final Payment ";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo $counter .": ". date("M-Y","$startdate" );
echo "<br />$";
echo number_format(round($finpay,3),2) ."<br />"." Total Paid  "."$". number_format($totalp+$finpay,2)."<br />" ." Total Paid To Interest "."$". number_format($totalp-$topri,2)  ;
echo "</b><br />";
echo "<br />";
//echo "First Bottom ".$prev_pmtsam+$row['minpay'] . " " .$prev_pmtsct;
$rowtg = $rowtg + 1;

	$prev_pmtsam = $row['minpay'];
	$prev_pmtsct = $counter+1;
$sn_tint=$sn_tint + ($totalp-$topri);
$sn_totp=$sn_totp + ($totalp+$finpay);
$sn_tpri=$sn_tpri + $topri;





echo "<P CLASS=\"breakhere\">";
//echo "2nd top ".$prev_pmtsam . " " .$prev_pmtsct;
$result = mysql_query("SELECT * FROM creditors WHERE clientid='$clientid' ORDER BY apr DESC LIMIT $rowtg, 1 ") or die(mysql_error()); 
 $row = mysql_fetch_assoc( $result );
$tinam=$tinam+$row['balance']*$row['apr'];

if($row['crname'] =="") {goto end;}
echo "<TABLE BORDER='3' width='260' >";
echo "<td>";
echo "<table border='0' font-size: 10pt >";

	echo "<tr><td>"; 
	echo "Creditor " . "</td><td>"  . $row['crname'] . "</td></tr>";
	echo "<tr><td>";
	echo "Phone " . "</td><td> ". $row['crphone'] . "</td></tr>";
	echo "<tr><td>";  
	echo "Date Account was Opened " . "</td><td>  ". $row['dacopen'] . "</td></tr>";
	echo "<tr><td>";
	echo "Available Credit " . "</td><td> ".  $row['avcred'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "Monthly Payment " . "</td><td> ". $row['minpay'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "APR " . "</td><td>". $row['apr'] ."</td></tr>";
	echo "<tr><td>"; 
	echo "Balance " . "</td><td>". $row['balance'] ."</td></tr>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	echo "</table>";
	//$rowtg = $rowtg + 1;
	$payamt = $row['minpay']; //Monthly Payment
	$counter = 1; //Number of payments
	$bdue=$row['balance'];
	$pcount=$row['tillpaid'];
	//$psplit = round($bdue/$pcount,0) ;
	echo "<table border=\"0\" cellspacing=\"15\">";
	echo "<tr><th><u>Payment  </u></th>";
	echo "<th> <u>Payment<br>Amount </u></th>";
	echo "<th> <u>To</u> <br /> <u>Interest</u></th>";
	echo "<th> <u>To</u> <br /> <u>Principal</u> </th><th> <u>Total</u> <br /><u> Paid</u> </th></tr></tr>";
$balance=$row['balance'];
$topr = 0 ;
$ircalc =0;
$fipy=0;
	while ($counter < $prev_pmtsct+1 ) {
	$bal=$row['balance']-$topr;
	
	$totalp=$counter*$row['minpay'];
	$iratecalc=$balance * $row['apr'] / 12;
	$topr=$topr + $row['minpay']-number_format($iratecalc,2) ;
	$balance= ($iratecalc+$balance)-$row['minpay'] ;
	//if($topr + $row['minpay'] - $row['balance'] <= $row['minpay']) {
	echo "<tr><td>";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo date("M-Y","$startdate" );

	echo "</td><td>";
	echo "$".number_format($row['minpay'],2) ;
	echo "</td><td>";
	echo "$".number_format(round($iratecalc,2),2) ;
	echo "</td><td>";
	echo "$".number_format($topr,2) ;
	echo "</td><td>";
	echo "$".number_format($totalp,2) ;
	echo "</td></tr>";
		$paidin=$payamt*$counter-$iratecalc;
		$counter = $counter + 1;
		
		$newct=$bdue/$newpmt;
		$intpd=$intpd+$iratecalc ;
		$pbal=$row['balance']-$paidin;	
		$piplan=$counter*$row['minpay'];
		$tot=$totalp-$topr;
} 		
//$ircalc =$iratecalc;
$iratecalc=0;
//Changed to match no snow figures now gotta fix bottom half figures
	$newpmt = $prev_pmtsam+$row['minpay'];
		$piplan=$piplan;
		$pmtcalc= $prev_pmtsct*$payamt;
		$newterm= $newpmt/$newbal;
		//$ncount=
		$count =0;
		//$counta= $count-$counter;
		$tpaidjp=$newpmt;
		
		while($newpmt < $bal ) {
	//$fcount=1;
		$totalp = $totalp+$newpmt  ;
			$bal =$iratecalc + $bal-$newpmt;	
		$iratecalc= $bal * $row['apr'] / 12 ;
	$topri=$totalp -$iratecalc ;
			//$topri=$topri+$totalpa;
			if($bal-$newpmt > $newpmt) {
	echo "<tr><td>";
$when="+".$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter+$count,1);
	echo date("M-Y","$startdate" );
	echo "</td><td>";
	echo "$".number_format($newpmt,2);
	echo "</td><td>";
	echo "$".number_format($iratecalc,2);
	$piplan=$piplan  ;
	echo "</td><td>";
	echo "$".number_format($topri ,2);
	echo "</td><td>";
	echo "$".number_format($totalp,2);
	echo "</td></tr>";
	$fipy=number_format($row['balance']-$totalp+$iratecalc ,2);
	}
	$finpay=$topri-$row['balance'] ;
$count=$count+1;
}
$tpcnt=$prev_pmtsct+$count +$counter;
	$prev_pmtsct = $count +$counter;
	//$finpay=$pbal;
echo "</table>";
echo "<b>Final Payment ";
$startdate=mktime(0,0,0,$today['mon']+$count+$counter-1,1);
echo $count+$counter .  date("M-Y","$startdate" );
echo "<br />$";
echo number_format(round($finpay,3),2) ."<br />". "Total Paid  "."$".number_format($totalp+$fipy,2) ."<br />". " Total Interest Paid  "."$".number_format($totalp-$row['balance'],2)  ;
echo "</b><br />";
echo "<br />";
end:
$rowtg = $rowtg + 1;
$sn_tint=$sn_tint + ($totalp-$topri+$tot);
$sn_totp=$sn_totp + ($totalp);
$sn_tpri=$sn_tpri + $topri-$tot-$row['minpay'];
//echo $tpcnt;
//echo "2nd Bottom ".$prev_pmtsam+$row['minpay'] . " " .$prev_pmtsct;
	$prev_pmtsam =$prev_pmtsam+ $row['minpay'];
}
Sorry for the mess here is the full page of code

Code: Select all

<STYLE TYPE="text/css">
     P.breakhere {page-break-before: always}
</STYLE>
<h1>America's First Choice's Plan</h1>
<?php
require_once('config.php');
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABASE);

// Get all rows returned
$preresult = mysql_query("SELECT * FROM creditors WHERE clientid='$clientid' ORDER BY apr  ") 
or die(mysql_error());  
$reccount=mysql_num_rows($preresult);
$rowtg  = 0;
if($reccount == 1){
	$frec = 1;
	
// set pre loop variables


$result = mysql_query("SELECT * FROM creditors WHERE clientid='$clientid' ORDER BY ccwrk DESC LIMIT $rowtg,1") or die(mysql_error());
 
 $row = mysql_fetch_assoc( $result );
$tinam=$tinam+$row['balance']*$row['apr'];

echo "<TABLE BORDER='3' width='260' >";
echo "<td>";
echo "<table border='0' font-size: 10pt >";

	echo "<tr><td>"; 
	echo "Creditor " . "</td><td>"  . $row['crname'] . "</td></tr>";
	echo "<tr><td>";
	echo "Phone " . "</td><td> ". $row['crphone'] . "</td></tr>";
	echo "<tr><td>";  
	echo "Date Account was Opened " . "</td><td>  ". $row['dacopen'] . "</td></tr>";
	echo "<tr><td>";
	echo "Available Credit " . "</td><td> ".  $row['avcred'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "Monthly Payment " . "</td><td> ". $row['minpay'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "APR " . "</td><td>". $row['apr'] ."</td></tr>";
	echo "<tr><td>"; 
	echo "Balance " . "</td><td>". $row['balance'] ."</td></tr>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	echo "</table>";
	//$rowtg = $rowtg + 1;
	//$payamt = $row['minpay']; //Monthly Payment
	$counter = 1; //Number of payments
	$bdue=$row['balance'];
	$pcount=$row['tillpaid'];
	//$psplit = round($bdue/$pcount,0) ;
	echo "<table border=\"0\" cellspacing=\"15\">";
	echo "<tr><th><u>Payment  </u></th>";
	echo "<th> <u>Amount </u></th>";
	echo "<th> <u>To</u> <br /> <u>Interest</u></th>";
	echo "<th> <u>To</u> <br /> <u>Principal</u> </th><th> <u>Total</u> <br /><u> Paid</u> </th></tr></tr>";

$balance=$row['balance'];
$topri=0;
//$prev_pmtsam = 0;
	//$prev_pmtsct = 0;
	while ($topri < $row['balance']) {
	
	$totalp=$counter*$row['minpay'];
	$bal=$row['balance']-$topri;
	$iratecalc=$balance * $row['apr'] / 12;
	$topri=$topri+$row['minpay']-round($iratecalc,2);
	$balance= ($iratecalc+$balance)-$row['minpay'] ;
	if($topri + $row['minpay'] - $row['balance'] <= $row['minpay']) {
	echo "<tr><td>";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo date("M-Y","$startdate" );

	echo "</td><td>";
	echo "$".number_format($row['minpay'],2) ;
	echo "</td><td>";
	echo "$".number_format(round($iratecalc,2),2) ;
	echo "</td><td>";
	echo "$".number_format($topri,2) ;
	echo "</td><td>";
	echo "$".number_format($totalp,2) ;
	echo "</td></tr>";
	
	$finpay=$row['balance'] - $topri ;
	$counter = $counter + 1;
	
	$prev_pmtsam = $row['minpay'];
	$prev_pmtsct = $counter-1;


 }	
}
		$counter = $counter - 1;
echo "</table>";
echo "<b>Final Payment ";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo $counter .": ". date("M-Y","$startdate" );
echo "<br />$";
echo number_format(round($finpay,3),2) ."<br />"." Total Paid  "."$". number_format($totalp+$finpay,2)."<br />" ." Total Paid To Interest "."$". number_format($totalp-$topri,2)  ;
echo "</b><br />";
echo "<br />";

$rowtg = $rowtg + 1;





	}
$frec = $reccount - 1;




while($rowtg <= $frec) {
$result = mysql_query("SELECT * FROM creditors WHERE clientid='$clientid' ORDER BY ccwrk DESC LIMIT $rowtg, 1") or die(mysql_error()); 
$row = mysql_fetch_assoc( $result );
//if($row['crname'] = " ") {echo "end";}
//start if statement
 if($row['ccwrk'] == 'yes'){
	
	echo "<TABLE BORDER='3' width='260' >";
echo "<td>";
echo "<table border='0' font-size: 10pt >";

	echo "<tr><td>"; 
	echo "Creditor " . "</td><td>"  . $row['crname'] . "</td></tr>";
	echo "<tr><td>";
	echo "Phone " . "</td><td> ". $row['crphone'] . "</td></tr>";
	echo "<tr><td>";  
	echo "Date Account was Opened " . "</td><td>  ". $row['dacopen'] . "</td></tr>";
	echo "<tr><td>";
	echo "Available Credit " . "</td><td> ".  $row['avcred'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "Monthly Payment " . "</td><td> ". $row['minpay'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "APR " . "</td><td>". $row['apr'] ."</td></tr>";
	echo "<tr><td>"; 
	echo "Balance " . "</td><td>". $row['balance'] ."</td></tr>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	echo "</table>";
	
	$payamt = $row['minpay']; //Monthly Payment
		$counter = 1; //Number of payments
		$bal=$row['balance'];
		$totalp=0;

	//$bdue=$row['balance'];
	//$pcount=$row['tillpaid'];
	//$psplit = round($bdue/$pcount,0) ;
	echo "<table border=\"0\" cellspacing=\"15\">";
	echo "<tr><th><u>Payment  </u></th>";
	echo "<th> <u>Amount </u></th>";
	echo "<th> <u>To</u> <br /> <u>Interest</u></th>";
	echo "<th> <u>To</u> <br /> <u>Principal</u> </th><th> <u>Total</u> <br /><u> Paid</u> </th></tr></tr>";

	while ( $counter <= $row['ccntrm'] ) {
	$totalp=$counter*$row['minpay'];
	$bal=$row['balance']-$totalp;
$iratecalc=$ccnint;
	$topri=$totalp-$iratecalc;
	
	echo "<tr><td>";
$when="+".$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo  date("M-Y","$startdate" );
	echo "</td><td>";
	echo "$".number_format($payamt,2);
	echo "</td><td>";
	echo "$".number_format(round($iratecalc,3),2);
	echo "</td><td>";
	echo "$".number_format($topri,2);
	echo "</td><td>";
	echo "$".number_format($totalp,2);
	echo "</td></tr>";
		//$paidin=$row['minpay']*$counter;
		$counter = $counter + 1;
	$finpaya=$row['balance']-$totalp;
}


$topri=$totalp+($row['minpay']* $row['apr'] / 12);
$balance=$finpaya;

	while ($topri<$row['balance']){
	
	$totalp=$counter*$row['minpay']-round($iratecalc,2);
	$bal=$row['balance']-$totalp;
	$iratecalc=$balance* $row['apr'] / 12;
	
	$balance= ($iratecalc+$balance)-$row['minpay'] ;

echo "<tr><td>";
$when="+".$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo  date("M-Y","$startdate" );
	echo "</td><td>";
	echo "$".number_format($row['minpay'],2);
	echo "</td><td>";

	echo "$".number_format(round($iratecalc,3),2);

	echo "</td><td>";
	
	echo "$".number_format($topri,2);
	echo "</td><td>";
	echo "$".number_format($totalp,2);
	echo "</td></tr>";
	
	$finpay=$row['balance'] -$topri ;
	$counter = $counter + 1;



	
	$prev_pmtsam = $row['minpay'];
	$prev_pmtsct = $counter-1;

$topri=$topri + $row['minpay'] - $iratecalc;


$piplan=$piplan+$topri ;

$rowtg = $rowtg + 1;
 	
}
				
echo "</table>";
echo "<b>Final Payment ";
echo $counter;
$when="start of month,+".$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);


date_default_timezone_set('UTC');

	echo ": ". date("M-Y","$startdate" );

	
	
	
echo "<br />";
echo "$".number_format($finpay,2)."<br />"."Total Paid "."$".number_format(($counter * $row['minpay']),2)."<br />"." Total Paid To Interest "."$". number_format(($counter * $row['minpay'])-$topri,2) ;
echo "</b><br />";
echo "<br />";


$sn_tint= $counter * $row['minpay']-$topri;
$sn_totp=$counter * $row['minpay'];
$sn_tpri=$topri;



$sn_tint=$sn_tint+$totalp-($piplan+$finpaya);
$sn_totp=$sn_totp + ($piplan+$iratecalc+$finpaya);
$sn_tpri=$sn_tpri + $topri;



echo "Total Interest Paid " . number_format($sn_tint,2)."<br />";
echo "Total Principal Paid " . number_format($sn_tpri,2)."<br />";
echo "Total Paid " . number_format($sn_totp,2)."<br />";





goto end;
 }
//end worked cc results



// Working Here on this section with 2 Credit cards works great.  With 3 or more it doesn't loop the code correctly


$result = mysql_query("SELECT * FROM creditors WHERE clientid='$clientid' ORDER BY apr DESC LIMIT $rowtg,1") or die(mysql_error());
 
 $row = mysql_fetch_assoc( $result );
$tinam=$tinam+$row['balance']*$row['apr'];

echo "<TABLE BORDER='3' width='260' >";
echo "<td>";
echo "<table border='0' font-size: 10pt >";

	echo "<tr><td>"; 
	echo "Creditor " . "</td><td>"  . $row['crname'] . "</td></tr>";
	echo "<tr><td>";
	echo "Phone " . "</td><td> ". $row['crphone'] . "</td></tr>";
	echo "<tr><td>";  
	echo "Date Account was Opened " . "</td><td>  ". $row['dacopen'] . "</td></tr>";
	echo "<tr><td>";
	echo "Available Credit " . "</td><td> ".  $row['avcred'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "Monthly Payment " . "</td><td> ". $row['minpay'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "APR " . "</td><td>". $row['apr'] ."</td></tr>";
	echo "<tr><td>"; 
	echo "Balance " . "</td><td>". $row['balance'] ."</td></tr>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	echo "</table>";


	$counter = 1; //Number of payments
	$bdue=$row['balance'];
	$pcount=$row['tillpaid'];

	echo "<table border=\"0\" cellspacing=\"15\">";
	echo "<tr><th><u>Payment  </u></th>";
	echo "<th> <u>Amount </u></th>";
	echo "<th> <u>To</u> <br /> <u>Interest</u></th>";
	echo "<th> <u>To</u> <br /> <u>Principal</u> </th><th> <u>Total</u> <br /><u> Paid</u> </th></tr></tr>";

$balance=$row['balance'];
$topri=0;


	while ($topri < $row['balance']) {
	
	$totalp=$counter*$row['minpay'];
	$bal=$row['balance']-$topri;
	$iratecalc=$balance * $row['apr'] / 12;
	$topri=$topri+$row['minpay']-round($iratecalc,2);
	$balance= ($iratecalc+$balance)-$row['minpay'] ;
	if($topri + $row['minpay'] - $row['balance'] <= $row['minpay']) {
	echo "<tr><td>";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo date("M-Y","$startdate" );

	echo "</td><td>";
	echo "$".number_format($row['minpay'],2) ;
	echo "</td><td>";
	echo "$".number_format(round($iratecalc,2),2) ;
	echo "</td><td>";
	echo "$".number_format($topri,2) ;
	echo "</td><td>";
	echo "$".number_format($totalp,2) ;
	echo "</td></tr>";
	
	$finpay=$row['balance'] - $topri ;
	$counter = $counter + 1;
	
 }	
 
}


echo "</table>";
echo "<b>Final Payment ";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo $counter .": ". date("M-Y","$startdate" );
echo "<br />$";
echo number_format(round($finpay,3),2) ."<br />"." Total Paid  "."$". number_format($totalp+$finpay,2)."<br />" ." Total Paid To Interest "."$". number_format($totalp-$topri,2)  ;
echo "</b><br />";
echo "<br />";

$rowtg = $rowtg + 1;

	$prev_pmtsam = $row['minpay'];
	$prev_pmtsct = $counter+1;
$sn_tint=$sn_tint + ($totalp-$topri);
$sn_totp=$sn_totp + ($totalp+$finpay);
$sn_tpri=$sn_tpri + $topri;





echo "<P CLASS=\"breakhere\">";

$result = mysql_query("SELECT * FROM creditors WHERE clientid='$clientid' ORDER BY apr DESC LIMIT $rowtg, 1 ") or die(mysql_error()); 
 $row = mysql_fetch_assoc( $result );
$tinam=$tinam+$row['balance']*$row['apr'];

if($row['crname'] =="") {goto end;}
echo "<TABLE BORDER='3' width='260' >";
echo "<td>";
echo "<table border='0' font-size: 10pt >";

	echo "<tr><td>"; 
	echo "Creditor " . "</td><td>"  . $row['crname'] . "</td></tr>";
	echo "<tr><td>";
	echo "Phone " . "</td><td> ". $row['crphone'] . "</td></tr>";
	echo "<tr><td>";  
	echo "Date Account was Opened " . "</td><td>  ". $row['dacopen'] . "</td></tr>";
	echo "<tr><td>";
	echo "Available Credit " . "</td><td> ".  $row['avcred'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "Monthly Payment " . "</td><td> ". $row['minpay'] . "</td></tr>";
	echo "<tr><td>"; 
	echo "APR " . "</td><td>". $row['apr'] ."</td></tr>";
	echo "<tr><td>"; 
	echo "Balance " . "</td><td>". $row['balance'] ."</td></tr>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	echo "</table>";

	$payamt = $row['minpay']; //Monthly Payment
	$counter = 1; //Number of payments
	$bdue=$row['balance'];
	$pcount=$row['tillpaid'];

	echo "<table border=\"0\" cellspacing=\"15\">";
	echo "<tr><th><u>Payment  </u></th>";
	echo "<th> <u>Payment<br>Amount </u></th>";
	echo "<th> <u>To</u> <br /> <u>Interest</u></th>";
	echo "<th> <u>To</u> <br /> <u>Principal</u> </th><th> <u>Total</u> <br /><u> Paid</u> </th></tr></tr>";
$balance=$row['balance'];
$topr = 0 ;
$ircalc =0;
$fipy=0;
	while ($counter < $prev_pmtsct+1 ) {
	$bal=$row['balance']-$topr;
	
	$totalp=$counter*$row['minpay'];
	$iratecalc=$balance * $row['apr'] / 12;
	$topr=$topr + $row['minpay']-number_format($iratecalc,2) ;
	$balance= ($iratecalc+$balance)-$row['minpay'] ;
	//if($topr + $row['minpay'] - $row['balance'] <= $row['minpay']) {
	echo "<tr><td>";
$when="+".$count+$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter,1);
	echo date("M-Y","$startdate" );

	echo "</td><td>";
	echo "$".number_format($row['minpay'],2) ;
	echo "</td><td>";
	echo "$".number_format(round($iratecalc,2),2) ;
	echo "</td><td>";
	echo "$".number_format($topr,2) ;
	echo "</td><td>";
	echo "$".number_format($totalp,2) ;
	echo "</td></tr>";
		$paidin=$payamt*$counter-$iratecalc;
		$counter = $counter + 1;
		
		$newct=$bdue/$newpmt;
		$intpd=$intpd+$iratecalc ;
		$pbal=$row['balance']-$paidin;	
		$piplan=$counter*$row['minpay'];
		$tot=$totalp-$topr;
} 		
//$ircalc =$iratecalc;
$iratecalc=0;

	$newpmt = $prev_pmtsam+$row['minpay'];
		$piplan=$piplan;
		$pmtcalc= $prev_pmtsct*$payamt;
		$newterm= $newpmt/$newbal;
		//$ncount=
		$count =0;
		//$counta= $count-$counter;
		$tpaidjp=$newpmt;
		
		while($newpmt < $bal ) {
	//$fcount=1;
		$totalp = $totalp+$newpmt  ;
			$bal =$iratecalc + $bal-$newpmt;	
		$iratecalc= $bal * $row['apr'] / 12 ;
	$topri=$totalp -$iratecalc ;
			//$topri=$topri+$totalpa;
			if($bal-$newpmt > $newpmt) {
	echo "<tr><td>";
$when="+".$counter." month";
$startdate=mktime(0,0,0,$today['mon']+$counter+$count,1);
	echo date("M-Y","$startdate" );
	echo "</td><td>";
	echo "$".number_format($newpmt,2);
	echo "</td><td>";
	echo "$".number_format($iratecalc,2);
	$piplan=$piplan  ;
	echo "</td><td>";
	echo "$".number_format($topri ,2);
	echo "</td><td>";
	echo "$".number_format($totalp,2);
	echo "</td></tr>";
	$fipy=number_format($row['balance']-$totalp+$iratecalc ,2);
	}
	$finpay=$topri-$row['balance'] ;
$count=$count+1;
}
$tpcnt=$prev_pmtsct+$count +$counter;
	$prev_pmtsct = $count +$counter;
	//$finpay=$pbal;
echo "</table>";
echo "<b>Final Payment ";
$startdate=mktime(0,0,0,$today['mon']+$count+$counter-1,1);
echo $count+$counter .  date("M-Y","$startdate" );
echo "<br />$";
echo number_format(round($finpay,3),2) ."<br />". "Total Paid  "."$".number_format($totalp+$fipy,2) ."<br />". " Total Interest Paid  "."$".number_format($totalp-$row['balance'],2)  ;
echo "</b><br />";
echo "<br />";
end:
$rowtg = $rowtg + 1;
$sn_tint=$sn_tint + ($totalp-$topri+$tot);
$sn_totp=$sn_totp + ($totalp);
$sn_tpri=$sn_tpri + $topri-$tot-$row['minpay'];

	$prev_pmtsam =$prev_pmtsam+ $row['minpay'];
}


$isave=round($tinam-($tinam-$tiplan),2);
$debtsaved=$debttotal-$isave;

?>
<P CLASS="breakhere">


I'm really hoping someone can help me with this I know it's a mess.
steve745
Forum Newbie
Posts: 2
Joined: Wed Aug 08, 2012 9:59 am

Re: Help with Looping. It's making me LOOPY

Post by steve745 »

WOW no one can help please. If it will cost me just let me know how much????
Post Reply