Jump to content

While Loop Is Repeating Its Self !


hopbop

Recommended Posts

ok so i commented out the area i have working now im trying to loop that to make a report and it's working but it's repeating the report about 20 times ...... im abit lost on how this is happaning and how to fix it any help would rock

 

 

<?php
$ecss=$_GET['ecs']; $date1=$_GET['date1']; $date2=$_GET['date2'];
require '../config.php';
mysql_select_db("learn_sym");

$result = mysql_query("SELECT agency FROM clients WHERE ecs= '$ecss' and date<= '$date2' and date>= '$date1'") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
    $agencyname= $row{'agency'};


    $result2 = mysql_query("SELECT * FROM clients WHERE ecs= '$ecss' and date<= '$date2' and date>= '$date1' AND agency= '$agencyname' ORDER BY date ASC") or die(mysql_error());
    echo"<ul style='width:800px; margin:0 auto 0;'>
    <h4 style='margin:0 10px 0; padding:0;'>"; if (!empty($agencyname)){echo$agencyname;} else {echo"Unknown";} echo"</h4>";
    while ($row2 = mysql_fetch_array($result2))
    {
    echo"<p style='margin:4px 0 0 15px;'>
        <span class='blue'>Name:</span> ".$row2['fname'].",".$row2['lname']." -
        <span class='blue'>Seen On:</span> ".$row2['date']."  
        <span class='blue'>Email:</span> ";if(!empty($row2['email'])){echo $row2['email'];}else{echo"N/A";}echo"  
        <span class='blue'>Phonenumber:</span> ";if(!empty($row2['cell_phone'])){echo $row2['cell_phone'];}else{echo $row2['home_phone'];}echo"
    </p>
    <p style='margin:4px 0 0 15px; background-color:#f3f3f2;'>
        <span class='blue'>Services: </span>";
        if (!empty($row2['want_serv_1'])){echo"FASFSA Completion, ";}
        if (!empty($row2['want_serv_4'])){echo"Admissions Application Assistance, ";}
        if (!empty($row2['want_serv_5'])){echo"Career and Academic Advising/Testing, ";}
        if (!empty($row2['want_serv_2'])){echo"Scholarship Search Information, ";}
        if (!empty($row2['want_serv_3'])){echo"Loan Default Assistance, ";}
        if (!empty($row2['want_serv_6'])){echo"Financial Literacy Education,";}echo"
    </p>
    <p style='margin:4px 0 0 15px;'>
        <span class='blue'>FASA: </span>";
        if (!empty($row2['fasfsa_amt'])){$fa = $row2['fasfsa_amt'];}        
        if (isset($fa)){$tf = $fa; echo"$".$tf; } else{echo"N/A";}echo" 
        <span class='blue'>Grants: </span>";
        if (!empty($row2['stgrants_amt'])){$sg = $row2['stgrants_amt'];}
        if (!empty($row2['logrants_amt'])){$lg = $row2['logrants_amt'];}
        if (isset($sg)&&isset($lg)){$tg = $sg + $lg; echo"$".$tg; } else{echo"N/A";}echo" 
        <span class='blue'>scholarships: </span>";
        if (!empty($row2['stscol_amt'])){$ss = $row2['stscol_amt'];}
        if (!empty($row2['priscol_amt'])){$ps = $row2['priscol_amt'];}
        if (isset($ss)&&isset($ps)){$ts = $ss + $ps; echo"$".$ts; } else{echo"N/A";}echo" 
        <span class='blue'>Loans: </span>";
        if (!empty($row2['subloan_amt'])){$sl = $row2['subloan_amt'];}
        if (!empty($row2['unsubloan_amt'])){$ul = $row2['unsubloan_amt'];}
        if (isset($sl)&&isset($ul)){$tl = $sl + $ul; echo"$".$tl; } else{echo"N/A";}echo" 
        <strong><span class='blue'>Total: </span></strong>";
        if (isset($tf)||isset($tg)||isset($ts)||isset($tl)){$gt = $tf + $tg + $ts + $tl; echo"$".$gt; } else{echo"N/A";}echo"
    </p>
    <div style='border-bottom:1px solid skyblue;padding-bottom:10px;'></div>";
    }
    echo"</ul><br />";
}
?>

Edited by ManiacDan
Link to comment
Share on other sites

The following example shows (untested) how to query for the rows you want in the order that you want them, then how to close out a previous section and output a new heading anytime a value in your data changes -

 

<?php
$ecss=$_GET['ecs']; $date1=$_GET['date1']; $date2=$_GET['date2'];
require '../config.php';
mysql_select_db("learn_sym");

$query = "SELECT * FROM clients WHERE ecs= '$ecss' and date BETWEEN '$date1' AND '$date2' ORDER BY agency,date";
$result = mysql_query($query) or die(mysql_error());
$last_heading = null;
while ($row = mysql_fetch_array($result)){
   if($last_heading != $row['agency']){
       // new heading detected
       if($last_heading != null){
           // not the first one, close out the previous section
           echo"</ul><br />";
       }
       // agency changed, output a new heading
	 echo"<ul style='width:800px; margin:0 auto 0;'>
	 <h4 style='margin:0 10px 0; padding:0;'>"; if (!empty($row['agency'])){echo $row['agency'];} else {echo "Unknown";} echo"</h4>";
       $last_heading = $row['agency'];
   }
   // output the data under each heading
	 echo"<p style='margin:4px 0 0 15px;'>
			 <span class='blue'>Name:</span> ".$row['fname'].",".$row['lname']." -
			 <span class='blue'>Seen On:</span> ".$row['date']." 
			 <span class='blue'>Email:</span> ";if(!empty($row['email'])){echo $row['email'];}else{echo"N/A";}echo" 
			 <span class='blue'>Phonenumber:</span> ";if(!empty($row['cell_phone'])){echo $row['cell_phone'];}else{echo $row['home_phone'];}echo"
	 </p>
	 <p style='margin:4px 0 0 15px; background-color:#f3f3f2;'>
			 <span class='blue'>Services: </span>";
			 if (!empty($row['want_serv_1'])){echo"FASFSA Completion, ";}
			 if (!empty($row['want_serv_4'])){echo"Admissions Application Assistance, ";}
			 if (!empty($row['want_serv_5'])){echo"Career and Academic Advising/Testing, ";}
			 if (!empty($row['want_serv_2'])){echo"Scholarship Search Information, ";}
			 if (!empty($row['want_serv_3'])){echo"Loan Default Assistance, ";}
			 if (!empty($row['want_serv_6'])){echo"Financial Literacy Education,";}echo"
	 </p>
	 <p style='margin:4px 0 0 15px;'>
			 <span class='blue'>FASA: </span>";
			 if (!empty($row['fasfsa_amt'])){$fa = $row['fasfsa_amt'];}		  
			 if (isset($fa)){$tf = $fa; echo"$".$tf; } else{echo"N/A";}echo"
			 <span class='blue'>Grants: </span>";
			 if (!empty($row['stgrants_amt'])){$sg = $row['stgrants_amt'];}
			 if (!empty($row['logrants_amt'])){$lg = $row['logrants_amt'];}
			 if (isset($sg)&&isset($lg)){$tg = $sg + $lg; echo"$".$tg; } else{echo"N/A";}echo"
			 <span class='blue'>scholarships: </span>";
			 if (!empty($row['stscol_amt'])){$ss = $row['stscol_amt'];}
			 if (!empty($row['priscol_amt'])){$ps = $row['priscol_amt'];}
			 if (isset($ss)&&isset($ps)){$ts = $ss + $ps; echo"$".$ts; } else{echo"N/A";}echo"
			 <span class='blue'>Loans: </span>";
			 if (!empty($row['subloan_amt'])){$sl = $row['subloan_amt'];}
			 if (!empty($row['unsubloan_amt'])){$ul = $row['unsubloan_amt'];}
			 if (isset($sl)&&isset($ul)){$tl = $sl + $ul; echo"$".$tl; } else{echo"N/A";}echo"
			 <strong><span class='blue'>Total: </span></strong>";
			 if (isset($tf)||isset($tg)||isset($ts)||isset($tl)){$gt = $tf + $tg + $ts + $tl; echo"$".$gt; } else{echo"N/A";}echo"
	 </p>
	 <div style='border-bottom:1px solid skyblue;padding-bottom:10px;'></div>";
}
// close out the last section, if any
if($last_heading != null){
   echo"</ul><br />";
}
?>

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.