How can i reuse a variable value which defined inside for loop?

Hi all,

Is possible to reuse a variable value outside for loop? But that variable defined in for loop, actually , this program has 2 for loop, the first one is for $i , and the second is for $k, if you have read below codes , you see a variable names $acd ( $acd= $sumtotal[$i] / $c_0[$k]; ) and it has been defined and calculated inside $k loop; My question is => How can i reuse the $acd value in (for $i loop)? Actually , the $acd value can output in

correctly , but after i rebuild an array like that ----->$record=array(“carriername”=>$carrierarray_all[$i],“carrierid”=>$countryidarray_all[$i],…“acd”=>$acd);and var_dump $record , the $record[“acd”=>$acd] value is not match with the correct value. Please advise, thank you

please see below code.

[php]<?php

for($i=0;$i<$count1;$i++) {

//some code…

for($k=0;$k<$count2;$k++)
		{
			if ($carrierarray_all[$i]==$carrierarray_0[$k] && $countryidarray_all[$i]==$countryidarray_0[$k])
			
        {
			$acd= $sumtotal[$i] / $c_0[$k];
			$acd=number_format(round($acd*100)/100,2);
			
			$checkacd=($f*85)/100;
			$checkacd=number_format(round($checkacd*100)/100,2);
            if ($checkacd > $acd)
            {$pacd=$acd;echo $acd;} //col 14 PACD
}
}

}[/php]

Brgds/Brandon Chau

Hi all , above post detail something wrong , please see below post >.<" thanks (I don’t know why i can’t modify my previous post =.=")

Hi all,

Is possible to reuse a variable value outside for loop? But that variable defined in for loop, actually , this program has 2 for loop, the first one is for $i , and the second is for $k, if you have read below codes , you see a variable names $c_0[$k] and it has been defined inside $k loop; My question is => How can i reuse the $c_0[$k] value in (for $i loop)? Actually , the $c_0[$k] value can output in

correctly , but after i rebuild an array like that ----->$record=array(“carriername”=>$carrierarray_all[$i],“carrierid”=>$countryidarray_all[$i],…“c0”=>$c_0[$k]);and var_dump $record , the $record(“c0”=>$c_0[$k]) value is not match with the correct value. Please advise, thank you

please see below code.

[php]<?php

for($i=0;$i<$count1;$i++) {

//some code…

<?php for($k=0;$k<$count2;$k++) { if($carrierarray_all[$i]==$carrierarray_0[$k] && $countryidarray_all[$i]== $countryidarray_0[$k]) { echo"$c_0[$k]"; # col 6 (S>0) } } $record=array("carriername"=>$carrierarray_all[$i],"carrierid"=>$countryidarray_all[$i].........,"c0"=>$c_0[$k]); var_dump($record); } ?>

[/php]

Brgds/Brandon Chau

Because you’re rebuilding the array outside of the for loops.

Hi richei ,

Thank you for your reply ^^"

Agree. Actually , this program has 5-6 other for loop (e.g for $j /$k) inside (for $i) , because of i need to use (for $j/$k) to match some record with (for $i) , and then show those record on a table, so how can i rebuild an correct array? or i shouldn’t use for loop in this case? whole coding as below ^^" Please help , thanks.

[php]<?php

if (isset($_POST[‘run’]))

{
$table=$_POST[‘table’];
$todate=$_POST[‘todate’];
$totime=$_POST[‘totime’];
$fromtime=$_POST[‘fromtime’];
$fromdate=$_POST[‘fromdate’];
require ‘setup.php’;
$sql=“select carriername,cdr.countryid, shortname,count(cdr.countryid)as a ,sum(totaltime) as c from
$table cdr ,shortName where cdr.countryid=shortName.countryid and
startdate>=’$fromdate’ and startdate<=’$todate’ and
concat(startDate ,’ ‘, startTime) >= ‘$fromdate $fromtime’ and
concat(startDate ,’ ', startTime) <= ‘$todate $totime’ and totaltime>=0 and
customerid<>‘52’ and customerid<>‘10415’ and
cause not like ‘%34%’ and cause not like ‘%38%’ and
cause not like ‘%42%’ and cause not like '%44%‘and
cause not like ‘%47%’ and cause <> ‘-3’ and cause <> ‘3’ and
cause not like ‘%41%’ and cause not like ‘%21%’ and
accounttype not like’wholesa%’ and (computername=‘SOFTAMED2’ or computername=‘SOFTAMED3’) and
cdr.countryid<>0 group by carriername,cdr.countryid order by carriername,cdr.countryid desc”;

$rs=odbc_exec($con,$sql) or die($con);

if(!$rs){exit("SQL error");}

$count=odbc_num_rows($rs);
while($results_array_all = odbc_fetch_array($rs))
{
$carrierarray_all[] = $results_array_all["carriername"];
$countryidarray_all[] = $results_array_all["countryid"];
$shortname_all[]= $results_array_all["shortname"];
$counta_all[] = $results_array_all["a"];
$sumtotal[] = $results_array_all["c"];
$carriername=odbc_result($rs,'carriername');
$countryid=odbc_result($rs,'countryid');
$shortname=odbc_result($rs,'shortname');

}

$sql1="select carriername,countryid, count(countryid)as b from $table cdr  where
         startdate>= '$fromdate' and startdate<='$todate' and concat(startDate ,' ', startTime)  >='$fromdate $fromtime' and
	   concat(startDate ,' ', startTime)  <='$todate $totime' and customerid<>'52' and customerid<>'10415' and duration>29
	   and cause not like '%34%' and cause not like '%38%' and cause not like '%42%' and cause not like '%44%' and
	   cause not like '%47%' and cause <> '-3' and cause <> '3' and cause not like '%41%' and
	   cause not like '%21%' and accounttype not like'wholesa%' and
	   (computername='SOFTAMED2' or computername='SOFTAMED3') and cdr.countryid<>0
	   group by carriername,countryid order by carriername,countryid desc";
	


$rs1=odbc_exec($con,$sql1) or die($con);
$count1=odbc_num_rows($rs1);
while($results_array_29 = odbc_fetch_array($rs1)){
$carrierarray_29[] = $results_array_29["carriername"];
$countryidarray_29[] = $results_array_29["countryid"];
$b_29[] = $results_array_29["b"];

if(!$rs1){exit("SQL error");}

$carriername_29=odbc_result($rs1,'carriername');
$countryid_29=odbc_result($rs1,'countryid');

}

$sql2="select carriername,countryid, count(countryid)as c from $table cdr  where
        startdate>= '$fromdate' and startdate<='$todate' and concat(startDate ,' ', startTime)  >='$fromdate $fromtime' and
        concat(startDate ,' ', startTime)  <='$todate $totime' and customerid<>'52' and customerid<>'10415' and duration>0
        and cause not like '%34%' and cause not like '%38%' and cause not like '%42%' and cause not like '%44%' and
        cause not like '%47%' and cause <> '-3' and cause <> '3' and cause not like '%41%' and
        cause not like '%21%' and accounttype not like'wholesa%' and
        (computername='SOFTAMED2' or computername='SOFTAMED3') and cdr.countryid<>0
        group by carriername,countryid order by carriername,countryid desc";

$rs2=odbc_exec($con,$sql2) or die($con);
$count2=odbc_num_rows($rs2);
while($results_array_0 = odbc_fetch_array($rs2)){
$carrierarray_0[] = $results_array_0["carriername"];
$countryidarray_0[] = $results_array_0["countryid"];
$c_0[] = $results_array_0["c"];

if(!$rs2){exit("SQL error");}

$carriername_0=odbc_result($rs2,'carriername');
$countryid_0=odbc_result($rs2,'countryid');

}

echo "<table border='1' cellpadding=0 >";
echo "
<tr>
<th><font size=2>carrier</font></td>
<th><font size=2>ID</font></td>
<th><font size=2>Contshort_name</font></td>
<th><font size=2>>=0</font></td>
<th><font size=2>S>30</font></td>
<th><font size=2>S>0</font></td>
<th  bgcolor='#FF66CC'><font size=2>R(S>30)</font></td>
<th><font size=2>R(S>0)</font></td>
<th><font size=2>Total Time</font></td>
<th bgcolor='#80FF80'><font size=2>ACD</font></td>
<th bgcolor='#FF66CC'><font size=2>AVER</font></td>
<th bgcolor='#80FF80'><font size=2>AveACD</font></td>
<th><font size=2>PASR</font></td>
<th><font size=2>PACD</font></td>
</tr>
";

for($i=0;$i<$count;$i++)
{
$shortname_aver=$shortname_all[$i];
$sql3=“select aver as e, ACD as f
from contave where
contshortname = ‘$shortname_aver’”;
$rs3=odbc_exec($con,$sql3) or die($con);
while(odbc_fetch_row($rs3))
{
$e= odbc_result($rs3,‘e’);
$f= odbc_result($rs3,‘f’);

if(!$rs1){exit("SQL error");}}
		   

	    echo"<tr align='center'>";
		echo"<td width=200 >$carrierarray_all[$i] </td>"; # col 1 carrier name
		echo"<td width=100>$countryidarray_all[$i]</td>"; # col 2 carrier id
		echo"<td width=100>$shortname_all[$i]</td>"; # col 3 Contshort_name
		echo"<td width=100> $counta_all[$i]</td>"; # col 4 (>=0)
		?>
<?php for($j=0;$j<$count1;$j++) { if ($carrierarray_all[$i]==$carrierarray_29[$j] && $countryidarray_all[$i]== $countryidarray_29[$j]) { global $b29; $b29=0;
		if (isset($b_29[$j])){$b29=$b_29[$j];}
		echo"$b29"; # col 5 (S>30)

		}
		}
		
		?></td>
<?php for($k=0;$k<$count2;$k++) { if ($carrierarray_all[$i]==$carrierarray_0[$k] && $countryidarray_all[$i]== $countryidarray_0[$k])
        {
           
			
			echo"$c_0[$k]"; # col 6 (S>0)
		
		}
		}
		?></td>
<?php for($j=0;$j<$count1;$j++) { if ($carrierarray_all[$i]==$carrierarray_29[$j] && $countryidarray_all[$i]== $countryidarray_29[$j]) { global $b29; $b29=0;
		if (isset($b_29[$j])){$b29=$b_29[$j];}
	    $rs30=$b29 / $counta_all[$i];
		$rs30=number_format(round($rs30*100)/100,2);
		echo"$rs30"; # col 7 R (s>30)
		
		}
		}
		?></td>
<?php for($k=0;$k<$count2;$k++) { if ($carrierarray_all[$i]==$carrierarray_0[$k] && $countryidarray_all[$i]==$countryidarray_0[$k])
        {
            $rs0= $c_0[$k] / $counta_all[$i];
			$rs0=number_format(round($rs0*100)/100,2);
			echo"$rs0";# col8 R (s>0)
		}
		}
		?></td>
<?php if ($sumtotal[$i]>0){
		echo"$sumtotal[$i]"; # col 9 Total
		}
	?></td>
<?php for($k=0;$k<$count2;$k++) { if ($carrierarray_all[$i]==$carrierarray_0[$k] && $countryidarray_all[$i]==$countryidarray_0[$k])
        {
			$acd= $sumtotal[$i] / $c_0[$k];
			$acd=number_format(round($acd*100)/100,2);
			echo"$acd"; # col 10 acd
			} 
		}
		
        echo"<td width=100 bgcolor='#FF66CC'>$e</td>"; #col 11 AVER
		echo"<td width=100 bgcolor='#80FF80'>$f</td>"; #col 12 AveACD
		

?>
<td>
<?php
for($j=0;$j<$count1;$j++)
		{
			if ($carrierarray_all[$i]==$carrierarray_29[$j] && $countryidarray_all[$i]==                    $countryidarray_29[$j])
        {
	 
		if (isset($b_29[$j])){$b29=$b_29[$j];}
$checkasr=$e * (85/100);	
$r30= $b29 / $counta_all[$i] ;
$r30=number_format(round($r30*100)/100,2);
if ($checkasr > $r30)
{$pasr=$r30;echo $pasr;} //col 13 PASR
}
}
?>
</td>
<td>
<?php 
//$checkaver = $f * (85/100);	
for($k=0;$k<$count2;$k++)
		{
			if ($carrierarray_all[$i]==$carrierarray_0[$k] && $countryidarray_all[$i]==$countryidarray_0[$k])
			
        {
			$acd= $sumtotal[$i] / $c_0[$k];
			$acd=number_format(round($acd*100)/100,2);
			
			$checkacd=($f*85)/100;
			$checkacd=number_format(round($checkacd*100)/100,2);
            if ($checkacd > $acd)
            {$pacd=$acd;echo $acd;} //col 14 PACD
}
}
$record=array("carriername"=>$carrierarray_all[$i],"carrierid"=>$countryidarray_all[$i],"shortname"=>$shortname_all[$i],">=0"=>$counta_all[$i],"s>30"=>$b29,"s>0"=>$c_0[$k],"r(s>30)"=>$rs30,"r(s>0)"=>$rs0,"total"=>$sumtotal[$i],"acd"=>$acd,"aver"=>$e,"aveacd"=>$f,"pasr"=>$pasr,"pacd"=>$pacd);

var_dump($record);
}
}

?>

<?php
	
echo "</table>";
odbc_close($con);

?>[/php]

Brgds/Brandon Chau

Sponsor our Newsletter | Privacy Policy | Terms of Service