Page 1 of 1

Why nothing happening

Posted: Tue Sep 21, 2010 5:00 am
by Nodral
Hi all

I have several arrays set, and I can guarantee I have values in these as I have tested them by echoing out each value.

However, when I pass it through a user defined function to produce a percentage for each value of the total arry, I get a null return. I don't even get any errors. Any ideas?

:wtf:

Code: Select all

// Produce the % array of each array for use in 2nd report

$percent_morale1=percentCalc($morale_count1[1],$morale_count1[2],$morale_count1[3],$morale_count1[4],$morale_count1[5],$total_morale1);
$percent_morale2=percentCalc($morale_count2[1],$morale_count2[2],$morale_count2[3],$morale_count2[4],$morale_count2[5],$total_morale2);
$percent_morale3=percentCalc($morale_count3[1],$morale_count3[2],$morale_count3[3],$morale_count3[4],$morale_count3[5],$total_morale3);
$percent_morale4=percentCalc($morale_count4[1],$morale_count4[2],$morale_count4[3],$morale_count4[4],$morale_count4[5],$total_morale4);
$percent_morale5=percentCalc($morale_count5[1],$morale_count5[2],$morale_count5[3],$morale_count5[4],$morale_count5[5],$total_morale5);
$percent_morale6=percentCalc($morale_count6[1],$morale_count6[2],$morale_count6[3],$morale_count6[4],$morale_count6[5],$total_morale6);

Code: Select all

<?php
function percentCalc($value1,$value2,$value3,$value4,$value5,$total)
{
	$array=array($value1,$value2,$value3,$value4,$value5);
	while (list($key,$arraycontent)= each($array))
	{
		$c_percent=(($arraycontent/$total)*100);
		$final_array[]=$c_percent;
	}
	
	return $final_array;
}
?>

Code: Select all

<?php
		echo"<td class=\"percent1\" >&nbsp;"; printf("%01.0f",$percent_morale1[1]); echo "%</td>";
		echo"<td class=\"percent2\" >&nbsp;"; printf("%01.0f",$percent_morale1[2]); echo "%</td>";
		echo"<td class=\"percent3\" >&nbsp;"; printf("%01.0f",$percent_morale1[3]); echo "%</td>";
		echo"<td class=\"percent4\" >&nbsp;"; printf("%01.0f",$percent_morale1[4]); echo "%</td>";
		echo"<td class=\"percent5\" >&nbsp;"; printf("%01.0f",$percent_morale1[5]); echo "%</td>";
		?>

Re: Why nothing happening

Posted: Tue Sep 21, 2010 10:58 am
by mikosiko
I did test your code doing a simple example... I changed this line

Code: Select all

$percent_morale1=percentCalc($morale_count1[1],$morale_count1[2],$morale_count1[3],$morale_count1[4],$morale_count1[5],$total_morale1);
to this

Code: Select all

$percent_morale1=percentCalc(5,10,15,20,50);
and your code works, except that every result is displaced by 1 and $percent_morale1[5] in your echo lines is undefined... php arrays start numbering elements in 0 not in 1.

if you are getting only nulls probably you don't have values on your variables $morale_count or you are also displaced by 1 in the array numbering.

And in another note... the part of the code where you have your function is part of the same file where you are calling it or is a separate file?... when you said "I get a null return"... you mean a blank screen or the echoes doesn't show anything in the value placeholder?