Page 1 of 1

Beginner Help Required

Posted: Fri Sep 17, 2010 7:19 am
by Nodral
Hi All

I know this may seem a bit basic, but I'm having real issues with what seems like a simple task.

I have about 60 linear arrays from which I need to produce percentages. I need to "walk" through each array and divide each value by the count of the array (which I have set as a variable) and then multiply by 100 and round to 1 decimal place.

I have tried all sorts of way of doing this but nothing seems to work. Please see code below for example of some of them. Unfortunately I cannot view the error log on my system as I have no access to switch this on with the ISP/Host

Code: Select all

// Reads information from data file

$handle = fopen("morale_data.csv",r);
if ($handle) {
	while(!feof($handle)){
		$line=fgets($handle);
		list($morale1,$morale2,$morale3,$morale4,$morale5,$morale6) = explode(",","$line");
		$morale_array1[]=$morale1;
		$morale_array2[]=$morale2;
		$morale_array3[]=$morale3;
		$morale_array4[]=$morale4;
		$morale_array5[]=$morale5;
		$morale_array6[]=$morale6;
	}
	fclose($handle);
}
// resets and sorts all basic imported arrays
reset($morale_array1);
reset($morale_array2);
reset($morale_array3);
reset($morale_array4);
reset($morale_array5);
reset($morale_array6);

sort($morale_array1);
sort($morale_array2);
sort($morale_array3);
sort($morale_array4);
sort($morale_array5);
sort($morale_array6);

// Adds the identical values within each array to produce a count for each answer

$morale_count1=array_count_values($morale_array1);
$morale_count2=array_count_values($morale_array2);
$morale_count3=array_count_values($morale_array3);
$morale_count4=array_count_values($morale_array4);
$morale_count5=array_count_values($morale_array5);
$morale_count6=array_count_values($morale_array6);

// Produces the total number of not null answers for each question.  This does not use count as fwrite in process always produces a blank line at the end of the file giving dummy null readings.
$total_morale1=($morale_count1[1]+$morale_count1[2]+$morale_count1[3]+$morale_count1[4]+$morale_count1[5]);
$total_morale2=($morale_count2[1]+$morale_count2[2]+$morale_count2[3]+$morale_count2[4]+$morale_count2[5]);
$total_morale3=($morale_count3[1]+$morale_count3[2]+$morale_count3[3]+$morale_count3[4]+$morale_count3[5]);
$total_morale4=($morale_count4[1]+$morale_count4[2]+$morale_count4[3]+$morale_count4[4]+$morale_count4[5]);
$total_morale5=($morale_count5[1]+$morale_count5[2]+$morale_count5[3]+$morale_count5[4]+$morale_count5[5]);
$total_morale6=($morale_count6[1]+$morale_count6[2]+$morale_count6[3]+$morale_count6[4]+$morale_count6[5]);

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

$percent_morale_count1=percentCalc($morale_count1,$total_morale1);

Code: Select all

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

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 7:43 am
by buckit
I didnt take a long hard look... but I did notice that your function has an error... you're missing a ) in your while statement

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 7:47 am
by Nodral
Thanks, corrected that now but still not working.

Any more thoughts anyone?

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 7:49 am
by buckit
elaborate on "not working". you may not be able to see the exact error message, but you know that its not working so you see (or dont see) something. is it returning a wrong value? blank page? returning no value at all?

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 7:56 am
by buckit
another thing about your function... you arent returning anything. you are actually overwriting $final_array with the $array supplied to the function.

try replacing:

Code: Select all

$final_array=$array;
with:

Code: Select all

return $final_array;

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 8:01 am
by Nodral
Sorry, new to all this stuff

I am echoing out $percent_morale_count1 into an HTML page and getting no value returned, just a blank.

Copied file to local system and also getting warnings as errors are enabled.


Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in D:\Documents\AI24\Web\mezoka\morale.php on line 37

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in D:\Documents\AI24\Web\mezoka\morale.php on line 38

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in D:\Documents\AI24\Web\mezoka\morale.php on line 39

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in D:\Documents\AI24\Web\mezoka\morale.php on line 40

Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in D:\Documents\AI24\Web\mezoka\morale.php on line 41

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 8:03 am
by Nodral
So how do I use $final_array. As I said I have about 60 linear arrays I need to perform this function on. Surely they will all then have the name $final_array.

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 9:28 am
by Nodral
Managed to sort this now

Cheers for all your help

Re: Beginner Help Required

Posted: Fri Sep 17, 2010 9:40 am
by buckit
Glad you were able to work it out.

If you dont mind, take a second to explain your solution so others searching/reading can learn.